@@ -2,6 +2,7 @@ use futures::channel::oneshot;
2
2
use futures:: executor:: { block_on, block_on_stream} ;
3
3
use futures:: future:: { self , join, Future , FutureExt , TryFutureExt } ;
4
4
use futures:: stream:: { FuturesOrdered , StreamExt } ;
5
+ use futures:: task:: Poll ;
5
6
use futures_test:: task:: noop_context;
6
7
use std:: any:: Any ;
7
8
@@ -45,6 +46,69 @@ fn works_2() {
45
46
assert ! ( stream. poll_next_unpin( & mut cx) . is_ready( ) ) ;
46
47
}
47
48
49
+ #[ test]
50
+ fn test_push_front ( ) {
51
+ let ( a_tx, a_rx) = oneshot:: channel :: < i32 > ( ) ;
52
+ let ( b_tx, b_rx) = oneshot:: channel :: < i32 > ( ) ;
53
+ let ( c_tx, c_rx) = oneshot:: channel :: < i32 > ( ) ;
54
+ let ( d_tx, d_rx) = oneshot:: channel :: < i32 > ( ) ;
55
+
56
+ let mut stream = FuturesOrdered :: new ( ) ;
57
+
58
+ let mut cx = noop_context ( ) ;
59
+
60
+ stream. push_back ( a_rx) ;
61
+ stream. push_back ( b_rx) ;
62
+ stream. push_back ( c_rx) ;
63
+
64
+ a_tx. send ( 1 ) . unwrap ( ) ;
65
+ b_tx. send ( 2 ) . unwrap ( ) ;
66
+ c_tx. send ( 3 ) . unwrap ( ) ;
67
+
68
+ // 1 and 2 should be received in order
69
+ assert_eq ! ( Poll :: Ready ( Some ( Ok ( 1 ) ) ) , stream. poll_next_unpin( & mut cx) ) ;
70
+ assert_eq ! ( Poll :: Ready ( Some ( Ok ( 2 ) ) ) , stream. poll_next_unpin( & mut cx) ) ;
71
+
72
+ stream. push_front ( d_rx) ;
73
+ d_tx. send ( 4 ) . unwrap ( ) ;
74
+
75
+ // we pushed `d_rx` to the front and sent 4, so we should recieve 4 next
76
+ // and then 3 after it
77
+ assert_eq ! ( Poll :: Ready ( Some ( Ok ( 4 ) ) ) , stream. poll_next_unpin( & mut cx) ) ;
78
+ assert_eq ! ( Poll :: Ready ( Some ( Ok ( 3 ) ) ) , stream. poll_next_unpin( & mut cx) ) ;
79
+ }
80
+
81
+ #[ test]
82
+ fn test_push_back ( ) {
83
+ let ( a_tx, a_rx) = oneshot:: channel :: < i32 > ( ) ;
84
+ let ( b_tx, b_rx) = oneshot:: channel :: < i32 > ( ) ;
85
+ let ( c_tx, c_rx) = oneshot:: channel :: < i32 > ( ) ;
86
+ let ( d_tx, d_rx) = oneshot:: channel :: < i32 > ( ) ;
87
+
88
+ let mut stream = FuturesOrdered :: new ( ) ;
89
+
90
+ let mut cx = noop_context ( ) ;
91
+
92
+ stream. push_back ( a_rx) ;
93
+ stream. push_back ( b_rx) ;
94
+ stream. push_back ( c_rx) ;
95
+
96
+ a_tx. send ( 1 ) . unwrap ( ) ;
97
+ b_tx. send ( 2 ) . unwrap ( ) ;
98
+ c_tx. send ( 3 ) . unwrap ( ) ;
99
+
100
+ // All results should be received in order
101
+
102
+ assert_eq ! ( Poll :: Ready ( Some ( Ok ( 1 ) ) ) , stream. poll_next_unpin( & mut cx) ) ;
103
+ assert_eq ! ( Poll :: Ready ( Some ( Ok ( 2 ) ) ) , stream. poll_next_unpin( & mut cx) ) ;
104
+
105
+ stream. push_back ( d_rx) ;
106
+ d_tx. send ( 4 ) . unwrap ( ) ;
107
+
108
+ assert_eq ! ( Poll :: Ready ( Some ( Ok ( 3 ) ) ) , stream. poll_next_unpin( & mut cx) ) ;
109
+ assert_eq ! ( Poll :: Ready ( Some ( Ok ( 4 ) ) ) , stream. poll_next_unpin( & mut cx) ) ;
110
+ }
111
+
48
112
#[ test]
49
113
fn from_iterator ( ) {
50
114
let stream = vec ! [ future:: ready:: <i32 >( 1 ) , future:: ready:: <i32 >( 2 ) , future:: ready:: <i32 >( 3 ) ]
0 commit comments