File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed
Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change @@ -110,3 +110,68 @@ impl Events {
110110 rx
111111 }
112112}
113+
114+ #[ cfg( test) ]
115+ mod test {
116+ use super :: * ;
117+ use crate :: replication:: CoreMethodsError ;
118+
119+ #[ async_std:: test]
120+ async fn test_events ( ) -> Result < ( ) , CoreMethodsError > {
121+ let mut core = crate :: core:: tests:: create_hypercore_with_data ( 0 ) . await ?;
122+
123+ // Check that appending data emits a DataUpgrade and Have event
124+
125+ let mut rx = core. event_subscribe ( ) ;
126+ let handle = async_std:: task:: spawn ( async move {
127+ let mut out = vec ! [ ] ;
128+ loop {
129+ if out. len ( ) == 2 {
130+ return ( out, rx) ;
131+ }
132+ if let Ok ( evt) = rx. recv ( ) . await {
133+ out. push ( evt) ;
134+ }
135+ }
136+ } ) ;
137+ core. append ( b"foo" ) . await ?;
138+ let ( res, mut rx) = handle. await ;
139+ assert ! ( matches!( res[ 0 ] , Event :: DataUpgrade ( _) ) ) ;
140+ assert ! ( matches!(
141+ res[ 1 ] ,
142+ Event :: Have ( Have {
143+ start: 0 ,
144+ length: 1 ,
145+ drop: false
146+ } )
147+ ) ) ;
148+ // no messages in queue
149+ assert ! ( rx. is_empty( ) ) ;
150+
151+ // Check that Hypercore::get for missing data emits a Get event
152+
153+ let handle = async_std:: task:: spawn ( async move {
154+ let mut out = vec ! [ ] ;
155+ loop {
156+ if out. len ( ) == 1 {
157+ return ( out, rx) ;
158+ }
159+ if let Ok ( evt) = rx. recv ( ) . await {
160+ out. push ( evt) ;
161+ }
162+ }
163+ } ) ;
164+ assert_eq ! ( core. get( 1 ) . await ?, None ) ;
165+ let ( res, rx) = handle. await ;
166+ assert ! ( matches!(
167+ res[ 0 ] ,
168+ Event :: Get ( Get {
169+ index: 1 ,
170+ get_result: _
171+ } )
172+ ) ) ;
173+ // no messages in queue
174+ assert ! ( rx. is_empty( ) ) ;
175+ Ok ( ( ) )
176+ }
177+ }
You can’t perform that action at this time.
0 commit comments