11package jsonrpclib .fs2interop
22
3- import weaver ._
4- import jsonrpclib ._
5- import jsonrpclib .fs2interop .FS2Channel
3+ import cats .data .Chain
64import cats .effect .IO
5+ import cats .effect .implicits ._
76import cats .effect .kernel .Ref
8- import cats .data . Chain
7+ import cats .effect . kernel . Resource
98import cats .effect .std .Queue
10- import jsonrpclib .Payload
11- import jsonrpclib .Endpoint
12- import cats .effect .implicits ._
13- import scala .concurrent .duration ._
149import cats .syntax .all ._
15-
16- import com .github .plokhotnyuk .jsoniter_scala .macros .JsonCodecMaker
1710import com .github .plokhotnyuk .jsoniter_scala .core .JsonValueCodec
11+ import com .github .plokhotnyuk .jsoniter_scala .macros .JsonCodecMaker
12+ import fs2 .Stream
1813import jsonrpclib .Codec
19- import cats .effect .kernel .Resource
14+ import jsonrpclib .Endpoint
15+ import jsonrpclib .Payload
16+ import jsonrpclib ._
17+ import jsonrpclib .fs2interop .FS2Channel
18+ import weaver ._
19+
20+ import scala .concurrent .duration ._
2021
2122object FS2ChannelSpec extends SimpleIOSuite {
2223
@@ -25,20 +26,20 @@ object FS2ChannelSpec extends SimpleIOSuite {
2526 implicit val jcodec : JsonValueCodec [IntWrapper ] = JsonCodecMaker .make
2627 }
2728
28- def testRes (name : TestName )(run : Resource [IO , Expectations ]): Unit =
29- test(name)(run.use(e => IO .pure(e)) )
29+ def testRes (name : TestName )(run : Stream [IO , Expectations ]): Unit =
30+ test(name)(run.compile.lastOrError )
3031
3132 testRes(" Round trip" ) {
3233 val endpoint : Endpoint [IO ] = Endpoint [IO ](" inc" ).simple((int : IntWrapper ) => IO (IntWrapper (int.int + 1 )))
3334
3435 for {
35- stdout <- Queue .bounded[IO , Payload ](10 ).toResource
36- stdin <- Queue .bounded[IO , Payload ](10 ).toResource
37- serverSideChannel <- FS2Channel [IO ](fs2. Stream .fromQueueUnterminated(stdin), stdout.offer)
38- clientSideChannel <- FS2Channel [IO ](fs2. Stream .fromQueueUnterminated(stdout), stdin.offer)
39- _ <- serverSideChannel.withEndpoint(endpoint)
36+ stdout <- Queue .bounded[IO , Payload ](10 ).toStream
37+ stdin <- Queue .bounded[IO , Payload ](10 ).toStream
38+ serverSideChannel <- FS2Channel [IO ](Stream .fromQueueUnterminated(stdin), stdout.offer)
39+ clientSideChannel <- FS2Channel [IO ](Stream .fromQueueUnterminated(stdout), stdin.offer)
40+ _ <- Stream .resource( serverSideChannel.withEndpoint(endpoint) )
4041 remoteFunction = clientSideChannel.simpleStub[IntWrapper , IntWrapper ](" inc" )
41- result <- remoteFunction(IntWrapper (1 )).toResource
42+ result <- remoteFunction(IntWrapper (1 )).toStream
4243 } yield {
4344 expect.same(result, IntWrapper (2 ))
4445 }
@@ -47,12 +48,12 @@ object FS2ChannelSpec extends SimpleIOSuite {
4748 testRes(" Endpoint not mounted" ) {
4849
4950 for {
50- stdout <- Queue .bounded[IO , Payload ](10 ).toResource
51- stdin <- Queue .bounded[IO , Payload ](10 ).toResource
52- serverSideChannel <- FS2Channel [IO ](fs2. Stream .fromQueueUnterminated(stdin), stdout.offer)
53- clientSideChannel <- FS2Channel [IO ](fs2. Stream .fromQueueUnterminated(stdout), stdin.offer)
51+ stdout <- Queue .bounded[IO , Payload ](10 ).toStream
52+ stdin <- Queue .bounded[IO , Payload ](10 ).toStream
53+ serverSideChannel <- FS2Channel [IO ](Stream .fromQueueUnterminated(stdin), stdout.offer)
54+ clientSideChannel <- FS2Channel [IO ](Stream .fromQueueUnterminated(stdout), stdin.offer)
5455 remoteFunction = clientSideChannel.simpleStub[IntWrapper , IntWrapper ](" inc" )
55- result <- remoteFunction(IntWrapper (1 )).attempt.toResource
56+ result <- remoteFunction(IntWrapper (1 )).attempt.toStream
5657 } yield {
5758 expect.same(result, Left (ErrorPayload (- 32601 , " Method inc not found" , None )))
5859 }
@@ -66,13 +67,13 @@ object FS2ChannelSpec extends SimpleIOSuite {
6667 }
6768
6869 for {
69- stdout <- Queue .bounded[IO , Payload ](10 ).toResource
70- stdin <- Queue .bounded[IO , Payload ](10 ).toResource
71- serverSideChannel <- FS2Channel [IO ](fs2. Stream .fromQueueUnterminated(stdin), payload => stdout.offer(payload))
72- clientSideChannel <- FS2Channel [IO ](fs2. Stream .fromQueueUnterminated(stdout), payload => stdin.offer(payload))
73- _ <- serverSideChannel.withEndpoint(endpoint)
70+ stdout <- Queue .bounded[IO , Payload ](10 ).toStream
71+ stdin <- Queue .bounded[IO , Payload ](10 ).toStream
72+ serverSideChannel <- FS2Channel [IO ](Stream .fromQueueUnterminated(stdin), payload => stdout.offer(payload))
73+ clientSideChannel <- FS2Channel [IO ](Stream .fromQueueUnterminated(stdout), payload => stdin.offer(payload))
74+ _ <- Stream .resource( serverSideChannel.withEndpoint(endpoint) )
7475 remoteFunction = clientSideChannel.simpleStub[IntWrapper , IntWrapper ](" inc" )
75- timedResults <- (1 to 10 ).toList.map(IntWrapper (_)).parTraverse(remoteFunction).toResource. timed
76+ timedResults <- (1 to 10 ).toList.map(IntWrapper (_)).parTraverse(remoteFunction).timed.toStream
7677 } yield {
7778 val (time, results) = timedResults
7879 expect.same(results, (2 to 11 ).toList.map(IntWrapper (_))) &&
0 commit comments