Skip to content

Commit 9e0889c

Browse files
committed
fix: observable test
1 parent 55cbbf7 commit 9e0889c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

common/observable/observable.go

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Observable[T any] struct {
1010
listener map[Subscription[T]]*Subscriber[T]
1111
mux sync.Mutex
1212
done bool
13+
stopCh chan struct{}
1314
}
1415

1516
func (o *Observable[T]) process() {
@@ -31,6 +32,7 @@ func (o *Observable[T]) close() {
3132
for _, sub := range o.listener {
3233
sub.Close()
3334
}
35+
close(o.stopCh)
3436
}
3537

3638
func (o *Observable[T]) Subscribe() (Subscription[T], error) {
@@ -59,6 +61,7 @@ func NewObservable[T any](iter Iterable[T]) *Observable[T] {
5961
observable := &Observable[T]{
6062
iterable: iter,
6163
listener: map[Subscription[T]]*Subscriber[T]{},
64+
stopCh: make(chan struct{}),
6265
}
6366
go observable.process()
6467
return observable

common/observable/observable_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ func TestObservable_SubscribeClosedSource(t *testing.T) {
7070
src := NewObservable[int](iter)
7171
data, _ := src.Subscribe()
7272
<-data
73-
74-
_, closed := src.Subscribe()
75-
assert.NotNil(t, closed)
73+
select {
74+
case <-src.stopCh:
75+
case <-time.After(time.Second):
76+
assert.Fail(t, "timeout not stop")
77+
}
7678
}
7779

7880
func TestObservable_UnSubscribeWithNotExistSubscription(t *testing.T) {

0 commit comments

Comments
 (0)