1- use std:: fmt:: Debug ;
21use codee:: { Decoder , Encoder } ;
32use futures:: { Sink , Stream } ;
43use pin_project:: pin_project;
4+ use std:: fmt:: Debug ;
55use std:: task:: { ready, Poll } ;
66use url:: Url ;
77
88use crate :: {
99 endpoints:: defns:: api:: websocket_updates:: { ClientMsg , ServerMsg } ,
10- sync_engine:: websocket_updates:: binary_transport:: { BinaryTransportError , BinaryTransportTrait , ConnOrClosedError , JsonSerdeToBinaryCodec } ,
10+ sync_engine:: websocket_updates:: binary_transport:: {
11+ BinaryTransportError , BinaryTransportTrait , ConnOrClosedError , JsonSerdeToBinaryCodec ,
12+ } ,
1113} ;
1214
13-
14-
1515#[ pin_project]
1616pub struct Transport < I > {
1717 #[ pin]
3434
3535 fn start_send ( self : std:: pin:: Pin < & mut Self > , item : ClientMsg ) -> Result < ( ) , Self :: Error > {
3636 let this = self . project ( ) ;
37- let encoded = JsonSerdeToBinaryCodec :: encode ( & item) . map_err ( BinaryTransportError :: Encode ) ?;
37+ let encoded =
38+ JsonSerdeToBinaryCodec :: encode ( & item) . map_err ( BinaryTransportError :: Encode ) ?;
3839 this. inner
3940 . start_send ( encoded)
4041 . map_err ( BinaryTransportError :: Conn )
@@ -95,14 +96,15 @@ where
9596 }
9697}
9798
98- pub trait TransportTrait : Sized + Sink < ClientMsg > + Stream < Item = Result < ServerMsg , Self :: TransportError > > {
99+ pub trait TransportTrait :
100+ Sized + Sink < ClientMsg > + Stream < Item = Result < ServerMsg , Self :: TransportError > >
101+ {
99102 type TransportError : Debug ;
100103
101104 #[ allow( async_fn_in_trait) ]
102105 async fn establish ( url : & Url ) -> Result < Self , Self :: TransportError > ;
103106}
104107
105-
106108impl < T > TransportTrait for Transport < T >
107109where
108110 T : BinaryTransportTrait ,
@@ -116,4 +118,72 @@ where
116118
117119 Ok ( Transport { inner } )
118120 }
121+ }
122+
123+ #[ cfg( test) ]
124+ pub mod tests {
125+ use std:: { pin:: Pin , task:: { Context , Poll } } ;
126+
127+ use futures:: { channel:: mpsc, Sink , Stream } ;
128+
129+ use crate :: endpoints:: defns:: api:: websocket_updates:: { ClientMsg , ServerMsg } ;
130+
131+ pub struct MockTransportHandler {
132+ send : mpsc:: Sender < ServerMsg > ,
133+ recv : mpsc:: Receiver < ClientMsg > ,
134+ }
135+
136+ pub struct TestTransport {
137+ recv : mpsc:: Receiver < ServerMsg > ,
138+ send : mpsc:: Sender < ClientMsg > ,
139+ }
140+
141+ impl TestTransport {
142+ pub fn new ( ) -> ( Self , MockTransportHandler ) {
143+ let ( server_msg_sender, server_msg_receiver) = mpsc:: channel ( 100 ) ;
144+ let ( client_msg_sender, client_msg_receiver) = mpsc:: channel ( 100 ) ;
145+
146+ (
147+ Self { recv : server_msg_receiver, send : client_msg_sender } ,
148+ MockTransportHandler { send : server_msg_sender, recv : client_msg_receiver } ,
149+ )
150+ }
151+ }
152+
153+ impl Stream for TestTransport {
154+ type Item = Result < ServerMsg , mpsc:: SendError > ;
155+
156+ fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
157+ Pin :: new ( & mut self . recv ) . poll_next ( cx) . map ( |opt| opt. map ( Ok ) )
158+ }
159+ }
160+
161+ impl Sink < ClientMsg > for TestTransport {
162+ type Error = mpsc:: SendError ;
163+
164+ fn poll_ready (
165+ mut self : Pin < & mut Self > ,
166+ cx : & mut Context < ' _ > ,
167+ ) -> Poll < Result < ( ) , Self :: Error > > {
168+ Pin :: new ( & mut self . send ) . poll_ready ( cx)
169+ }
170+
171+ fn start_send ( mut self : Pin < & mut Self > , item : ClientMsg ) -> Result < ( ) , Self :: Error > {
172+ Pin :: new ( & mut self . send ) . start_send ( item)
173+ }
174+
175+ fn poll_flush (
176+ mut self : Pin < & mut Self > ,
177+ cx : & mut Context < ' _ > ,
178+ ) -> Poll < Result < ( ) , Self :: Error > > {
179+ Pin :: new ( & mut self . send ) . poll_flush ( cx)
180+ }
181+
182+ fn poll_close (
183+ mut self : Pin < & mut Self > ,
184+ cx : & mut Context < ' _ > ,
185+ ) -> Poll < Result < ( ) , Self :: Error > > {
186+ Pin :: new ( & mut self . send ) . poll_close ( cx)
187+ }
188+ }
119189}
0 commit comments