简体   繁体   English

fs2流:测试fs2服务器

[英]fs2 streams: testing an fs2 server

I have a stream in this form: 我有这种形式的信息流:

val server = for {
  _ <- Stream.eval(initTasks)
  serverBinding <- Stream.eval(...)
} yield serverBinding

I understand that in order to run it, I should do something like: 我知道要运行它,我应该做类似的事情:

server
  .compile
  .drain
  .unsafeRunAsync(_ => ())

Now, I want to write unit-tests for this server, but I can only run my tests when the server is all set-up. 现在,我想为此服务器编写单元测试,但是只有在服务器全部设置好后才能运行测试。 Currently, in my beforeAll block, I have: 当前,在我的beforeAll块中,我有:

testServer
  .interruptWhen(shutdownSignal)
  .compile
  .drain
  .unsafeRunAsync(_ => ())

Thread.sleep(5000)

and in my afterAll block, I have: 在我的afterAll块中,我有:

shutdownSignal.set(true).unsafeRunSync()

to bring down the server after my test. 在测试后关闭服务器。

I was wondering if I could use signals to achieve my stated goal so that I can somehow "wait" on the signal so that the tests are run only once the signal is up. 我想知道我是否可以使用信号来达到我设定的目标,以便我可以以某种方式“等待”信号,以便仅在信号启动后才运行测试。 Alternatively I'd love to know of the generic/idiomatic way of doing this. 另外,我很想知道这样做的通用/惯用方式。

fs2 had this construct called Promise which can be used for this purpose. fs2具有称为Promise的构造,可用于此目的。 It's now deprecated but Cat's Deferred can be used. 现在已弃用,但可以使用Cat's Deferred So before running compile.drain on the stream, we can make the stream complete the promise/deferred value and then in the beforeAll we can wait on its value. 因此,在流上运行compile.drain之前,我们可以使流完成promise / deferred值,然后在beforeAll中等待其值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM