diff options
author | Felix Lange <fjl@twurst.com> | 2014-10-08 22:20:44 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2014-10-08 22:31:08 +0800 |
commit | d4512699775497abd5392aa4c617350491021630 (patch) | |
tree | 0f60025412e03db78a198e3680276d2b502d5573 | |
parent | 7c9508ed7160786b6bd87d59d898213bfbdeced5 (diff) | |
download | go-tangerine-d4512699775497abd5392aa4c617350491021630.tar go-tangerine-d4512699775497abd5392aa4c617350491021630.tar.gz go-tangerine-d4512699775497abd5392aa4c617350491021630.tar.bz2 go-tangerine-d4512699775497abd5392aa4c617350491021630.tar.lz go-tangerine-d4512699775497abd5392aa4c617350491021630.tar.xz go-tangerine-d4512699775497abd5392aa4c617350491021630.tar.zst go-tangerine-d4512699775497abd5392aa4c617350491021630.zip |
eventer: add test for concurrent Post/Register
This test reports the race condition when run
using "go test -race".
-rw-r--r-- | eventer/eventer_test.go | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/eventer/eventer_test.go b/eventer/eventer_test.go index 6891622e3..a5db6d901 100644 --- a/eventer/eventer_test.go +++ b/eventer/eventer_test.go @@ -1,6 +1,10 @@ package eventer -import "testing" +import ( + "math/rand" + "testing" + "time" +) func TestChannel(t *testing.T) { eventer := New() @@ -64,3 +68,46 @@ func TestOn(t *testing.T) { t.Error("Expected function event with data 'hello world'. Got", data) } } + +func TestConcurrentUsage(t *testing.T) { + rand.Seed(time.Now().Unix()) + eventer := New() + stop := make(chan struct{}) + recv := make(chan int) + poster := func() { + for { + select { + case <-stop: + return + default: + eventer.Post("test", "hi") + } + } + } + listener := func(i int) { + time.Sleep(time.Duration(rand.Intn(99)) * time.Millisecond) + c := eventer.Register("test") + // wait for the first event + <-c + recv <- i + // keep receiving to prevent deadlock + for { + select { + case <-stop: + return + case <-c: + } + } + } + + nlisteners := 200 + go poster() + for i := 0; i < nlisteners; i++ { + go listener(i) + } + // wait until everyone has been served + for i := 0; i < nlisteners; i++ { + <-recv + } + close(stop) +} |