2017-04-14 05:28:54 +00:00
|
|
|
package observer
|
|
|
|
|
2017-04-14 06:28:33 +00:00
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
2017-04-14 05:28:54 +00:00
|
|
|
|
2017-04-14 06:28:33 +00:00
|
|
|
type Test struct {
|
|
|
|
Id string
|
|
|
|
}
|
2017-04-14 05:28:54 +00:00
|
|
|
|
2017-04-14 06:28:33 +00:00
|
|
|
func TestAddNotifyObserver(t *testing.T) {
|
|
|
|
|
|
|
|
// others package notify call
|
|
|
|
go func() {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
Notify("test", Test{Id: "test"})
|
|
|
|
}()
|
|
|
|
|
|
|
|
ch := make(chan interface{},0)
|
|
|
|
Add("test", ch)
|
|
|
|
|
|
|
|
dd := <-ch
|
|
|
|
bb := dd.(Test)
|
|
|
|
assert.Equal(t, "test", bb.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveObserver(t *testing.T) {
|
|
|
|
|
|
|
|
ch := make(chan interface{},0)
|
|
|
|
Add("test", ch)
|
|
|
|
|
|
|
|
err := Remove("test", ch)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// +build ignore
|
|
|
|
func TestMultiAddObserver(t *testing.T) {
|
|
|
|
ch1 := make(chan interface{},0)
|
|
|
|
ch2 := make(chan interface{},0)
|
|
|
|
ch3 := make(chan interface{},0)
|
|
|
|
ch4 := make(chan interface{},0)
|
|
|
|
ch5 := make(chan interface{},0)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
d:=<-ch1
|
|
|
|
dd := d.(Test)
|
|
|
|
assert.Equal(t, dd.Id,"test")
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
d:=<-ch2
|
|
|
|
dd := d.(Test)
|
|
|
|
assert.Equal(t, dd.Id,"test")
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
d:=<-ch3
|
|
|
|
dd := d.(Test)
|
|
|
|
assert.Equal(t, dd.Id,"test")
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
d:=<-ch4
|
|
|
|
dd := d.(Test)
|
|
|
|
assert.Equal(t, dd.Id,"test")
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
d:=<-ch4
|
|
|
|
dd := d.(Test)
|
|
|
|
assert.Equal(t, dd.Id,"test")
|
|
|
|
}()
|
|
|
|
|
|
|
|
Add("test", ch1)
|
|
|
|
Add("test", ch2)
|
|
|
|
Add("test", ch3)
|
|
|
|
Add("test", ch4)
|
|
|
|
Add("test", ch5)
|
|
|
|
|
|
|
|
// others package notify call
|
|
|
|
go func() {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
Notify("test", Test{Id: "test"})
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
Remove("test", ch1)
|
|
|
|
Remove("test", ch2)
|
|
|
|
Remove("test", ch3)
|
|
|
|
Remove("test", ch4)
|
|
|
|
Remove("test", ch5)
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
}
|