-
Notifications
You must be signed in to change notification settings - Fork 422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
websockets support in zio server #1789
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a87891
initial implementation of websockets support in zio server
5fa7829
simplify match
55e50e5
ping pong test
2feb2cf
some optional ws config support
a85ee3c
fix compile scala3
f5eb1ad
additional test
f5a00bd
updated docs
e80b317
concatenate frames testable by default
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,8 @@ import sttp.ws.{WebSocket, WebSocketFrame} | |
|
||
abstract class ServerWebSocketTests[F[_], S <: Streams[S], ROUTE]( | ||
createServerTest: CreateServerTest[F, S with WebSockets, ROUTE], | ||
val streams: S | ||
val streams: S, | ||
val concatenateFragmentedFrames: Boolean = true | ||
)(implicit | ||
m: MonadError[F] | ||
) { | ||
|
@@ -29,106 +30,143 @@ abstract class ServerWebSocketTests[F[_], S <: Streams[S], ROUTE]( | |
private def stringWs = webSocketBody[String, CodecFormat.TextPlain, String, CodecFormat.TextPlain].apply(streams) | ||
private def stringEcho = functionToPipe((s: String) => s"echo: $s") | ||
|
||
def tests(): List[Test] = List( | ||
testServer( | ||
endpoint.out(stringWs), | ||
"string client-terminated echo" | ||
)((_: Unit) => pureResult(stringEcho.asRight[Unit])) { (backend, baseUri) => | ||
basicRequest | ||
.response(asWebSocket { (ws: WebSocket[IO]) => | ||
for { | ||
_ <- ws.sendText("test1") | ||
_ <- ws.sendText("test2") | ||
m1 <- ws.receiveText() | ||
m2 <- ws.receiveText() | ||
} yield List(m1, m2) | ||
}) | ||
.get(baseUri.scheme("ws")) | ||
.send(backend) | ||
.map(_.body shouldBe Right(List("echo: test1", "echo: test2"))) | ||
}, { | ||
def tests(): List[Test] = { | ||
val basicTests = List( | ||
testServer( | ||
endpoint.out(stringWs), | ||
"string client-terminated echo" | ||
)((_: Unit) => pureResult(stringEcho.asRight[Unit])) { (backend, baseUri) => | ||
basicRequest | ||
.response(asWebSocket { (ws: WebSocket[IO]) => | ||
for { | ||
_ <- ws.sendText("test1") | ||
m1 <- ws.receiveText() | ||
_ <- ws.sendText("test2") | ||
m2 <- ws.receiveText() | ||
} yield List(m1, m2) | ||
}) | ||
.get(baseUri.scheme("ws")) | ||
.send(backend) | ||
.map(_.body shouldBe Right(List("echo: test1", "echo: test2"))) | ||
}, { | ||
|
||
val reqCounter = newRequestCounter[F] | ||
val resCounter = newResponseCounter[F] | ||
val metrics = new MetricsRequestInterceptor[F](List(reqCounter, resCounter), Seq.empty) | ||
val reqCounter = newRequestCounter[F] | ||
val resCounter = newResponseCounter[F] | ||
val metrics = new MetricsRequestInterceptor[F](List(reqCounter, resCounter), Seq.empty) | ||
|
||
testServer(endpoint.out(stringWs).name("metrics"), metricsInterceptor = metrics.some)((_: Unit) => | ||
pureResult(stringEcho.asRight[Unit]) | ||
testServer(endpoint.out(stringWs).name("metrics"), metricsInterceptor = metrics.some)((_: Unit) => | ||
pureResult(stringEcho.asRight[Unit]) | ||
) { (backend, baseUri) => | ||
basicRequest | ||
.response(asWebSocket { (ws: WebSocket[IO]) => | ||
for { | ||
_ <- ws.sendText("test1") | ||
m <- ws.receiveText() | ||
} yield List(m) | ||
}) | ||
.get(baseUri.scheme("ws")) | ||
.send(backend) | ||
.map { r => | ||
r.body shouldBe Right(List("echo: test1")) | ||
reqCounter.metric.value.get() shouldBe 1 | ||
resCounter.metric.value.get() shouldBe 1 | ||
} | ||
} | ||
}, | ||
testServer(endpoint.out(webSocketBody[Fruit, CodecFormat.Json, Fruit, CodecFormat.Json](streams)), "json client-terminated echo")( | ||
(_: Unit) => pureResult(functionToPipe((f: Fruit) => Fruit(s"echo: ${f.f}")).asRight[Unit]) | ||
) { (backend, baseUri) => | ||
basicRequest | ||
.response(asWebSocket { (ws: WebSocket[IO]) => | ||
for { | ||
_ <- ws.sendText("""{"f":"apple"}""") | ||
m1 <- ws.receiveText() | ||
_ <- ws.sendText("""{"f":"orange"}""") | ||
m2 <- ws.receiveText() | ||
} yield List(m1, m2) | ||
}) | ||
.get(baseUri.scheme("ws")) | ||
.send(backend) | ||
.map(_.body shouldBe Right(List("""{"f":"echo: apple"}""", """{"f":"echo: orange"}"""))) | ||
}, | ||
testServer( | ||
endpoint.out(webSocketBody[String, CodecFormat.TextPlain, Option[String], CodecFormat.TextPlain](streams)), | ||
"string server-terminated echo" | ||
)((_: Unit) => | ||
pureResult(functionToPipe[String, Option[String]] { | ||
case "end" => None | ||
case msg => Some(s"echo: $msg") | ||
}.asRight[Unit]) | ||
) { (backend, baseUri) => | ||
basicRequest | ||
.response(asWebSocket { (ws: WebSocket[IO]) => | ||
for { | ||
_ <- ws.sendText("test1") | ||
m <- ws.receiveText() | ||
} yield List(m) | ||
m1 <- ws.eitherClose(ws.receiveText()) | ||
_ <- ws.sendText("test2") | ||
m2 <- ws.eitherClose(ws.receiveText()) | ||
_ <- ws.sendText("end") | ||
m3 <- ws.eitherClose(ws.receiveText()) | ||
} yield List(m1, m2, m3) | ||
}) | ||
.get(baseUri.scheme("ws")) | ||
.send(backend) | ||
.map { r => | ||
r.body shouldBe Right(List("echo: test1")) | ||
reqCounter.metric.value.get() shouldBe 1 | ||
resCounter.metric.value.get() shouldBe 1 | ||
} | ||
} | ||
}, | ||
testServer(endpoint.out(webSocketBody[Fruit, CodecFormat.Json, Fruit, CodecFormat.Json](streams)), "json client-terminated echo")( | ||
(_: Unit) => pureResult(functionToPipe((f: Fruit) => Fruit(s"echo: ${f.f}")).asRight[Unit]) | ||
) { (backend, baseUri) => | ||
basicRequest | ||
.response(asWebSocket { (ws: WebSocket[IO]) => | ||
for { | ||
_ <- ws.sendText("""{"f":"apple"}""") | ||
_ <- ws.sendText("""{"f":"orange"}""") | ||
m1 <- ws.receiveText() | ||
m2 <- ws.receiveText() | ||
} yield List(m1, m2) | ||
}) | ||
.get(baseUri.scheme("ws")) | ||
.send(backend) | ||
.map(_.body shouldBe Right(List("""{"f":"echo: apple"}""", """{"f":"echo: orange"}"""))) | ||
}, | ||
testServer( | ||
endpoint.out(webSocketBody[String, CodecFormat.TextPlain, Option[String], CodecFormat.TextPlain](streams)), | ||
"string server-terminated echo" | ||
)((_: Unit) => | ||
pureResult(functionToPipe[String, Option[String]] { | ||
case "end" => None | ||
case msg => Some(s"echo: $msg") | ||
}.asRight[Unit]) | ||
) { (backend, baseUri) => | ||
basicRequest | ||
.response(asWebSocket { (ws: WebSocket[IO]) => | ||
for { | ||
_ <- ws.sendText("test1") | ||
_ <- ws.sendText("test2") | ||
_ <- ws.sendText("end") | ||
m1 <- ws.eitherClose(ws.receiveText()) | ||
m2 <- ws.eitherClose(ws.receiveText()) | ||
m3 <- ws.eitherClose(ws.receiveText()) | ||
} yield List(m1, m2, m3) | ||
}) | ||
.get(baseUri.scheme("ws")) | ||
.send(backend) | ||
.map( | ||
_.body.map(_.map(_.left.map(_.statusCode))) shouldBe Right( | ||
List(Right("echo: test1"), Right("echo: test2"), Left(WebSocketFrame.close.statusCode)) | ||
.map( | ||
_.body.map(_.map(_.left.map(_.statusCode))) shouldBe Right( | ||
List(Right("echo: test1"), Right("echo: test2"), Left(WebSocketFrame.close.statusCode)) | ||
) | ||
) | ||
) | ||
}, | ||
testServer( | ||
endpoint | ||
.in(isWebSocket) | ||
.errorOut(stringBody) | ||
.out(stringWs), | ||
"non web-socket request" | ||
)(isWS => if (isWS) pureResult(stringEcho.asRight) else pureResult("Not a WS!".asLeft)) { (backend, baseUri) => | ||
basicRequest | ||
.response(asString) | ||
.get(baseUri.scheme("http")) | ||
.send(backend) | ||
.map(_.body shouldBe Left("Not a WS!")) | ||
} | ||
) | ||
}, | ||
testServer( | ||
endpoint | ||
.in(isWebSocket) | ||
.errorOut(stringBody) | ||
.out(stringWs), | ||
"non web-socket request" | ||
)(isWS => if (isWS) pureResult(stringEcho.asRight) else pureResult("Not a WS!".asLeft)) { (backend, baseUri) => | ||
basicRequest | ||
.response(asString) | ||
.get(baseUri.scheme("http")) | ||
.send(backend) | ||
.map(_.body shouldBe Left("Not a WS!")) | ||
}, | ||
testServer( | ||
endpoint.out(stringWs), | ||
"pong on ping" | ||
)((_: Unit) => pureResult(stringEcho.asRight[Unit])) { (backend, baseUri) => | ||
basicRequest | ||
.response(asWebSocket { (ws: WebSocket[IO]) => | ||
for { | ||
_ <- ws.send(WebSocketFrame.ping) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm I'm not sure if all backends give acces to ping-pongs, so if this fails e.g. on akka-http we might need to parametrize the tests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it passed |
||
pong <- ws.receive() | ||
} yield pong | ||
}) | ||
.get(baseUri.scheme("ws")) | ||
.send(backend) | ||
.map(_.body.map(_.isInstanceOf[WebSocketFrame.Pong]) shouldBe Right(true)) | ||
} | ||
) | ||
|
||
val concatenateFramesTest = List( | ||
testServer( | ||
endpoint.out(stringWs.concatenateFragmentedFrames(true)), | ||
"concatenate fragmented frames" | ||
)((_: Unit) => pureResult(stringEcho.asRight[Unit])) { (backend, baseUri) => | ||
basicRequest | ||
.response(asWebSocket { (ws: WebSocket[IO]) => | ||
for { | ||
_ <- ws.send(WebSocketFrame.Text("hello-", finalFragment = false, rsv = None)) | ||
_ <- ws.send(WebSocketFrame.Text("from-", finalFragment = false, rsv = None)) | ||
_ <- ws.send(WebSocketFrame.Text("server", finalFragment = true, rsv = None)) | ||
text <- ws.receiveText() | ||
} yield text | ||
}) | ||
.get(baseUri.scheme("ws")) | ||
.send(backend) | ||
.map(_.body shouldBe Right("echo: hello-from-server")) | ||
} | ||
) | ||
|
||
// TODO: tests for ping/pong (control frames handling) | ||
basicTests ++ (if (concatenateFragmentedFrames) concatenateFramesTest else Nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the changed ordering of operations? didn't the original test work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, the order of received messages was not deterministic for zio-server
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm well that's bad. Either a bug in zio-http, or somewhere else. I'd revert those tests to their original ordering and look for the root cause. That's quite normal WS usage, that you send multiple messages in sequence and then get a response
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's probably zio-http, I'll keep that in mind and see what will came up in the issue I reported - that may be related I think