From 54d56b1df4acdca2eabe306ac8b1665dfe24d9b8 Mon Sep 17 00:00:00 2001 From: "jackdaw@loafle.com" Date: Fri, 14 Apr 2017 15:28:33 +0900 Subject: [PATCH] . --- observer/observer.go | 15 +++++- observer/observer_test.go | 96 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 5 deletions(-) diff --git a/observer/observer.go b/observer/observer.go index 8b556ad..33c7d8c 100644 --- a/observer/observer.go +++ b/observer/observer.go @@ -41,10 +41,14 @@ func (o *observer) Remove(id string, rch chan interface{}) error { newArr = append(newArr, ch) } else { close(ch) + ch = nil } } - - o.events[id] = newArr + if len(newArr) == 0 { + delete(o.events,id) + } else { + o.events[id] = newArr + } return nil } func (o *observer) Notify(id string, arg interface{}) error { @@ -73,6 +77,9 @@ func init() { }) } + +// interface + func Add(id string, ch chan interface{}) error { return _observer.Add(id, ch) } @@ -82,3 +89,7 @@ func Remove(id string, rch chan interface{}) error { func Notify(id string, arg interface{}) error { return _observer.Notify(id, arg) } + +func GetCount() (int) { + return len(_observer.events) +} diff --git a/observer/observer_test.go b/observer/observer_test.go index aecca24..4fdb4da 100644 --- a/observer/observer_test.go +++ b/observer/observer_test.go @@ -1,7 +1,97 @@ package observer -import "testing" +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) -func TestObserver(t *testing.T) { +type Test struct { + Id string +} -} \ No newline at end of file +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) + +}