2015-02-09 73 views
1

我有一個Akka actor以1500字節左右的塊讀取文件的內容。當參與者收到一個NextBlock消息時,它會回覆下一個包裝在ByteString中的數據塊。一對非常簡單的測試和手動觀察數據表明演員工作正常。我使用的是Scala 2.11.5,Akka 2.3.9,ScalaTest 2.2.1和SBT 0.13.5。將ByteString寫入導致NullPointerException的文件

我在設置較大的測試時遇到問題。我想將大約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() 
    } 
} 

我試圖讓周圍這一堆不同的方式

  • 的字節字符串轉換爲字節緩衝區和寫作它通過了ByteChannel(上面顯示)
  • 寫字節串中的個別字節到的BufferedOutputStream
  • 的字節串轉換爲數組[字節]和寫入通過的BufferedOutputStream

不管我怎麼努力我一直在結束了沿

[debug] Running TaskDef(demo.ByteBufferTest, [email protected], 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

回答

1

我沒有找到確切的根本原因,但它似乎是有關當測試類是從斯卡拉測試推出的初始化。

我建議把createTestFile()放在beforeAll()方法中。經過測試,它的工作原理。

override def beforeAll { 
    createTestFile() 
} 
相關問題