basic_test.go 6.64 KB
Newer Older
1
package eventbus
Łukasz Magiera's avatar
Łukasz Magiera committed
2 3

import (
Łukasz Magiera's avatar
Łukasz Magiera committed
4
	"fmt"
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
5
	"sync"
Łukasz Magiera's avatar
Łukasz Magiera committed
6
	"sync/atomic"
Łukasz Magiera's avatar
Łukasz Magiera committed
7
	"testing"
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
8
	"time"
Łukasz Magiera's avatar
Łukasz Magiera committed
9 10

	"github.com/jbenet/go-detect-race"
Łukasz Magiera's avatar
Łukasz Magiera committed
11 12 13
)

type EventA struct{}
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
14
type EventB int
Łukasz Magiera's avatar
Łukasz Magiera committed
15

Łukasz Magiera's avatar
Łukasz Magiera committed
16 17 18 19 20 21 22 23
func getN() int {
	n := 50000
	if detectrace.WithRace() {
		n = 1000
	}
	return n
}

Łukasz Magiera's avatar
Łukasz Magiera committed
24 25 26 27
func (EventA) String() string {
	return "Oh, Hello"
}

Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
28
func TestEmit(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
29
	bus := NewBus()
30 31
	events := make(chan EventA)
	cancel, err := bus.Subscribe(events)
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
32 33 34
	if err != nil {
		t.Fatal(err)
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
35

Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
36 37 38 39 40
	go func() {
		defer cancel()
		<-events
	}()

Raúl Kripalani's avatar
Raúl Kripalani committed
41
	em, err := bus.Emitter(new(EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
42 43 44
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
45
	defer em.Close()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
46

Raúl Kripalani's avatar
Raúl Kripalani committed
47
	em.Emit(EventA{})
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
48 49 50 51
}

func TestSub(t *testing.T) {
	bus := NewBus()
52 53
	events := make(chan EventB)
	cancel, err := bus.Subscribe(events)
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
54 55 56 57 58 59 60 61 62 63 64
	if err != nil {
		t.Fatal(err)
	}

	var event EventB

	var wait sync.WaitGroup
	wait.Add(1)

	go func() {
		defer cancel()
65
		event = <-events
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
66 67 68
		wait.Done()
	}()

Raúl Kripalani's avatar
Raúl Kripalani committed
69
	em, err := bus.Emitter(new(EventB))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
70 71 72
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
73
	defer em.Close()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
74

Raúl Kripalani's avatar
Raúl Kripalani committed
75
	em.Emit(EventB(7))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
76 77 78 79 80 81 82 83 84 85
	wait.Wait()

	if event != 7 {
		t.Error("got wrong event")
	}
}

func TestEmitNoSubNoBlock(t *testing.T) {
	bus := NewBus()

Raúl Kripalani's avatar
Raúl Kripalani committed
86
	em, err := bus.Emitter(new(EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
87 88 89
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
90
	defer em.Close()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
91

Raúl Kripalani's avatar
Raúl Kripalani committed
92
	em.Emit(EventA{})
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
93 94 95 96 97
}

func TestEmitOnClosed(t *testing.T) {
	bus := NewBus()

Raúl Kripalani's avatar
Raúl Kripalani committed
98
	em, err := bus.Emitter(new(EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
99 100 101
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
102
	em.Close()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
103 104 105 106 107 108 109 110 111 112 113

	defer func() {
		r := recover()
		if r == nil {
			t.Errorf("expected panic")
		}
		if r.(string) != "emitter is closed" {
			t.Error("unexpected message")
		}
	}()

Raúl Kripalani's avatar
Raúl Kripalani committed
114
	em.Emit(EventA{})
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
115 116 117
}

func TestClosingRaces(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
118 119
	subs := getN()
	emits := getN()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
120 121 122 123 124 125 126

	var wg sync.WaitGroup
	var lk sync.RWMutex
	lk.Lock()

	wg.Add(subs + emits)

Łukasz Magiera's avatar
Łukasz Magiera committed
127
	b := NewBus()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
128 129 130 131 132 133

	for i := 0; i < subs; i++ {
		go func() {
			lk.RLock()
			defer lk.RUnlock()

134
			cancel, _ := b.Subscribe(make(chan EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
135 136 137 138 139 140 141 142 143 144 145
			time.Sleep(10 * time.Millisecond)
			cancel()

			wg.Done()
		}()
	}
	for i := 0; i < emits; i++ {
		go func() {
			lk.RLock()
			defer lk.RUnlock()

Łukasz Magiera's avatar
Łukasz Magiera committed
146
			emit, _ := b.Emitter(new(EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
147
			time.Sleep(10 * time.Millisecond)
Łukasz Magiera's avatar
Łukasz Magiera committed
148
			emit.Close()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
149 150 151 152 153 154 155 156 157

			wg.Done()
		}()
	}

	time.Sleep(10 * time.Millisecond)
	lk.Unlock() // start everything

	wg.Wait()
Łukasz Magiera's avatar
Łukasz Magiera committed
158

Raúl Kripalani's avatar
Raúl Kripalani committed
159
	if len(b.(*basicBus).nodes) != 0 {
Łukasz Magiera's avatar
Łukasz Magiera committed
160 161 162 163 164 165 166 167 168
		t.Error("expected no nodes")
	}
}

func TestSubMany(t *testing.T) {
	bus := NewBus()

	var r int32

Łukasz Magiera's avatar
Łukasz Magiera committed
169
	n := getN()
Łukasz Magiera's avatar
Łukasz Magiera committed
170 171 172 173 174 175 176
	var wait sync.WaitGroup
	var ready sync.WaitGroup
	wait.Add(n)
	ready.Add(n)

	for i := 0; i < n; i++ {
		go func() {
177 178
			events := make(chan EventB)
			cancel, err := bus.Subscribe(events)
Łukasz Magiera's avatar
Łukasz Magiera committed
179 180 181 182 183 184
			if err != nil {
				panic(err)
			}
			defer cancel()

			ready.Done()
185
			atomic.AddInt32(&r, int32(<-events))
Łukasz Magiera's avatar
Łukasz Magiera committed
186 187 188 189
			wait.Done()
		}()
	}

Raúl Kripalani's avatar
Raúl Kripalani committed
190
	em, err := bus.Emitter(new(EventB))
Łukasz Magiera's avatar
Łukasz Magiera committed
191 192 193
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
194
	defer em.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
195 196 197

	ready.Wait()

Raúl Kripalani's avatar
Raúl Kripalani committed
198
	em.Emit(EventB(7))
Łukasz Magiera's avatar
Łukasz Magiera committed
199 200
	wait.Wait()

Łukasz Magiera's avatar
Gofmt  
Łukasz Magiera committed
201
	if int(r) != 7*n {
Łukasz Magiera's avatar
Łukasz Magiera committed
202 203 204 205
		t.Error("got wrong result")
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
206
func TestSubType(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
207
	bus := NewBus()
Łukasz Magiera's avatar
Łukasz Magiera committed
208 209 210 211 212 213 214 215 216 217
	events := make(chan fmt.Stringer)
	cancel, err := bus.Subscribe(events, ForceSubType(new(EventA)))
	if err != nil {
		t.Fatal(err)
	}

	var event fmt.Stringer

	var wait sync.WaitGroup
	wait.Add(1)
Łukasz Magiera's avatar
Łukasz Magiera committed
218 219 220

	go func() {
		defer cancel()
Łukasz Magiera's avatar
Łukasz Magiera committed
221 222
		event = <-events
		wait.Done()
Łukasz Magiera's avatar
Łukasz Magiera committed
223 224
	}()

Raúl Kripalani's avatar
Raúl Kripalani committed
225
	em, err := bus.Emitter(new(EventA))
Łukasz Magiera's avatar
Łukasz Magiera committed
226
	if err != nil {
Łukasz Magiera's avatar
Łukasz Magiera committed
227
		t.Fatal(err)
Łukasz Magiera's avatar
Łukasz Magiera committed
228
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
229
	defer em.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
230

Raúl Kripalani's avatar
Raúl Kripalani committed
231
	em.Emit(EventA{})
Łukasz Magiera's avatar
Łukasz Magiera committed
232
	wait.Wait()
Łukasz Magiera's avatar
Łukasz Magiera committed
233

Łukasz Magiera's avatar
Łukasz Magiera committed
234 235
	if event.String() != "Oh, Hello" {
		t.Error("didn't get the correct message")
Łukasz Magiera's avatar
Łukasz Magiera committed
236
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
237
}
Łukasz Magiera's avatar
Łukasz Magiera committed
238

Łukasz Magiera's avatar
Łukasz Magiera committed
239 240
func TestNonStateful(t *testing.T) {
	bus := NewBus()
Raúl Kripalani's avatar
Raúl Kripalani committed
241
	em, err := bus.Emitter(new(EventB))
Łukasz Magiera's avatar
Łukasz Magiera committed
242 243 244
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
245
	defer em.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259

	eventsA := make(chan EventB, 1)
	cancelS, err := bus.Subscribe(eventsA)
	if err != nil {
		t.Fatal(err)
	}
	defer cancelS()

	select {
	case <-eventsA:
		t.Fatal("didn't expect to get an event")
	default:
	}

Raúl Kripalani's avatar
Raúl Kripalani committed
260
	em.Emit(EventB(1))
Łukasz Magiera's avatar
Łukasz Magiera committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

	select {
	case e := <-eventsA:
		if e != 1 {
			t.Fatal("got wrong event")
		}
	default:
		t.Fatal("expected to get an event")
	}

	eventsB := make(chan EventB, 1)
	cancelS2, err := bus.Subscribe(eventsB)
	if err != nil {
		t.Fatal(err)
	}
	defer cancelS2()

	select {
	case <-eventsA:
		t.Fatal("didn't expect to get an event")
	default:
	}
}

func TestStateful(t *testing.T) {
	bus := NewBus()
Raúl Kripalani's avatar
Raúl Kripalani committed
287
	em, err := bus.Emitter(new(EventB), Stateful)
Łukasz Magiera's avatar
Łukasz Magiera committed
288 289 290
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
291
	defer em.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
292

Raúl Kripalani's avatar
Raúl Kripalani committed
293
	em.Emit(EventB(2))
Łukasz Magiera's avatar
Łukasz Magiera committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307

	eventsA := make(chan EventB, 1)
	cancelS, err := bus.Subscribe(eventsA)
	if err != nil {
		t.Fatal(err)
	}
	defer cancelS()

	if <-eventsA != 2 {
		t.Fatal("got wrong event")
	}
}

func testMany(t testing.TB, subs, emits, msgs int, stateful bool) {
Łukasz Magiera's avatar
Łukasz Magiera committed
308
	if detectrace.WithRace() && subs+emits > 5000 {
Łukasz Magiera's avatar
Łukasz Magiera committed
309 310 311
		t.SkipNow()
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
312 313 314 315 316 317 318 319 320 321 322
	bus := NewBus()

	var r int64

	var wait sync.WaitGroup
	var ready sync.WaitGroup
	wait.Add(subs + emits)
	ready.Add(subs)

	for i := 0; i < subs; i++ {
		go func() {
323 324
			events := make(chan EventB)
			cancel, err := bus.Subscribe(events)
Łukasz Magiera's avatar
Łukasz Magiera committed
325 326 327 328 329 330
			if err != nil {
				panic(err)
			}
			defer cancel()

			ready.Done()
Łukasz Magiera's avatar
Gofmt  
Łukasz Magiera committed
331
			for i := 0; i < emits*msgs; i++ {
332
				atomic.AddInt64(&r, int64(<-events))
Łukasz Magiera's avatar
Łukasz Magiera committed
333 334 335 336 337 338 339
			}
			wait.Done()
		}()
	}

	for i := 0; i < emits; i++ {
		go func() {
Raúl Kripalani's avatar
Raúl Kripalani committed
340
			em, err := bus.Emitter(new(EventB), func(settings interface{}) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
341 342
				settings.(*emitterSettings).makeStateful = stateful
				return nil
Łukasz Magiera's avatar
Łukasz Magiera committed
343
			})
Łukasz Magiera's avatar
Łukasz Magiera committed
344 345 346
			if err != nil {
				panic(err)
			}
Raúl Kripalani's avatar
Raúl Kripalani committed
347
			defer em.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
348 349 350 351

			ready.Wait()

			for i := 0; i < msgs; i++ {
Raúl Kripalani's avatar
Raúl Kripalani committed
352
				em.Emit(EventB(97))
Łukasz Magiera's avatar
Łukasz Magiera committed
353 354 355 356 357 358 359 360
			}

			wait.Done()
		}()
	}

	wait.Wait()

Łukasz Magiera's avatar
Gofmt  
Łukasz Magiera committed
361
	if int(r) != 97*subs*emits*msgs {
Łukasz Magiera's avatar
Łukasz Magiera committed
362 363 364 365 366
		t.Fatal("got wrong result")
	}
}

func TestBothMany(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
367
	testMany(t, 10000, 100, 10, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
368 369 370 371
}

func BenchmarkSubs(b *testing.B) {
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
372
	testMany(b, b.N, 100, 100, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
373 374 375 376
}

func BenchmarkEmits(b *testing.B) {
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
377
	testMany(b, 100, b.N, 100, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
378 379 380 381
}

func BenchmarkMsgs(b *testing.B) {
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
382
	testMany(b, 100, 100, b.N, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
383 384 385 386
}

func BenchmarkOneToMany(b *testing.B) {
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
387
	testMany(b, b.N, 1, 100, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
388 389 390 391
}

func BenchmarkManyToOne(b *testing.B) {
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
392
	testMany(b, 1, b.N, 100, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
393 394 395 396 397
}

func BenchmarkMs1e2m4(b *testing.B) {
	b.N = 1000000
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
398
	testMany(b, 10, 100, 10000, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
399 400 401
}

func BenchmarkMs1e0m6(b *testing.B) {
Łukasz Magiera's avatar
Łukasz Magiera committed
402
	b.N = 10000000
Łukasz Magiera's avatar
Łukasz Magiera committed
403
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
404
	testMany(b, 10, 1, 1000000, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
405 406
}

Łukasz Magiera's avatar
Łukasz Magiera committed
407 408 409
func BenchmarkMs0e0m6(b *testing.B) {
	b.N = 1000000
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
410 411 412 413 414 415 416 417 418 419 420 421 422
	testMany(b, 1, 1, 1000000, false)
}

func BenchmarkStatefulMs1e0m6(b *testing.B) {
	b.N = 10000000
	b.ReportAllocs()
	testMany(b, 10, 1, 1000000, true)
}

func BenchmarkStatefulMs0e0m6(b *testing.B) {
	b.N = 1000000
	b.ReportAllocs()
	testMany(b, 1, 1, 1000000, true)
Łukasz Magiera's avatar
Łukasz Magiera committed
423 424
}

Łukasz Magiera's avatar
Łukasz Magiera committed
425 426 427
func BenchmarkMs0e6m0(b *testing.B) {
	b.N = 1000000
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
428
	testMany(b, 1, 1000000, 1, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
429 430 431 432 433
}

func BenchmarkMs6e0m0(b *testing.B) {
	b.N = 1000000
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
434
	testMany(b, 1000000, 1, 1, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
435
}