Newer
Older
Benjamin Bollen
committed
import (
"encoding/hex"
"fmt"
"runtime"
"testing"
"time"
Benjamin Bollen
committed
"github.com/stretchr/testify/assert"
Benjamin Bollen
committed
)
var mockInterval = 10 * time.Millisecond
type mockSub struct {
subId string
eventId string
Benjamin Bollen
committed
shutdown bool
sdChan chan struct{}
}
type mockEventData struct {
subId string
eventId string
}
Benjamin Bollen
committed
// A mock event
func newMockSub(subId, eventId string, f func(evts.EventData)) mockSub {
Benjamin Bollen
committed
return mockSub{subId, eventId, f, false, make(chan struct{})}
}
type mockEventEmitter struct {
subs map[string]mockSub
mutex *sync.Mutex
Benjamin Bollen
committed
}
func newMockEventEmitter() *mockEventEmitter {
return &mockEventEmitter{make(map[string]mockSub), &sync.Mutex{}}
Benjamin Bollen
committed
}
func (this *mockEventEmitter) Subscribe(subId, eventId string, callback func(evts.EventData)) error {
Benjamin Bollen
committed
if _, ok := this.subs[subId]; ok {
Benjamin Bollen
committed
}
me := newMockSub(subId, eventId, callback)
this.mutex.Lock()
this.subs[subId] = me
this.mutex.Unlock()
Benjamin Bollen
committed
go func() {
<-me.sdChan
me.shutdown = true
}()
go func() {
for {
if !me.shutdown {
me.f(mockEventData{subId, eventId})
Benjamin Bollen
committed
} else {
this.mutex.Lock()
delete(this.subs, subId)
this.mutex.Unlock()
Benjamin Bollen
committed
return
}
time.Sleep(mockInterval)
}
}()
Benjamin Bollen
committed
}
func (this *mockEventEmitter) Unsubscribe(subId string) error {
Benjamin Bollen
committed
sub, ok := this.subs[subId]
if !ok {
Benjamin Bollen
committed
}
sub.shutdown = true
delete(this.subs, subId)
Benjamin Bollen
committed
}
// Test that event subscriptions can be added manually and then automatically reaped.
func TestSubReaping(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
NUM_SUBS := 100
reaperThreshold = 200 * time.Millisecond
reaperTimeout = 100 * time.Millisecond
mee := newMockEventEmitter()
eSubs := NewEventSubscriptions(mee)
doneChan := make(chan error)
go func() {
for i := 0; i < NUM_SUBS; i++ {
time.Sleep(2 * time.Millisecond)
go func() {
Benjamin Bollen
committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
if err != nil {
doneChan <- err
return
}
if len(id) != 64 {
doneChan <- fmt.Errorf("Id not of length 64")
return
}
_, err2 := hex.DecodeString(id)
if err2 != nil {
doneChan <- err2
}
doneChan <- nil
}()
}
}()
k := 0
for k < NUM_SUBS {
err := <-doneChan
assert.NoError(t, err)
k++
}
time.Sleep(1100 * time.Millisecond)
assert.Len(t, mee.subs, 0)
assert.Len(t, eSubs.subs, 0)
t.Logf("Added %d subs that were all automatically reaped.", NUM_SUBS)
}
// Test that event subscriptions can be added and removed manually.
func TestSubManualClose(t *testing.T) {
NUM_SUBS := 100
// Keep the reaper out of this.
reaperThreshold = 10000 * time.Millisecond
reaperTimeout = 10000 * time.Millisecond
mee := newMockEventEmitter()
eSubs := NewEventSubscriptions(mee)
doneChan := make(chan error)
go func() {
for i := 0; i < NUM_SUBS; i++ {
time.Sleep(2 * time.Millisecond)
go func() {
Benjamin Bollen
committed
if err != nil {
doneChan <- err
return
}
if len(id) != 64 {
doneChan <- fmt.Errorf("Id not of length 64")
return
}
_, err2 := hex.DecodeString(id)
if err2 != nil {
doneChan <- err2
}
time.Sleep(100 * time.Millisecond)
Benjamin Bollen
committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
if err3 != nil {
doneChan <- err3
}
doneChan <- nil
}()
}
}()
k := 0
for k < NUM_SUBS {
err := <-doneChan
assert.NoError(t, err)
k++
}
assert.Len(t, eSubs.subs, 0)
t.Logf("Added %d subs that were all closed down by unsubscribing.", NUM_SUBS)
}
// Test that the system doesn't fail under high pressure.
func TestSubFlooding(t *testing.T) {
NUM_SUBS := 100
// Keep the reaper out of this.
reaperThreshold = 10000 * time.Millisecond
reaperTimeout = 10000 * time.Millisecond
// Crank it up. Now pressure is 10 times higher on each sub.
mockInterval = 1 * time.Millisecond
mee := newMockEventEmitter()
eSubs := NewEventSubscriptions(mee)
doneChan := make(chan error)
go func() {
for i := 0; i < NUM_SUBS; i++ {
time.Sleep(1 * time.Millisecond)
go func() {
Benjamin Bollen
committed
if err != nil {
doneChan <- err
return
}
if len(id) != 64 {
doneChan <- fmt.Errorf("Id not of length 64")
return
}
_, err2 := hex.DecodeString(id)
if err2 != nil {
doneChan <- err2
}
time.Sleep(1000 * time.Millisecond)
Benjamin Bollen
committed
if err3 != nil {
doneChan <- err3
}
doneChan <- nil
}()
}
}()
k := 0
for k < NUM_SUBS {
err := <-doneChan
assert.NoError(t, err)
k++
}
assert.Len(t, eSubs.subs, 0)
t.Logf("Added %d subs that all received 1000 events each. They were all closed down by unsubscribing.", NUM_SUBS)
}