Skip to content

Commit 37fcd00

Browse files
authored
Merge pull request #245 from iwaltgen/merge-operator-each-observable-goroutine
Fix Merge Operator always emit values sequentially
2 parents 8e8d5ba + be630be commit 37fcd00

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

factory.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,9 @@ func Merge(observables []Observable, opts ...Option) Observable {
269269
}
270270
}
271271

272-
go func() {
273-
for _, o := range observables {
274-
f(o)
275-
}
276-
}()
272+
for _, o := range observables {
273+
go f(o)
274+
}
277275

278276
go func() {
279277
wg.Wait()

factory_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,27 @@ func Test_Merge_Error(t *testing.T) {
379379
Assert(context.Background(), t, obs, IsNotEmpty(), HasError(errFoo))
380380
}
381381

382+
func Test_Merge_Interval(t *testing.T) {
383+
var obs []Observable
384+
ctx, cancel := context.WithCancel(context.Background())
385+
obs = append(obs, Interval(WithDuration(3*time.Millisecond), WithContext(ctx)).
386+
Take(3).
387+
Map(func(_ context.Context, v interface{}) (interface{}, error) {
388+
return 10 + v.(int), nil
389+
}))
390+
obs = append(obs, Interval(WithDuration(5*time.Millisecond), WithContext(ctx)).
391+
Take(3).
392+
Map(func(_ context.Context, v interface{}) (interface{}, error) {
393+
return 20 + v.(int), nil
394+
}))
395+
396+
go func() {
397+
time.Sleep(50 * time.Millisecond)
398+
cancel()
399+
}()
400+
Assert(ctx, t, Merge(obs), HasNoError(), HasItemsNoOrder(10, 11, 12, 20, 21, 22))
401+
}
402+
382403
func Test_Range(t *testing.T) {
383404
obs := Range(5, 3)
384405
Assert(context.Background(), t, obs, HasItems(5, 6, 7, 8))

0 commit comments

Comments
 (0)