我有一个Scala方法,如下所示:
def doAnAsynchOperation(bunchedFilesUnit:BunchOfFilesUnit):
Future[FilesHandled] = {
val handleFuture = future {
BunchOfFilesUnit.unit.map(file => {
//FileParsing code - instantiate a class for file parsing
//Open a fileInputStream
//get an OutputStream
try {
//do some parsing, pass in a "file" invoke a parsing method
} catch {
case e:Throwable =>
//some code
} finally {
fileInputStream.close()
outStream.close()
}
})
}我在我的单元测试中调用了这个方法,在"describe“中写了一个”describe“,在里面,我将有一个" it”。
有5个线程。每个线程都被分配了处理单个BunchedFileUnit的任务。
这5个线程异步调用doAnAsynchOperation方法--每个线程各自独立--来处理自己的BunchedFileUnit,该方法返回一个FutureBunchedFileUnit。
我想要实现的是:
我想写一个或多个测试来测试这个场景。我想加入一个超时。
我不太清楚这里的超时概念,但我假设我必须设置一个超时并预期一个TimeOutException。对于这样的场景,我如何布置我的测试呢?这就是我想要完成的。
目前,我将我的测试安排如下:
import org.scalatest._
import org.scalatest.concurrent.ScalaFutures
import scala.concurrent.{Await, ExecutionContext, duration, Future}
import duration._
import akka.actor.{Actor, ActorRef, ActorSystem}
import org.scalatest.MustMatchers
import java.io.File
class FileHandlerTest extends FunSpec
with MustMatchers
with ScalaFutures
with BeforeAndAfter with SomeTraitThatINeeed {
describe("------------"){
}
}
发布于 2015-04-22 22:27:26
我敢打赌,我猜你在找"whenReady“帮手。我看到您已经导入了ScalaFutures,所以您应该在示例中执行以下操作:
val files = Seq(...)
whenReady(doAnAsynchOperation(files), timeout(Span(6, Seconds))) { result =>
result mustEqual "the correct value"
}如果您正在尝试测试失败条件,您可以尝试给它一个非常短的超时,但我不确定这样做除了确保它不会完成得太快或花费太长时间之外,还能完成另一个任务。
https://stackoverflow.com/questions/28910037
复制相似问题