@@ -84,76 +84,97 @@ func Test_Concat_OneEmptyObservable(t *testing.T) {
84
84
}
85
85
86
86
func Test_Create (t * testing.T ) {
87
- obs := Create ([]Producer {func (ctx context.Context , next chan <- Item , done func () ) {
87
+ obs := Create ([]Producer {func (ctx context.Context , next chan <- Item ) {
88
88
next <- Of (1 )
89
89
next <- Of (2 )
90
90
next <- Of (3 )
91
- done ()
92
91
}})
93
92
Assert (context .Background (), t , obs , HasItems (1 , 2 , 3 ), HasNoError ())
94
93
}
95
94
96
95
func Test_Create_SingleDup (t * testing.T ) {
97
- obs := Create ([]Producer {func (ctx context.Context , next chan <- Item , done func () ) {
96
+ obs := Create ([]Producer {func (ctx context.Context , next chan <- Item ) {
98
97
next <- Of (1 )
99
98
next <- Of (2 )
100
99
next <- Of (3 )
101
- done ()
102
100
}})
103
101
Assert (context .Background (), t , obs , HasItems (1 , 2 , 3 ), HasNoError ())
104
102
Assert (context .Background (), t , obs , IsEmpty (), HasNoError ())
105
103
}
106
104
105
+ func Test_Create_ContextCancelled (t * testing.T ) {
106
+ closed1 := make (chan struct {})
107
+ ctx , cancel := context .WithCancel (context .Background ())
108
+ Create ([]Producer {
109
+ func (ctx context.Context , next chan <- Item ) {
110
+ cancel ()
111
+ }, func (ctx context.Context , next chan <- Item ) {
112
+ <- ctx .Done ()
113
+ closed1 <- struct {}{}
114
+ },
115
+ }, WithContext (ctx )).Run ()
116
+
117
+ select {
118
+ case <- time .Tick (time .Second ):
119
+ assert .FailNow (t , "producer not closed" )
120
+ case <- closed1 :
121
+ }
122
+ }
123
+
107
124
func Test_Defer (t * testing.T ) {
108
- obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item , done func () ) {
125
+ obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item ) {
109
126
next <- Of (1 )
110
127
next <- Of (2 )
111
128
next <- Of (3 )
112
- done ()
113
129
}})
114
130
Assert (context .Background (), t , obs , HasItems (1 , 2 , 3 ), HasNoError ())
115
131
}
116
132
117
133
func Test_Defer_Multiple (t * testing.T ) {
118
- obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item , done func () ) {
134
+ obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item ) {
119
135
next <- Of (1 )
120
136
next <- Of (2 )
121
- done ()
122
- }, func (ctx context.Context , next chan <- Item , done func ()) {
137
+ }, func (ctx context.Context , next chan <- Item ) {
123
138
next <- Of (10 )
124
139
next <- Of (20 )
125
- done ()
126
140
}})
127
141
Assert (context .Background (), t , obs , HasItemsNoOrder (1 , 2 , 10 , 20 ), HasNoError ())
128
142
}
129
143
130
- func Test_Defer_Close (t * testing.T ) {
131
- obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item , done func ()) {
132
- next <- Of (1 )
133
- next <- Of (2 )
134
- next <- Of (3 )
135
- done ()
136
- }})
137
- Assert (context .Background (), t , obs , HasItems (1 , 2 , 3 ), HasNoError ())
144
+ func Test_Defer_ContextCancelled (t * testing.T ) {
145
+ closed1 := make (chan struct {})
146
+ ctx , cancel := context .WithCancel (context .Background ())
147
+ Defer ([]Producer {
148
+ func (ctx context.Context , next chan <- Item ) {
149
+ cancel ()
150
+ }, func (ctx context.Context , next chan <- Item ) {
151
+ <- ctx .Done ()
152
+ closed1 <- struct {}{}
153
+ },
154
+ }, WithContext (ctx )).Run ()
155
+
156
+ select {
157
+ case <- time .Tick (time .Second ):
158
+ assert .FailNow (t , "producer not closed" )
159
+ case <- closed1 :
160
+ }
138
161
}
139
162
140
163
func Test_Defer_SingleDup (t * testing.T ) {
141
- obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item , done func () ) {
164
+ obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item ) {
142
165
next <- Of (1 )
143
166
next <- Of (2 )
144
167
next <- Of (3 )
145
- done ()
146
168
}})
147
169
Assert (context .Background (), t , obs , HasItems (1 , 2 , 3 ), HasNoError ())
148
170
Assert (context .Background (), t , obs , HasItems (1 , 2 , 3 ), HasNoError ())
149
171
}
150
172
151
173
func Test_Defer_ComposedDup (t * testing.T ) {
152
- obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item , done func () ) {
174
+ obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item ) {
153
175
next <- Of (1 )
154
176
next <- Of (2 )
155
177
next <- Of (3 )
156
- done ()
157
178
}}).Map (func (_ context.Context , i interface {}) (_ interface {}, _ error ) {
158
179
return i .(int ) + 1 , nil
159
180
}).Map (func (_ context.Context , i interface {}) (_ interface {}, _ error ) {
@@ -164,11 +185,10 @@ func Test_Defer_ComposedDup(t *testing.T) {
164
185
}
165
186
166
187
func Test_Defer_ComposedDup_EagerObservation (t * testing.T ) {
167
- obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item , done func () ) {
188
+ obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item ) {
168
189
next <- Of (1 )
169
190
next <- Of (2 )
170
191
next <- Of (3 )
171
- done ()
172
192
}}).Map (func (_ context.Context , i interface {}) (_ interface {}, _ error ) {
173
193
return i .(int ) + 1 , nil
174
194
}, WithObservationStrategy (Eager )).Map (func (_ context.Context , i interface {}) (_ interface {}, _ error ) {
@@ -181,11 +201,10 @@ func Test_Defer_ComposedDup_EagerObservation(t *testing.T) {
181
201
}
182
202
183
203
func Test_Defer_Error (t * testing.T ) {
184
- obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item , done func () ) {
204
+ obs := Defer ([]Producer {func (ctx context.Context , next chan <- Item ) {
185
205
next <- Of (1 )
186
206
next <- Of (2 )
187
207
next <- Error (errFoo )
188
- done ()
189
208
}})
190
209
Assert (context .Background (), t , obs , HasItems (1 , 2 ), HasError (errFoo ))
191
210
}
@@ -366,7 +385,11 @@ func Test_Thrown(t *testing.T) {
366
385
367
386
func Test_Timer (t * testing.T ) {
368
387
obs := Timer (WithDuration (time .Nanosecond ))
369
- Assert (context .Background (), t , obs , IsNotEmpty ())
388
+ select {
389
+ case <- time .Tick (time .Second ):
390
+ assert .FailNow (t , "observable not closed" )
391
+ case <- obs .Observe ():
392
+ }
370
393
}
371
394
372
395
func Test_Timer_Empty (t * testing.T ) {
@@ -376,5 +399,9 @@ func Test_Timer_Empty(t *testing.T) {
376
399
time .Sleep (50 * time .Millisecond )
377
400
cancel ()
378
401
}()
379
- Assert (context .Background (), t , obs , IsEmpty ())
402
+ select {
403
+ case <- time .Tick (time .Second ):
404
+ assert .FailNow (t , "observable not closed" )
405
+ case <- obs .Observe ():
406
+ }
380
407
}
0 commit comments