agent_api/observer/observer_test.go

114 lines
1.8 KiB
Go
Raw Normal View History

2017-04-14 05:28:54 +00:00
package observer
2017-04-14 06:28:33 +00:00
import (
2017-05-15 09:19:37 +00:00
"fmt"
2017-04-14 06:28:33 +00:00
"github.com/stretchr/testify/assert"
2017-05-29 10:41:02 +00:00
"git.loafle.net/overflow/agent_api/messages"
2017-04-14 06:28:33 +00:00
"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"})
}()
2017-05-15 09:19:37 +00:00
ch := make(chan interface{}, 0)
2017-04-14 06:28:33 +00:00
Add("test", ch)
dd := <-ch
bb := dd.(Test)
assert.Equal(t, "test", bb.Id)
}
func TestRemoveObserver(t *testing.T) {
2017-05-15 09:19:37 +00:00
ch := make(chan interface{}, 0)
2017-04-14 06:28:33 +00:00
Add("test", ch)
err := Remove("test", ch)
assert.Equal(t, nil, err)
}
// +build ignore
func TestMultiAddObserver(t *testing.T) {
2017-05-15 09:19:37 +00:00
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)
2017-04-14 06:28:33 +00:00
go func() {
2017-05-15 09:19:37 +00:00
d := <-ch1
2017-04-14 06:28:33 +00:00
dd := d.(Test)
2017-05-15 09:19:37 +00:00
assert.Equal(t, dd.Id, "test")
2017-04-14 06:28:33 +00:00
}()
2017-05-15 09:19:37 +00:00
2017-04-14 06:28:33 +00:00
go func() {
2017-05-15 09:19:37 +00:00
d := <-ch2
2017-04-14 06:28:33 +00:00
dd := d.(Test)
2017-05-15 09:19:37 +00:00
assert.Equal(t, dd.Id, "test")
2017-04-14 06:28:33 +00:00
}()
2017-05-15 09:19:37 +00:00
2017-04-14 06:28:33 +00:00
go func() {
2017-05-15 09:19:37 +00:00
d := <-ch3
2017-04-14 06:28:33 +00:00
dd := d.(Test)
2017-05-15 09:19:37 +00:00
assert.Equal(t, dd.Id, "test")
2017-04-14 06:28:33 +00:00
}()
2017-05-15 09:19:37 +00:00
2017-04-14 06:28:33 +00:00
go func() {
2017-05-15 09:19:37 +00:00
d := <-ch4
2017-04-14 06:28:33 +00:00
dd := d.(Test)
2017-05-15 09:19:37 +00:00
assert.Equal(t, dd.Id, "test")
2017-04-14 06:28:33 +00:00
}()
2017-05-15 09:19:37 +00:00
2017-04-14 06:28:33 +00:00
go func() {
2017-05-15 09:19:37 +00:00
d := <-ch4
2017-04-14 06:28:33 +00:00
dd := d.(Test)
2017-05-15 09:19:37 +00:00
assert.Equal(t, dd.Id, "test")
2017-04-14 06:28:33 +00:00
}()
2017-05-15 09:19:37 +00:00
2017-04-14 06:28:33 +00:00
Add("test", ch1)
Add("test", ch2)
Add("test", ch3)
Add("test", ch4)
Add("test", ch5)
2017-05-15 09:19:37 +00:00
2017-04-14 06:28:33 +00:00
// others package notify call
go func() {
time.Sleep(1 * time.Second)
Notify("test", Test{Id: "test"})
time.Sleep(2 * time.Second)
2017-04-27 05:58:34 +00:00
Remove(messages.CRAWLER_ADD, ch1)
2017-04-14 06:28:33 +00:00
Remove("test", ch2)
Remove("test", ch3)
Remove("test", ch4)
Remove("test", ch5)
}()
2017-05-15 09:19:37 +00:00
2017-04-14 06:28:33 +00:00
time.Sleep(1 * time.Second)
2017-04-27 05:58:34 +00:00
2017-05-15 09:19:37 +00:00
}
2017-04-27 05:58:34 +00:00
func TestStringNotify(t *testing.T) {
2017-05-15 09:19:37 +00:00
ch := make(chan interface{}, 0)
Add("test", ch)
2017-04-27 05:58:34 +00:00
go func() {
2017-05-15 09:19:37 +00:00
data := <-ch
2017-04-27 05:58:34 +00:00
str := data.(string)
fmt.Println(str)
}()
2017-05-15 09:19:37 +00:00
Notify("test", "testsetasetaset")
2017-04-27 05:58:34 +00:00
time.Sleep(1 * time.Second)
}