@@ -3,7 +3,7 @@ use std::pin::Pin;
3
3
use std:: sync:: { Arc , Mutex } ;
4
4
use std:: task:: Context ;
5
5
6
- use event_listener:: { Event , EventListener } ;
6
+ use event_listener:: { Event , EventListener , QueueStrategy } ;
7
7
use waker_fn:: waker_fn;
8
8
9
9
#[ cfg( target_family = "wasm" ) ]
@@ -17,8 +17,17 @@ fn is_notified(listener: &mut EventListener) -> bool {
17
17
}
18
18
19
19
#[ test]
20
- fn notify ( ) {
21
- let event = Event :: new ( ) ;
20
+ fn notify_fifo ( ) {
21
+ notify ( QueueStrategy :: Fifo )
22
+ }
23
+
24
+ #[ test]
25
+ fn notify_lifo ( ) {
26
+ notify ( QueueStrategy :: Lifo )
27
+ }
28
+
29
+ fn notify ( queue_strategy : QueueStrategy ) {
30
+ let event = Event :: new_with_queue_strategy ( queue_strategy) ;
22
31
23
32
let mut l1 = event. listen ( ) ;
24
33
let mut l2 = event. listen ( ) ;
@@ -31,14 +40,32 @@ fn notify() {
31
40
assert_eq ! ( event. notify( 2 ) , 2 ) ;
32
41
assert_eq ! ( event. notify( 1 ) , 0 ) ;
33
42
34
- assert ! ( is_notified( & mut l1) ) ;
35
- assert ! ( is_notified( & mut l2) ) ;
36
- assert ! ( !is_notified( & mut l3) ) ;
43
+ match queue_strategy {
44
+ QueueStrategy :: Fifo => {
45
+ assert ! ( is_notified( & mut l1) ) ;
46
+ assert ! ( is_notified( & mut l2) ) ;
47
+ assert ! ( !is_notified( & mut l3) ) ;
48
+ }
49
+ QueueStrategy :: Lifo => {
50
+ assert ! ( is_notified( & mut l3) ) ;
51
+ assert ! ( is_notified( & mut l2) ) ;
52
+ assert ! ( !is_notified( & mut l1) ) ;
53
+ }
54
+ }
37
55
}
38
56
39
57
#[ test]
40
- fn notify_additional ( ) {
41
- let event = Event :: new ( ) ;
58
+ fn notify_additional_fifo ( ) {
59
+ notify_additional ( QueueStrategy :: Fifo )
60
+ }
61
+
62
+ #[ test]
63
+ fn notify_additional_lifo ( ) {
64
+ notify_additional ( QueueStrategy :: Lifo )
65
+ }
66
+
67
+ fn notify_additional ( queue_strategy : QueueStrategy ) {
68
+ let event = Event :: new_with_queue_strategy ( queue_strategy) ;
42
69
43
70
let mut l1 = event. listen ( ) ;
44
71
let mut l2 = event. listen ( ) ;
@@ -48,14 +75,32 @@ fn notify_additional() {
48
75
assert_eq ! ( event. notify( 1 ) , 0 ) ;
49
76
assert_eq ! ( event. notify_additional( 1 ) , 1 ) ;
50
77
51
- assert ! ( is_notified( & mut l1) ) ;
52
- assert ! ( is_notified( & mut l2) ) ;
53
- assert ! ( !is_notified( & mut l3) ) ;
78
+ match queue_strategy {
79
+ QueueStrategy :: Fifo => {
80
+ assert ! ( is_notified( & mut l1) ) ;
81
+ assert ! ( is_notified( & mut l2) ) ;
82
+ assert ! ( !is_notified( & mut l3) ) ;
83
+ }
84
+ QueueStrategy :: Lifo => {
85
+ assert ! ( is_notified( & mut l3) ) ;
86
+ assert ! ( is_notified( & mut l2) ) ;
87
+ assert ! ( !is_notified( & mut l1) ) ;
88
+ }
89
+ }
54
90
}
55
91
56
92
#[ test]
57
- fn notify_one ( ) {
58
- let event = Event :: new ( ) ;
93
+ fn notify_one_fifo ( ) {
94
+ notify_one ( QueueStrategy :: Fifo )
95
+ }
96
+
97
+ #[ test]
98
+ fn notify_one_lifo ( ) {
99
+ notify_one ( QueueStrategy :: Lifo )
100
+ }
101
+
102
+ fn notify_one ( queue_strategy : QueueStrategy ) {
103
+ let event = Event :: new_with_queue_strategy ( queue_strategy) ;
59
104
60
105
let mut l1 = event. listen ( ) ;
61
106
let mut l2 = event. listen ( ) ;
@@ -64,16 +109,36 @@ fn notify_one() {
64
109
assert ! ( !is_notified( & mut l2) ) ;
65
110
66
111
assert_eq ! ( event. notify( 1 ) , 1 ) ;
67
- assert ! ( is_notified( & mut l1) ) ;
68
- assert ! ( !is_notified( & mut l2) ) ;
112
+ match queue_strategy {
113
+ QueueStrategy :: Fifo => {
114
+ assert ! ( is_notified( & mut l1) ) ;
115
+ assert ! ( !is_notified( & mut l2) ) ;
116
+ }
117
+ QueueStrategy :: Lifo => {
118
+ assert ! ( is_notified( & mut l2) ) ;
119
+ assert ! ( !is_notified( & mut l1) ) ;
120
+ }
121
+ }
69
122
70
123
assert_eq ! ( event. notify( 1 ) , 1 ) ;
71
- assert ! ( is_notified( & mut l2) ) ;
124
+ match queue_strategy {
125
+ QueueStrategy :: Fifo => assert ! ( is_notified( & mut l2) ) ,
126
+ QueueStrategy :: Lifo => assert ! ( is_notified( & mut l1) ) ,
127
+ }
72
128
}
73
129
74
130
#[ test]
75
- fn notify_all ( ) {
76
- let event = Event :: new ( ) ;
131
+ fn notify_all_fifo ( ) {
132
+ notify_all ( QueueStrategy :: Fifo )
133
+ }
134
+
135
+ #[ test]
136
+ fn notify_all_lifo ( ) {
137
+ notify_all ( QueueStrategy :: Lifo )
138
+ }
139
+
140
+ fn notify_all ( queue_strategy : QueueStrategy ) {
141
+ let event = Event :: new_with_queue_strategy ( queue_strategy) ;
77
142
78
143
let mut l1 = event. listen ( ) ;
79
144
let mut l2 = event. listen ( ) ;
@@ -87,8 +152,8 @@ fn notify_all() {
87
152
}
88
153
89
154
#[ test]
90
- fn drop_notified ( ) {
91
- let event = Event :: new ( ) ;
155
+ fn drop_notified_fifo ( ) {
156
+ let event = Event :: new_with_queue_strategy ( QueueStrategy :: Fifo ) ;
92
157
93
158
let l1 = event. listen ( ) ;
94
159
let mut l2 = event. listen ( ) ;
@@ -101,8 +166,22 @@ fn drop_notified() {
101
166
}
102
167
103
168
#[ test]
104
- fn drop_notified2 ( ) {
105
- let event = Event :: new ( ) ;
169
+ fn drop_notified_lifo ( ) {
170
+ let event = Event :: new_with_queue_strategy ( QueueStrategy :: Lifo ) ;
171
+
172
+ let mut l1 = event. listen ( ) ;
173
+ let mut l2 = event. listen ( ) ;
174
+ let l3 = event. listen ( ) ;
175
+
176
+ assert_eq ! ( event. notify( 1 ) , 1 ) ;
177
+ drop ( l3) ;
178
+ assert ! ( is_notified( & mut l2) ) ;
179
+ assert ! ( !is_notified( & mut l1) ) ;
180
+ }
181
+
182
+ #[ test]
183
+ fn drop_notified2_fifo ( ) {
184
+ let event = Event :: new_with_queue_strategy ( QueueStrategy :: Fifo ) ;
106
185
107
186
let l1 = event. listen ( ) ;
108
187
let mut l2 = event. listen ( ) ;
@@ -115,8 +194,22 @@ fn drop_notified2() {
115
194
}
116
195
117
196
#[ test]
118
- fn drop_notified_additional ( ) {
119
- let event = Event :: new ( ) ;
197
+ fn drop_notified2_lifo ( ) {
198
+ let event = Event :: new_with_queue_strategy ( QueueStrategy :: Lifo ) ;
199
+
200
+ let mut l1 = event. listen ( ) ;
201
+ let mut l2 = event. listen ( ) ;
202
+ let l3 = event. listen ( ) ;
203
+
204
+ assert_eq ! ( event. notify( 2 ) , 2 ) ;
205
+ drop ( l3) ;
206
+ assert ! ( is_notified( & mut l2) ) ;
207
+ assert ! ( !is_notified( & mut l1) ) ;
208
+ }
209
+
210
+ #[ test]
211
+ fn drop_notified_additional_fifo ( ) {
212
+ let event = Event :: new_with_queue_strategy ( QueueStrategy :: Fifo ) ;
120
213
121
214
let l1 = event. listen ( ) ;
122
215
let mut l2 = event. listen ( ) ;
@@ -132,8 +225,25 @@ fn drop_notified_additional() {
132
225
}
133
226
134
227
#[ test]
135
- fn drop_non_notified ( ) {
136
- let event = Event :: new ( ) ;
228
+ fn drop_notified_additional_lifo ( ) {
229
+ let event = Event :: new_with_queue_strategy ( QueueStrategy :: Lifo ) ;
230
+
231
+ let mut l1 = event. listen ( ) ;
232
+ let mut l2 = event. listen ( ) ;
233
+ let mut l3 = event. listen ( ) ;
234
+ let l4 = event. listen ( ) ;
235
+
236
+ assert_eq ! ( event. notify_additional( 1 ) , 1 ) ;
237
+ assert_eq ! ( event. notify( 2 ) , 1 ) ;
238
+ drop ( l4) ;
239
+ assert ! ( is_notified( & mut l3) ) ;
240
+ assert ! ( is_notified( & mut l2) ) ;
241
+ assert ! ( !is_notified( & mut l1) ) ;
242
+ }
243
+
244
+ #[ test]
245
+ fn drop_non_notified_fifo ( ) {
246
+ let event = Event :: new_with_queue_strategy ( QueueStrategy :: Fifo ) ;
137
247
138
248
let mut l1 = event. listen ( ) ;
139
249
let mut l2 = event. listen ( ) ;
@@ -146,8 +256,31 @@ fn drop_non_notified() {
146
256
}
147
257
148
258
#[ test]
149
- fn notify_all_fair ( ) {
150
- let event = Event :: new ( ) ;
259
+ fn drop_non_notified_lifo ( ) {
260
+ let event = Event :: new_with_queue_strategy ( QueueStrategy :: Lifo ) ;
261
+
262
+ let l1 = event. listen ( ) ;
263
+ let mut l2 = event. listen ( ) ;
264
+ let mut l3 = event. listen ( ) ;
265
+
266
+ assert_eq ! ( event. notify( 1 ) , 1 ) ;
267
+ drop ( l1) ;
268
+ assert ! ( is_notified( & mut l3) ) ;
269
+ assert ! ( !is_notified( & mut l2) ) ;
270
+ }
271
+
272
+ #[ test]
273
+ fn notify_all_fair_fifo ( ) {
274
+ notify_all_fair ( QueueStrategy :: Fifo )
275
+ }
276
+
277
+ #[ test]
278
+ fn notify_all_fair_lifo ( ) {
279
+ notify_all_fair ( QueueStrategy :: Lifo )
280
+ }
281
+
282
+ fn notify_all_fair ( queue_strategy : QueueStrategy ) {
283
+ let event = Event :: new_with_queue_strategy ( queue_strategy) ;
151
284
let v = Arc :: new ( Mutex :: new ( vec ! [ ] ) ) ;
152
285
153
286
let mut l1 = event. listen ( ) ;
@@ -178,7 +311,11 @@ fn notify_all_fair() {
178
311
. is_pending( ) ) ;
179
312
180
313
assert_eq ! ( event. notify( usize :: MAX ) , 3 ) ;
181
- assert_eq ! ( & * v. lock( ) . unwrap( ) , & [ 1 , 2 , 3 ] ) ;
314
+
315
+ match queue_strategy {
316
+ QueueStrategy :: Fifo => assert_eq ! ( & * v. lock( ) . unwrap( ) , & [ 1 , 2 , 3 ] ) ,
317
+ QueueStrategy :: Lifo => assert_eq ! ( & * v. lock( ) . unwrap( ) , & [ 3 , 2 , 1 ] ) ,
318
+ }
182
319
183
320
assert ! ( Pin :: new( & mut l1)
184
321
. poll( & mut Context :: from_waker( & waker1) )
0 commit comments