async-graphql/tests/subscription_websocket_grap...

730 lines
17 KiB
Rust
Raw Permalink Normal View History

2022-04-19 04:25:11 +00:00
use std::{
pin::Pin,
sync::{Arc, Mutex},
time::Duration,
};
2022-04-19 04:25:11 +00:00
use async_graphql::{http::WebSocketProtocols, *};
use futures_channel::mpsc;
2022-04-19 04:25:11 +00:00
use futures_util::{
stream::{BoxStream, Stream, StreamExt},
SinkExt,
};
2020-09-11 08:41:56 +00:00
#[tokio::test]
2020-09-11 08:41:56 +00:00
pub async fn test_subscription_ws_transport() {
2021-11-20 03:16:48 +00:00
struct Query;
2020-09-11 08:41:56 +00:00
#[Object]
2021-11-20 03:16:48 +00:00
impl Query {
async fn value(&self) -> i32 {
10
}
}
2020-09-11 08:41:56 +00:00
2021-11-20 03:16:48 +00:00
struct Subscription;
2020-09-11 08:41:56 +00:00
#[Subscription]
2021-11-20 03:16:48 +00:00
impl Subscription {
2020-09-11 08:41:56 +00:00
async fn values(&self) -> impl Stream<Item = i32> {
2020-10-16 06:49:22 +00:00
futures_util::stream::iter(0..10)
2020-09-11 08:41:56 +00:00
}
}
2021-11-20 03:16:48 +00:00
let schema = Schema::new(Query, EmptyMutation, Subscription);
let (mut tx, rx) = mpsc::unbounded();
2020-12-04 08:44:48 +00:00
let mut stream = http::WebSocket::new(schema, rx, WebSocketProtocols::GraphQLWS);
2020-09-17 18:22:54 +00:00
tx.send(
serde_json::to_string(&value!({
2020-09-17 18:22:54 +00:00
"type": "connection_init",
}))
.unwrap(),
)
.await
.unwrap();
2020-09-11 08:41:56 +00:00
assert_eq!(
serde_json::from_str::<serde_json::Value>(&stream.next().await.unwrap().unwrap_text())
.unwrap(),
2020-09-17 18:22:54 +00:00
serde_json::json!({
"type": "connection_ack",
}),
2020-09-11 08:41:56 +00:00
);
2020-09-17 18:22:54 +00:00
tx.send(
serde_json::to_string(&value!({
2020-09-17 18:22:54 +00:00
"type": "start",
"id": "1",
"payload": {
"query": "subscription { values }"
},
}))
.unwrap(),
)
.await
.unwrap();
2020-09-11 08:41:56 +00:00
for i in 0..10 {
assert_eq!(
serde_json::from_str::<serde_json::Value>(&stream.next().await.unwrap().unwrap_text())
.unwrap(),
2020-09-17 18:22:54 +00:00
serde_json::json!({
2020-12-04 08:44:48 +00:00
"type": "next",
2020-09-17 18:22:54 +00:00
"id": "1",
"payload": { "data": { "values": i } },
}),
2020-09-11 08:41:56 +00:00
);
}
assert_eq!(
serde_json::from_str::<serde_json::Value>(&stream.next().await.unwrap().unwrap_text())
.unwrap(),
2020-09-17 18:22:54 +00:00
serde_json::json!({
"type": "complete",
"id": "1",
}),
2020-09-11 08:41:56 +00:00
);
}
#[tokio::test]
2020-09-11 08:41:56 +00:00
pub async fn test_subscription_ws_transport_with_token() {
struct Token(String);
2021-11-20 03:16:48 +00:00
struct Query;
2020-09-11 08:41:56 +00:00
#[Object]
2021-11-20 03:16:48 +00:00
impl Query {
async fn value(&self) -> i32 {
10
}
}
2020-09-11 08:41:56 +00:00
2021-11-20 03:16:48 +00:00
struct Subscription;
2020-09-11 08:41:56 +00:00
#[Subscription]
2021-11-20 03:16:48 +00:00
impl Subscription {
Rework errors This completely overhauls the error system used in async-graphql. - `Error` has been renamed to `ServerError` and `FieldError` has been renamed to just `Error`. This is because `FieldError` is by far the most common error that users will have to use so it makes sense to use the most obvious error name. Also, the current name didn't make sense as it was used for things other than field errors, such as the data callback for websockets. - `ServerError` has been made completely opaque. Before it was an enum of all the possible errors, but now it just contains an error message, the locations, the path and extensions. It is a shame that we lose information, it makes more sense as _conceptually_ GraphQL does not provide that information. It also frees us to change the internals of async-graphql a lot more. - The path of errors is no longer an opaque JSON value but a regular type, `Vec<PathSegment>`. The type duplication of `PathSegment` and `QueryPathSegment` is unfortunate, I plan to work on this in the future. - Now that `ServerError` is opaque, `RuleError` has been removed from the public API, making it simpler. - Additionally `QueryError` has been completely removed. Instead the error messages are constructed ad-hoc; I took care to never repeat an error message. - Instead of constructing field-not-found errors inside the implementations of field resolvers they now return `Option`s, where a `None` value is representative of the field not being found. - As an unfortunate consequence of the last change, self-referential types based on the output of a subscription resolver can no longer be created. This does not mean anything for users, but causes lifetime issues in the implementation of merged objects. I fixed it with a bit of a hack, but this'll have to be looked into further. - `InputValueError` now has a generic parameter - it's kind of weird but it's necessary for ergonomics. It also improves error messages. - The `ErrorExtensions` trait has been removed. I didn't think the `extend` method was necessary since `From` impls exist. But the ergonomics are still there with a new trait `ExtendError`, which is implemented for both errors and results. - `Response` now supports serializing multiple errors. This allows for nice things like having multiple validation errors not be awkwardly shoved into a single error. - When an error occurs in execution, data is sent as `null`. This is slightly more compliant with the spec but the algorithm described in <https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has yet to be implemented.
2020-09-29 19:06:44 +00:00
async fn values(&self, ctx: &Context<'_>) -> Result<impl Stream<Item = i32>> {
2020-09-11 08:41:56 +00:00
if ctx.data_unchecked::<Token>().0 != "123456" {
return Err("forbidden".into());
}
2020-10-16 06:49:22 +00:00
Ok(futures_util::stream::iter(0..10))
2020-09-11 08:41:56 +00:00
}
}
2021-11-20 03:16:48 +00:00
let schema = Schema::new(Query, EmptyMutation, Subscription);
let (mut tx, rx) = mpsc::unbounded();
2021-11-09 09:01:51 +00:00
let mut stream = http::WebSocket::new(schema.clone(), rx, WebSocketProtocols::GraphQLWS)
2021-11-10 12:03:09 +00:00
.on_connection_init(|value| async {
2020-09-17 18:22:54 +00:00
#[derive(serde::Deserialize)]
struct Payload {
token: String,
}
2020-09-11 08:41:56 +00:00
2020-09-17 18:22:54 +00:00
let payload: Payload = serde_json::from_value(value).unwrap();
let mut data = Data::default();
data.insert(Token(payload.token));
Ok(data)
2021-11-09 09:01:51 +00:00
});
2020-09-17 18:22:54 +00:00
tx.send(
serde_json::to_string(&value!({
2020-09-17 18:22:54 +00:00
"type": "connection_init",
"payload": { "token": "123456" }
}))
.unwrap(),
)
.await
.unwrap();
2020-09-11 08:41:56 +00:00
assert_eq!(
Some(value!({
2020-09-17 18:22:54 +00:00
"type": "connection_ack",
2020-09-11 08:41:56 +00:00
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
2020-09-11 08:41:56 +00:00
);
2020-09-17 18:22:54 +00:00
tx.send(
serde_json::to_string(&value!({
2020-09-17 18:22:54 +00:00
"type": "start",
"id": "1",
"payload": {
"query": "subscription { values }"
},
}))
.unwrap(),
)
.await
.unwrap();
2020-09-11 08:41:56 +00:00
for i in 0..10 {
assert_eq!(
Some(value!({
2020-12-04 08:44:48 +00:00
"type": "next",
2020-09-17 18:22:54 +00:00
"id": "1",
"payload": { "data": { "values": i } },
2020-09-11 08:41:56 +00:00
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
2020-09-11 08:41:56 +00:00
);
}
assert_eq!(
Some(value!({
2020-09-17 18:22:54 +00:00
"type": "complete",
"id": "1",
2020-09-11 08:41:56 +00:00
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
2020-09-11 08:41:56 +00:00
);
2021-11-09 09:01:51 +00:00
let (mut tx, rx) = mpsc::unbounded();
let mut data = Data::default();
data.insert(Token("123456".to_string()));
let mut stream =
http::WebSocket::new(schema, rx, WebSocketProtocols::GraphQLWS).connection_data(data);
tx.send(
serde_json::to_string(&value!({
"type": "connection_init",
}))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
Some(value!({
"type": "connection_ack",
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
);
tx.send(
serde_json::to_string(&value!({
"type": "start",
"id": "1",
"payload": {
"query": "subscription { values }"
},
}))
.unwrap(),
)
.await
.unwrap();
for i in 0..10 {
assert_eq!(
Some(value!({
"type": "next",
"id": "1",
"payload": { "data": { "values": i } },
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
);
}
assert_eq!(
Some(value!({
"type": "complete",
"id": "1",
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
);
2020-09-11 08:41:56 +00:00
}
#[tokio::test]
2020-09-11 08:41:56 +00:00
pub async fn test_subscription_ws_transport_error() {
struct Event {
value: i32,
}
#[Object]
2020-09-11 08:41:56 +00:00
impl Event {
Rework errors This completely overhauls the error system used in async-graphql. - `Error` has been renamed to `ServerError` and `FieldError` has been renamed to just `Error`. This is because `FieldError` is by far the most common error that users will have to use so it makes sense to use the most obvious error name. Also, the current name didn't make sense as it was used for things other than field errors, such as the data callback for websockets. - `ServerError` has been made completely opaque. Before it was an enum of all the possible errors, but now it just contains an error message, the locations, the path and extensions. It is a shame that we lose information, it makes more sense as _conceptually_ GraphQL does not provide that information. It also frees us to change the internals of async-graphql a lot more. - The path of errors is no longer an opaque JSON value but a regular type, `Vec<PathSegment>`. The type duplication of `PathSegment` and `QueryPathSegment` is unfortunate, I plan to work on this in the future. - Now that `ServerError` is opaque, `RuleError` has been removed from the public API, making it simpler. - Additionally `QueryError` has been completely removed. Instead the error messages are constructed ad-hoc; I took care to never repeat an error message. - Instead of constructing field-not-found errors inside the implementations of field resolvers they now return `Option`s, where a `None` value is representative of the field not being found. - As an unfortunate consequence of the last change, self-referential types based on the output of a subscription resolver can no longer be created. This does not mean anything for users, but causes lifetime issues in the implementation of merged objects. I fixed it with a bit of a hack, but this'll have to be looked into further. - `InputValueError` now has a generic parameter - it's kind of weird but it's necessary for ergonomics. It also improves error messages. - The `ErrorExtensions` trait has been removed. I didn't think the `extend` method was necessary since `From` impls exist. But the ergonomics are still there with a new trait `ExtendError`, which is implemented for both errors and results. - `Response` now supports serializing multiple errors. This allows for nice things like having multiple validation errors not be awkwardly shoved into a single error. - When an error occurs in execution, data is sent as `null`. This is slightly more compliant with the spec but the algorithm described in <https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has yet to be implemented.
2020-09-29 19:06:44 +00:00
async fn value(&self) -> Result<i32> {
2020-09-11 08:41:56 +00:00
if self.value < 5 {
Ok(self.value)
} else {
Err("TestError".into())
}
}
}
2021-11-20 03:16:48 +00:00
struct Query;
#[Object]
2021-11-20 03:16:48 +00:00
impl Query {
async fn value(&self) -> i32 {
10
}
}
2020-09-11 08:41:56 +00:00
2021-11-20 03:16:48 +00:00
struct Subscription;
2020-09-11 08:41:56 +00:00
#[Subscription]
2021-11-20 03:16:48 +00:00
impl Subscription {
2020-09-11 08:41:56 +00:00
async fn events(&self) -> impl Stream<Item = Event> {
2020-10-16 06:49:22 +00:00
futures_util::stream::iter((0..10).map(|n| Event { value: n }))
2020-09-11 08:41:56 +00:00
}
}
2021-11-20 03:16:48 +00:00
let schema = Schema::new(Query, EmptyMutation, Subscription);
let (mut tx, rx) = mpsc::unbounded();
2020-12-04 08:44:48 +00:00
let mut stream = http::WebSocket::new(schema, rx, WebSocketProtocols::GraphQLWS);
2020-09-17 18:22:54 +00:00
tx.send(
serde_json::to_string(&value!({
2020-09-17 18:22:54 +00:00
"type": "connection_init"
}))
.unwrap(),
)
.await
.unwrap();
2020-09-11 08:41:56 +00:00
assert_eq!(
Some(value!({
2020-09-17 18:22:54 +00:00
"type": "connection_ack",
2020-09-11 08:41:56 +00:00
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
2020-09-11 08:41:56 +00:00
);
2020-09-17 18:22:54 +00:00
tx.send(
serde_json::to_string(&value!({
2020-09-17 18:22:54 +00:00
"type": "start",
"id": "1",
"payload": {
"query": "subscription { events { value } }"
},
}))
.unwrap(),
)
.await
.unwrap();
2020-09-11 08:41:56 +00:00
for i in 0i32..5 {
assert_eq!(
Some(value!({
2020-12-04 08:44:48 +00:00
"type": "next",
2020-09-17 18:22:54 +00:00
"id": "1",
"payload": { "data": { "events": { "value": i } } },
2020-09-11 08:41:56 +00:00
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
2020-09-11 08:41:56 +00:00
);
}
assert_eq!(
Some(value!({
2020-12-04 08:44:48 +00:00
"type": "next",
2020-09-17 18:22:54 +00:00
"id": "1",
"payload": {
2021-06-07 12:51:20 +00:00
"data": null,
2020-09-17 18:22:54 +00:00
"errors": [{
"message": "TestError",
"locations": [{"line": 1, "column": 25}],
"path": ["events", "value"],
}],
},
2020-09-11 08:41:56 +00:00
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
);
}
#[tokio::test]
pub async fn test_subscription_init_error() {
2021-11-20 03:16:48 +00:00
struct Query;
#[Object]
2021-11-20 03:16:48 +00:00
impl Query {
async fn value(&self) -> i32 {
10
}
}
2021-11-20 03:16:48 +00:00
struct Subscription;
#[Subscription]
2021-11-20 03:16:48 +00:00
impl Subscription {
async fn events(&self) -> impl Stream<Item = i32> {
futures_util::stream::once(async move { 10 })
}
}
2021-11-20 03:16:48 +00:00
let schema = Schema::new(Query, EmptyMutation, Subscription);
let (mut tx, rx) = mpsc::unbounded();
2021-11-09 09:01:51 +00:00
let mut stream = http::WebSocket::new(schema, rx, WebSocketProtocols::GraphQLWS)
2021-11-10 12:03:09 +00:00
.on_connection_init(|_| async move { Err("Error!".into()) });
tx.send(
serde_json::to_string(&value!({
"type": "connection_init"
}))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
(1002, "Error!".to_string()),
dbg!(stream.next().await.unwrap()).unwrap_close()
);
}
#[tokio::test]
pub async fn test_subscription_too_many_initialisation_requests_error() {
2021-11-20 03:16:48 +00:00
struct Query;
#[Object]
2021-11-20 03:16:48 +00:00
impl Query {
async fn value(&self) -> i32 {
10
}
}
2021-11-20 03:16:48 +00:00
struct Subscription;
#[Subscription]
2021-11-20 03:16:48 +00:00
impl Subscription {
async fn events(&self) -> impl Stream<Item = i32> {
futures_util::stream::once(async move { 10 })
}
}
2021-11-20 03:16:48 +00:00
let schema = Schema::new(Query, EmptyMutation, Subscription);
let (mut tx, rx) = mpsc::unbounded();
let mut stream = http::WebSocket::new(schema, rx, WebSocketProtocols::GraphQLWS);
tx.send(
serde_json::to_string(&value!({
"type": "connection_init"
}))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
Some(value!({
"type": "connection_ack",
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
);
tx.send(
serde_json::to_string(&value!({
"type": "connection_init"
}))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
(4429, "Too many initialisation requests.".to_string()),
stream.next().await.unwrap().unwrap_close()
2020-09-11 08:41:56 +00:00
);
}
#[tokio::test]
2020-09-11 08:41:56 +00:00
pub async fn test_query_over_websocket() {
2021-11-20 03:16:48 +00:00
struct Query;
2020-09-11 08:41:56 +00:00
#[Object]
2021-11-20 03:16:48 +00:00
impl Query {
2020-09-11 08:41:56 +00:00
async fn value(&self) -> i32 {
999
}
}
2021-11-20 03:16:48 +00:00
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
let (mut tx, rx) = mpsc::unbounded();
2020-12-04 08:44:48 +00:00
let mut stream = http::WebSocket::new(schema, rx, WebSocketProtocols::GraphQLWS);
2020-09-17 18:22:54 +00:00
tx.send(
serde_json::to_string(&value!({
2020-09-17 18:22:54 +00:00
"type": "connection_init",
}))
.unwrap(),
)
.await
.unwrap();
2020-09-11 08:41:56 +00:00
assert_eq!(
Some(value!({
2020-09-11 08:41:56 +00:00
"type": "connection_ack",
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
2020-09-11 08:41:56 +00:00
);
2020-09-17 18:22:54 +00:00
tx.send(
serde_json::to_string(&value!({
2020-09-17 18:22:54 +00:00
"type": "start",
"id": "1",
"payload": {
"query": "query { value }"
},
}))
.unwrap(),
)
.await
.unwrap();
2020-09-11 08:41:56 +00:00
assert_eq!(
Some(value!({
2020-12-04 08:44:48 +00:00
"type": "next",
2020-09-11 08:41:56 +00:00
"id": "1",
"payload": { "data": { "value": 999 } },
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
2020-09-11 08:41:56 +00:00
);
assert_eq!(
Some(value!({
2020-09-11 08:41:56 +00:00
"type": "complete",
"id": "1",
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
2020-09-11 08:41:56 +00:00
);
}
#[tokio::test]
pub async fn test_start_before_connection_init() {
2021-11-20 03:16:48 +00:00
struct Query;
#[Object]
2021-11-20 03:16:48 +00:00
impl Query {
async fn value(&self) -> i32 {
999
}
}
2021-11-20 03:16:48 +00:00
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
let (mut tx, rx) = mpsc::unbounded();
let mut stream = http::WebSocket::new(schema, rx, WebSocketProtocols::GraphQLWS);
tx.send(
serde_json::to_string(&value!({
"type": "start",
"id": "1",
"payload": {
"query": "query { value }"
},
}))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
stream.next().await.unwrap().unwrap_close(),
(1011, "The handshake is not completed.".to_string())
);
}
#[tokio::test]
pub async fn test_stream_drop() {
type Dropped = Arc<Mutex<bool>>;
struct TestStream {
inner: BoxStream<'static, i32>,
dropped: Dropped,
}
impl Drop for TestStream {
fn drop(&mut self) {
*self.dropped.lock().unwrap() = true;
}
}
impl Stream for TestStream {
type Item = i32;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
self.inner.as_mut().poll_next(cx)
}
}
2021-11-20 03:16:48 +00:00
struct Query;
#[Object]
2021-11-20 03:16:48 +00:00
impl Query {
async fn value(&self) -> i32 {
999
}
}
2021-11-20 03:16:48 +00:00
struct Subscription;
#[Subscription]
2021-11-20 03:16:48 +00:00
impl Subscription {
async fn values(&self, ctx: &Context<'_>) -> impl Stream<Item = i32> {
TestStream {
inner: Box::pin(async_stream::stream! {
loop {
tokio::time::sleep(Duration::from_millis(10)).await;
yield 10;
}
}),
dropped: ctx.data_unchecked::<Dropped>().clone(),
}
}
}
let dropped = Dropped::default();
2021-11-20 03:16:48 +00:00
let schema = Schema::build(Query, EmptyMutation, Subscription)
.data(dropped.clone())
.finish();
let (mut tx, rx) = mpsc::unbounded();
let mut stream = http::WebSocket::new(schema, rx, WebSocketProtocols::GraphQLWS);
tx.send(
serde_json::to_string(&value!({
"type": "connection_init",
}))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
serde_json::from_str::<serde_json::Value>(&stream.next().await.unwrap().unwrap_text())
.unwrap(),
serde_json::json!({
"type": "connection_ack",
}),
);
tx.send(
serde_json::to_string(&value!({
"type": "start",
"id": "1",
"payload": {
"query": "subscription { values }"
},
}))
.unwrap(),
)
.await
.unwrap();
for _ in 0..5 {
assert_eq!(
Some(value!({
"type": "next",
"id": "1",
"payload": { "data": { "values": 10 } },
})),
serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap()
);
}
tx.send(
serde_json::to_string(&value!({
"type": "stop",
"id": "1",
}))
.unwrap(),
)
.await
.unwrap();
loop {
let value = serde_json::from_str(&stream.next().await.unwrap().unwrap_text()).unwrap();
if value
== Some(value!({
"type": "next",
"id": "1",
"payload": { "data": { "values": 10 } },
}))
{
continue;
} else {
assert_eq!(
Some(value!({
"type": "complete",
"id": "1",
})),
value
);
break;
}
}
2021-07-31 15:54:16 +00:00
assert!(*dropped.lock().unwrap());
}
2021-09-16 12:22:04 +00:00
#[tokio::test]
pub async fn test_ping_pong() {
2021-11-20 03:16:48 +00:00
struct Query;
2021-09-16 12:22:04 +00:00
#[Object]
2021-11-20 03:16:48 +00:00
impl Query {
2021-09-16 12:22:04 +00:00
async fn value(&self) -> i32 {
10
}
}
2021-11-20 03:16:48 +00:00
struct Subscription;
2021-09-16 12:22:04 +00:00
#[Subscription]
2021-11-20 03:16:48 +00:00
impl Subscription {
2021-09-16 12:22:04 +00:00
async fn values(&self) -> impl Stream<Item = i32> {
futures_util::stream::iter(0..10)
}
}
2021-11-20 03:16:48 +00:00
let schema = Schema::new(Query, EmptyMutation, Subscription);
2021-09-16 12:22:04 +00:00
let (mut tx, rx) = mpsc::unbounded();
let mut stream = http::WebSocket::new(schema, rx, WebSocketProtocols::GraphQLWS);
tx.send(
serde_json::to_string(&value!({
"type": "connection_init",
}))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
serde_json::from_str::<serde_json::Value>(&stream.next().await.unwrap().unwrap_text())
.unwrap(),
serde_json::json!({
"type": "connection_ack",
}),
);
for _ in 0..5 {
tx.send(
serde_json::to_string(&value!({
"type": "ping",
}))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
serde_json::from_str::<serde_json::Value>(&stream.next().await.unwrap().unwrap_text())
.unwrap(),
serde_json::json!({
"type": "pong",
}),
);
}
tx.send(
serde_json::to_string(&value!({
"type": "pong",
}))
.unwrap(),
)
.await
.unwrap();
assert!(
tokio::time::timeout(Duration::from_millis(100), stream.next())
.await
.is_err()
);
}