2018-06-10 14:50:03 +00:00
|
|
|
package observable
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2020-07-18 12:56:13 +00:00
|
|
|
"sync/atomic"
|
2018-06-10 14:50:03 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2019-10-30 07:43:55 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-06-10 14:50:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func iterator(item []interface{}) chan interface{} {
|
|
|
|
ch := make(chan interface{})
|
|
|
|
go func() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
for _, elm := range item {
|
|
|
|
ch <- elm
|
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObservable(t *testing.T) {
|
|
|
|
iter := iterator([]interface{}{1, 2, 3, 4, 5})
|
|
|
|
src := NewObservable(iter)
|
|
|
|
data, err := src.Subscribe()
|
2019-10-30 07:43:55 +00:00
|
|
|
assert.Nil(t, err)
|
2018-06-10 14:50:03 +00:00
|
|
|
count := 0
|
2018-07-12 10:03:02 +00:00
|
|
|
for range data {
|
2019-10-14 09:13:23 +00:00
|
|
|
count++
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2019-10-30 07:43:55 +00:00
|
|
|
assert.Equal(t, count, 5)
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestObservable_MutilSubscribe(t *testing.T) {
|
|
|
|
iter := iterator([]interface{}{1, 2, 3, 4, 5})
|
|
|
|
src := NewObservable(iter)
|
|
|
|
ch1, _ := src.Subscribe()
|
|
|
|
ch2, _ := src.Subscribe()
|
2020-07-18 12:56:13 +00:00
|
|
|
var count int32
|
2018-06-10 14:50:03 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
waitCh := func(ch <-chan interface{}) {
|
2018-07-12 10:03:02 +00:00
|
|
|
for range ch {
|
2020-07-18 12:56:13 +00:00
|
|
|
atomic.AddInt32(&count, 1)
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
go waitCh(ch1)
|
|
|
|
go waitCh(ch2)
|
|
|
|
wg.Wait()
|
2020-07-18 12:56:13 +00:00
|
|
|
assert.Equal(t, int32(10), count)
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestObservable_UnSubscribe(t *testing.T) {
|
|
|
|
iter := iterator([]interface{}{1, 2, 3, 4, 5})
|
|
|
|
src := NewObservable(iter)
|
|
|
|
data, err := src.Subscribe()
|
2019-10-30 07:43:55 +00:00
|
|
|
assert.Nil(t, err)
|
2018-06-10 14:50:03 +00:00
|
|
|
src.UnSubscribe(data)
|
|
|
|
_, open := <-data
|
2019-10-30 07:43:55 +00:00
|
|
|
assert.False(t, open)
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 10:03:02 +00:00
|
|
|
func TestObservable_SubscribeClosedSource(t *testing.T) {
|
|
|
|
iter := iterator([]interface{}{1})
|
|
|
|
src := NewObservable(iter)
|
|
|
|
data, _ := src.Subscribe()
|
|
|
|
<-data
|
|
|
|
|
|
|
|
_, closed := src.Subscribe()
|
2019-10-30 07:43:55 +00:00
|
|
|
assert.NotNil(t, closed)
|
2018-07-12 10:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestObservable_UnSubscribeWithNotExistSubscription(t *testing.T) {
|
|
|
|
sub := Subscription(make(chan interface{}))
|
|
|
|
iter := iterator([]interface{}{1})
|
|
|
|
src := NewObservable(iter)
|
|
|
|
src.UnSubscribe(sub)
|
|
|
|
}
|
|
|
|
|
2018-06-10 14:50:03 +00:00
|
|
|
func TestObservable_SubscribeGoroutineLeak(t *testing.T) {
|
|
|
|
iter := iterator([]interface{}{1, 2, 3, 4, 5})
|
|
|
|
src := NewObservable(iter)
|
|
|
|
max := 100
|
|
|
|
|
|
|
|
var list []Subscription
|
|
|
|
for i := 0; i < max; i++ {
|
|
|
|
ch, _ := src.Subscribe()
|
|
|
|
list = append(list, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(max)
|
|
|
|
waitCh := func(ch <-chan interface{}) {
|
2018-07-12 10:03:02 +00:00
|
|
|
for range ch {
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ch := range list {
|
|
|
|
go waitCh(ch)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2020-07-18 12:56:13 +00:00
|
|
|
|
|
|
|
for _, sub := range list {
|
|
|
|
_, more := <-sub
|
|
|
|
assert.False(t, more)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, more := <-list[0]
|
|
|
|
assert.False(t, more)
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|