1

我有一个 Akka 演员,它以大约 1500 字节的块读取文件的内容。当 Actor 收到NextBlock消息时,它会使用包装在 ByteString 中的下一个数据块进行回复。几个非常简单的测试和人工目测数据表明演员工作正常。我正在使用 Scala 2.11.5、Akka 2.3.9、ScalaTest 2.2.1 和 SBT 0.13.5。

我在设置更大的测试时遇到问题。我想将大约 10kB 左右的测试模式数据写入文件,然后验证 Actor 的输出是否符合我的预期。我正在通过 ByteStringBuilder 创建测试模式。当我将测试数据写入文件时,我得到了 NullPointerExceptions。这是显示该问题的测试的条带化版本的代码:

import java.nio.ByteOrder
import java.nio.file.StandardOpenOption._
import java.nio.file.{Files, Paths}

import akka.actor.ActorSystem
import akka.testkit.TestKit
import akka.util.{ByteString, ByteStringBuilder}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}

class ByteBufferTest extends TestKit(ActorSystem("ByteBufferTest"))
with WordSpecLike with Matchers with BeforeAndAfterAll {

  implicit val byteOrder = ByteOrder.BIG_ENDIAN
  val file = Paths.get("test.data")

  Files.deleteIfExists(file)
  createTestFile()

  "A ByteBufferTest" must { "work" in { assert(true) } }

  def createTestFile(): Unit = {
    val out = Files.newByteChannel(file, CREATE, WRITE)
    out.write(contents.toByteBuffer) // Here is where the NPE occurs
    out.close()
  }

  val contents: ByteString = {
    val builder = new ByteStringBuilder
    (0 to 255).foreach(builder.putInt)
    builder.result()
  }

  override protected def afterAll(): Unit = {
    Files.delete(file)
    system.shutdown()
  }
}

我尝试了很多不同的方法来解决这个问题

  • 将 ByteString 转换为 ByteBuffer 并通过 ByteChannel 写入(如上所示)
  • 将 ByteString 的各个字节写入 BufferedOutputStream
  • 将 ByteString 转换为 Array[Byte] 并通过 BufferedOutputStream 写入

无论我尝试什么,我都会以类似的方式结束

[debug] Running TaskDef(demo.ByteBufferTest, org.scalatest.tools.Framework$$anon$1@40fbf2c, false, [SuiteSelector])
java.lang.NullPointerException at demo.ByteBufferTest.createTestFile(ByteBufferTest.scala:32)
  at demo.ByteBufferTest.<init>(ByteBufferTest.scala:21)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
  at java.lang.Class.newInstance(Class.java:374)
  at org.scalatest.tools.Framework$ScalaTestTask.execute(Framework.scala:641)
  at sbt.TestRunner.runTest$1(TestFramework.scala:84)
  at sbt.TestRunner.run(TestFramework.scala:94)
  at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:219)
  at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:219)
  at sbt.TestFramework$.sbt$TestFramework$$withContextLoader(TestFramework.scala:207)
  at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:219)
  at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:219)
  at sbt.TestFunction.apply(TestFramework.scala:224)
  at sbt.Tests$$anonfun$7.apply(Tests.scala:196)
  at sbt.Tests$$anonfun$7.apply(Tests.scala:196)
  at sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:45)
  at sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:45)
  at sbt.std.Transform$$anon$4.work(System.scala:64)
  at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
  at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
  at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
  at sbt.Execute.work(Execute.scala:244)
  at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
  at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
  at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
  at sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
  at java.util.concurrent.FutureTask.run(FutureTask.java:262)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
  at java.util.concurrent.FutureTask.run(FutureTask.java:262)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:745)

有人知道我做错了什么吗?

我在github上放了一个示例项目

4

1 回答 1

1

我没有找到确切的根本原因,但它似乎与从 scala 测试启动测试类时的初始化有关。

我建议把它放在createTestFile()beforeAll() 的方法中。经过测试,它可以工作。

override def beforeAll {
    createTestFile()
}
于 2015-02-09T18:06:18.940 回答