Closed dayaftereh closed 2 years ago
You shouldn't pass context on every function, because by default the context is propagate to every children. Instead you should do like this
func run(t *testing.T, run int) bool {
// create a context and cancel it after 100 ms
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(time.Millisecond * 100)
cancel()
}()
// make an observable from a channel
ch := make(chan rxgo.Item)
observable := rxgo.FromChannel(ch, rxgo.WithContext(ctx))
// use first and get to get the first matching item
i, err := observable.
Filter(func(i interface{}) bool {
return false
}).
First().
Get()
/// OR this ------------------->
observable := rxgo.FromChannel(ch)
// use first and get to get the first matching item
i, err := observable.
Filter(func(i interface{}) bool {
return false
}).
First().
Get(rxgo.WithContext(ctx))
// use to keep the item
item = i
// the error should be not nil, because context canceled
return err != nil
}
@si3nloong I tested both versions of your code with github.com/reactivex/rxgo/v2 v2.5.0
, but the issue still exists.
I use
Filter()
withFirst()
andGet()
to get the firstrxgo.Item
, which match my filter function. But when the passed context byrxgo.WithContext
is canceled, theGet()
is sometimes not returning thecontext canceled
error.The following test is running 10 times an context cancel after 100 ms and counts how often an error (
context canceled
) is returned fromGet()
. Running the test multiply times theok
andnot ok
count changes randomly.Expected is that all runs returning an
context canceled
error and thenotOk
count is zero.I think this is an issue with
First()
andGet()
in combination withFilter()
.Tested with:
github.com/reactivex/rxgo/v2 v2.5.0