@@ -84,76 +84,97 @@ func Test_Concat_OneEmptyObservable(t *testing.T) {
8484}
8585
8686func 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 ) {
8888 next <- Of (1 )
8989 next <- Of (2 )
9090 next <- Of (3 )
91- done ()
9291 }})
9392 Assert (context .Background (), t , obs , HasItems (1 , 2 , 3 ), HasNoError ())
9493}
9594
9695func 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 ) {
9897 next <- Of (1 )
9998 next <- Of (2 )
10099 next <- Of (3 )
101- done ()
102100 }})
103101 Assert (context .Background (), t , obs , HasItems (1 , 2 , 3 ), HasNoError ())
104102 Assert (context .Background (), t , obs , IsEmpty (), HasNoError ())
105103}
106104
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+
107124func 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 ) {
109126 next <- Of (1 )
110127 next <- Of (2 )
111128 next <- Of (3 )
112- done ()
113129 }})
114130 Assert (context .Background (), t , obs , HasItems (1 , 2 , 3 ), HasNoError ())
115131}
116132
117133func 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 ) {
119135 next <- Of (1 )
120136 next <- Of (2 )
121- done ()
122- }, func (ctx context.Context , next chan <- Item , done func ()) {
137+ }, func (ctx context.Context , next chan <- Item ) {
123138 next <- Of (10 )
124139 next <- Of (20 )
125- done ()
126140 }})
127141 Assert (context .Background (), t , obs , HasItemsNoOrder (1 , 2 , 10 , 20 ), HasNoError ())
128142}
129143
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+ }
138161}
139162
140163func 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 ) {
142165 next <- Of (1 )
143166 next <- Of (2 )
144167 next <- Of (3 )
145- done ()
146168 }})
147169 Assert (context .Background (), t , obs , HasItems (1 , 2 , 3 ), HasNoError ())
148170 Assert (context .Background (), t , obs , HasItems (1 , 2 , 3 ), HasNoError ())
149171}
150172
151173func 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 ) {
153175 next <- Of (1 )
154176 next <- Of (2 )
155177 next <- Of (3 )
156- done ()
157178 }}).Map (func (_ context.Context , i interface {}) (_ interface {}, _ error ) {
158179 return i .(int ) + 1 , nil
159180 }).Map (func (_ context.Context , i interface {}) (_ interface {}, _ error ) {
@@ -164,11 +185,10 @@ func Test_Defer_ComposedDup(t *testing.T) {
164185}
165186
166187func 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 ) {
168189 next <- Of (1 )
169190 next <- Of (2 )
170191 next <- Of (3 )
171- done ()
172192 }}).Map (func (_ context.Context , i interface {}) (_ interface {}, _ error ) {
173193 return i .(int ) + 1 , nil
174194 }, WithObservationStrategy (Eager )).Map (func (_ context.Context , i interface {}) (_ interface {}, _ error ) {
@@ -181,11 +201,10 @@ func Test_Defer_ComposedDup_EagerObservation(t *testing.T) {
181201}
182202
183203func 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 ) {
185205 next <- Of (1 )
186206 next <- Of (2 )
187207 next <- Error (errFoo )
188- done ()
189208 }})
190209 Assert (context .Background (), t , obs , HasItems (1 , 2 ), HasError (errFoo ))
191210}
0 commit comments