basic_test.go 8.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/libp2p/go-libp2p-testing/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
func getN() int {
	n := 50000
18
	if race.WithRace() {
Łukasz Magiera's avatar
Łukasz Magiera committed
19 20 21 22 23
		n = 1000
	}
	return n
}

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

Jakub Sztandera's avatar
Jakub Sztandera committed
28 29 30 31 32 33 34 35 36 37 38
func TestDefaultSubIsBuffered(t *testing.T) {
	bus := NewBus()
	s, err := bus.Subscribe(new(EventA))
	if err != nil {
		t.Fatal(err)
	}
	if cap(s.(*sub).ch) == 0 {
		t.Fatalf("without any options subscribe should be buffered. was %d", cap(s.(*sub).ch))
	}
}

Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
39
func TestEmit(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
40
	bus := NewBus()
Łukasz Magiera's avatar
Łukasz Magiera committed
41
	sub, err := bus.Subscribe(new(EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
42 43 44
	if err != nil {
		t.Fatal(err)
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
45

Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
46
	go func() {
Łukasz Magiera's avatar
Łukasz Magiera committed
47 48
		defer sub.Close()
		<-sub.Out()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
49 50
	}()

Raúl Kripalani's avatar
Raúl Kripalani committed
51
	em, err := bus.Emitter(new(EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
52 53 54
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
55
	defer em.Close()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
56

Raúl Kripalani's avatar
Raúl Kripalani committed
57
	em.Emit(EventA{})
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
58 59 60 61
}

func TestSub(t *testing.T) {
	bus := NewBus()
Łukasz Magiera's avatar
Łukasz Magiera committed
62
	sub, err := bus.Subscribe(new(EventB))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
63 64 65 66 67 68 69 70 71 72
	if err != nil {
		t.Fatal(err)
	}

	var event EventB

	var wait sync.WaitGroup
	wait.Add(1)

	go func() {
Łukasz Magiera's avatar
Łukasz Magiera committed
73 74
		defer sub.Close()
		event = (<-sub.Out()).(EventB)
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
75 76 77
		wait.Done()
	}()

Raúl Kripalani's avatar
Raúl Kripalani committed
78
	em, err := bus.Emitter(new(EventB))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
79 80 81
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
82
	defer em.Close()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
83

Raúl Kripalani's avatar
Raúl Kripalani committed
84
	em.Emit(EventB(7))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
85 86 87 88 89 90 91 92 93 94
	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
95
	em, err := bus.Emitter(new(EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
96 97 98
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
99
	defer em.Close()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
100

Raúl Kripalani's avatar
Raúl Kripalani committed
101
	em.Emit(EventA{})
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
102 103 104 105 106
}

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

Raúl Kripalani's avatar
Raúl Kripalani committed
107
	em, err := bus.Emitter(new(EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
108 109 110
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
111
	em.Close()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
112 113 114 115 116 117 118 119 120 121 122

	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
123
	em.Emit(EventA{})
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
124 125 126
}

func TestClosingRaces(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
127 128
	subs := getN()
	emits := getN()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
129 130 131 132 133 134 135

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

	wg.Add(subs + emits)

Łukasz Magiera's avatar
Łukasz Magiera committed
136
	b := NewBus()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
137 138 139 140 141 142

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

Łukasz Magiera's avatar
Łukasz Magiera committed
143
			sub, _ := b.Subscribe(new(EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
144
			time.Sleep(10 * time.Millisecond)
Łukasz Magiera's avatar
Łukasz Magiera committed
145
			sub.Close()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
146 147 148 149 150 151 152 153 154

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

Łukasz Magiera's avatar
Łukasz Magiera committed
155
			emit, _ := b.Emitter(new(EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
156
			time.Sleep(10 * time.Millisecond)
Łukasz Magiera's avatar
Łukasz Magiera committed
157
			emit.Close()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
158 159 160 161 162 163 164 165 166

			wg.Done()
		}()
	}

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

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

Raúl Kripalani's avatar
Raúl Kripalani committed
168
	if len(b.(*basicBus).nodes) != 0 {
Łukasz Magiera's avatar
Łukasz Magiera committed
169 170 171 172 173 174 175 176 177
		t.Error("expected no nodes")
	}
}

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

	var r int32

Łukasz Magiera's avatar
Łukasz Magiera committed
178
	n := getN()
Łukasz Magiera's avatar
Łukasz Magiera committed
179 180 181 182 183 184 185
	var wait sync.WaitGroup
	var ready sync.WaitGroup
	wait.Add(n)
	ready.Add(n)

	for i := 0; i < n; i++ {
		go func() {
Łukasz Magiera's avatar
Łukasz Magiera committed
186
			sub, err := bus.Subscribe(new(EventB))
Łukasz Magiera's avatar
Łukasz Magiera committed
187 188 189
			if err != nil {
				panic(err)
			}
Łukasz Magiera's avatar
Łukasz Magiera committed
190
			defer sub.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
191 192

			ready.Done()
Łukasz Magiera's avatar
Łukasz Magiera committed
193
			atomic.AddInt32(&r, int32((<-sub.Out()).(EventB)))
Łukasz Magiera's avatar
Łukasz Magiera committed
194 195 196 197
			wait.Done()
		}()
	}

Raúl Kripalani's avatar
Raúl Kripalani committed
198
	em, err := bus.Emitter(new(EventB))
Łukasz Magiera's avatar
Łukasz Magiera committed
199 200 201
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
202
	defer em.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
203 204 205

	ready.Wait()

Raúl Kripalani's avatar
Raúl Kripalani committed
206
	em.Emit(EventB(7))
Łukasz Magiera's avatar
Łukasz Magiera committed
207 208
	wait.Wait()

Łukasz Magiera's avatar
Gofmt  
Łukasz Magiera committed
209
	if int(r) != 7*n {
Łukasz Magiera's avatar
Łukasz Magiera committed
210 211 212 213
		t.Error("got wrong result")
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
214
func TestSubType(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
215
	bus := NewBus()
Łukasz Magiera's avatar
Łukasz Magiera committed
216
	sub, err := bus.Subscribe([]interface{}{new(EventA), new(EventB)})
Łukasz Magiera's avatar
Łukasz Magiera committed
217 218 219 220 221 222 223 224
	if err != nil {
		t.Fatal(err)
	}

	var event fmt.Stringer

	var wait sync.WaitGroup
	wait.Add(1)
Łukasz Magiera's avatar
Łukasz Magiera committed
225 226

	go func() {
Łukasz Magiera's avatar
Łukasz Magiera committed
227 228
		defer sub.Close()
		event = (<-sub.Out()).(EventA)
Łukasz Magiera's avatar
Łukasz Magiera committed
229
		wait.Done()
Łukasz Magiera's avatar
Łukasz Magiera committed
230 231
	}()

Raúl Kripalani's avatar
Raúl Kripalani committed
232
	em, err := bus.Emitter(new(EventA))
Łukasz Magiera's avatar
Łukasz Magiera committed
233
	if err != nil {
Łukasz Magiera's avatar
Łukasz Magiera committed
234
		t.Fatal(err)
Łukasz Magiera's avatar
Łukasz Magiera committed
235
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
236
	defer em.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
237

Raúl Kripalani's avatar
Raúl Kripalani committed
238
	em.Emit(EventA{})
Łukasz Magiera's avatar
Łukasz Magiera committed
239
	wait.Wait()
Łukasz Magiera's avatar
Łukasz Magiera committed
240

Łukasz Magiera's avatar
Łukasz Magiera committed
241 242
	if event.String() != "Oh, Hello" {
		t.Error("didn't get the correct message")
Łukasz Magiera's avatar
Łukasz Magiera committed
243
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
244
}
Łukasz Magiera's avatar
Łukasz Magiera committed
245

Łukasz Magiera's avatar
Łukasz Magiera committed
246 247
func TestNonStateful(t *testing.T) {
	bus := NewBus()
Raúl Kripalani's avatar
Raúl Kripalani committed
248
	em, err := bus.Emitter(new(EventB))
Łukasz Magiera's avatar
Łukasz Magiera committed
249 250 251
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
252
	defer em.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
253

Łukasz Magiera's avatar
Łukasz Magiera committed
254
	sub1, err := bus.Subscribe(new(EventB), BufSize(1))
Łukasz Magiera's avatar
Łukasz Magiera committed
255 256 257
	if err != nil {
		t.Fatal(err)
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
258
	defer sub1.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
259 260

	select {
Łukasz Magiera's avatar
Łukasz Magiera committed
261
	case <-sub1.Out():
Łukasz Magiera's avatar
Łukasz Magiera committed
262 263 264 265
		t.Fatal("didn't expect to get an event")
	default:
	}

Raúl Kripalani's avatar
Raúl Kripalani committed
266
	em.Emit(EventB(1))
Łukasz Magiera's avatar
Łukasz Magiera committed
267 268

	select {
Łukasz Magiera's avatar
Łukasz Magiera committed
269 270
	case e := <-sub1.Out():
		if e.(EventB) != 1 {
Łukasz Magiera's avatar
Łukasz Magiera committed
271 272 273 274 275 276
			t.Fatal("got wrong event")
		}
	default:
		t.Fatal("expected to get an event")
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
277
	sub2, err := bus.Subscribe(new(EventB), BufSize(1))
Łukasz Magiera's avatar
Łukasz Magiera committed
278 279 280
	if err != nil {
		t.Fatal(err)
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
281
	defer sub2.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
282 283

	select {
Łukasz Magiera's avatar
Łukasz Magiera committed
284
	case <-sub2.Out():
Łukasz Magiera's avatar
Łukasz Magiera committed
285 286 287 288 289 290 291
		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
292
	em, err := bus.Emitter(new(EventB), Stateful)
Łukasz Magiera's avatar
Łukasz Magiera committed
293 294 295
	if err != nil {
		t.Fatal(err)
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
296
	defer em.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
297

Raúl Kripalani's avatar
Raúl Kripalani committed
298
	em.Emit(EventB(2))
Łukasz Magiera's avatar
Łukasz Magiera committed
299

Łukasz Magiera's avatar
Łukasz Magiera committed
300
	sub, err := bus.Subscribe(new(EventB), BufSize(1))
Łukasz Magiera's avatar
Łukasz Magiera committed
301 302 303
	if err != nil {
		t.Fatal(err)
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
304
	defer sub.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
305

Łukasz Magiera's avatar
Łukasz Magiera committed
306
	if (<-sub.Out()).(EventB) != 2 {
Łukasz Magiera's avatar
Łukasz Magiera committed
307 308 309 310
		t.Fatal("got wrong event")
	}
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
func TestCloseBlocking(t *testing.T) {
	bus := NewBus()
	em, err := bus.Emitter(new(EventB))
	if err != nil {
		t.Fatal(err)
	}

	sub, err := bus.Subscribe(new(EventB))
	if err != nil {
		t.Fatal(err)
	}

	go func() {
		em.Emit(EventB(159))
	}()

	time.Sleep(10 * time.Millisecond) // make sure that emit is blocked

	sub.Close() // cancel sub
}

332 333 334 335 336
func panicOnTimeout(d time.Duration) {
	<-time.After(d)
	panic("timeout reached")
}

Łukasz Magiera's avatar
Łukasz Magiera committed
337 338 339 340 341 342 343 344 345 346 347 348
func TestSubFailFully(t *testing.T) {
	bus := NewBus()
	em, err := bus.Emitter(new(EventB))
	if err != nil {
		t.Fatal(err)
	}

	_, err = bus.Subscribe([]interface{}{new(EventB), 5})
	if err == nil || err.Error() != "subscribe called with non-pointer type" {
		t.Fatal(err)
	}

349 350
	go panicOnTimeout(5 * time.Second)

Łukasz Magiera's avatar
Łukasz Magiera committed
351 352 353
	em.Emit(EventB(159)) // will hang if sub doesn't fail properly
}

Łukasz Magiera's avatar
Łukasz Magiera committed
354
func testMany(t testing.TB, subs, emits, msgs int, stateful bool) {
355
	if race.WithRace() && subs+emits > 5000 {
Łukasz Magiera's avatar
Łukasz Magiera committed
356 357 358
		t.SkipNow()
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
359 360 361 362 363 364 365 366 367 368 369
	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() {
Łukasz Magiera's avatar
Łukasz Magiera committed
370
			sub, err := bus.Subscribe(new(EventB))
Łukasz Magiera's avatar
Łukasz Magiera committed
371 372 373
			if err != nil {
				panic(err)
			}
Łukasz Magiera's avatar
Łukasz Magiera committed
374
			defer sub.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
375 376

			ready.Done()
Łukasz Magiera's avatar
Gofmt  
Łukasz Magiera committed
377
			for i := 0; i < emits*msgs; i++ {
Łukasz Magiera's avatar
Łukasz Magiera committed
378 379 380 381 382
				e, ok := <-sub.Out()
				if !ok {
					panic("wat")
				}
				atomic.AddInt64(&r, int64(e.(EventB)))
Łukasz Magiera's avatar
Łukasz Magiera committed
383 384 385 386 387 388 389
			}
			wait.Done()
		}()
	}

	for i := 0; i < emits; i++ {
		go func() {
Raúl Kripalani's avatar
Raúl Kripalani committed
390
			em, err := bus.Emitter(new(EventB), func(settings interface{}) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
391 392
				settings.(*emitterSettings).makeStateful = stateful
				return nil
Łukasz Magiera's avatar
Łukasz Magiera committed
393
			})
Łukasz Magiera's avatar
Łukasz Magiera committed
394 395 396
			if err != nil {
				panic(err)
			}
Raúl Kripalani's avatar
Raúl Kripalani committed
397
			defer em.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
398 399 400 401

			ready.Wait()

			for i := 0; i < msgs; i++ {
Raúl Kripalani's avatar
Raúl Kripalani committed
402
				em.Emit(EventB(97))
Łukasz Magiera's avatar
Łukasz Magiera committed
403 404 405 406 407 408 409 410
			}

			wait.Done()
		}()
	}

	wait.Wait()

Łukasz Magiera's avatar
Gofmt  
Łukasz Magiera committed
411
	if int(r) != 97*subs*emits*msgs {
Łukasz Magiera's avatar
Łukasz Magiera committed
412 413 414 415 416
		t.Fatal("got wrong result")
	}
}

func TestBothMany(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
417
	testMany(t, 10000, 100, 10, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
418 419
}

Jakub Sztandera's avatar
Jakub Sztandera committed
420 421 422 423
type benchCase struct {
	subs     int
	emits    int
	stateful bool
Łukasz Magiera's avatar
Łukasz Magiera committed
424 425
}

Jakub Sztandera's avatar
Jakub Sztandera committed
426
func (bc benchCase) name() string {
Jakub Sztandera's avatar
Jakub Sztandera committed
427
	return fmt.Sprintf("subs-%03d/emits-%03d/stateful-%t", bc.subs, bc.emits, bc.stateful)
Łukasz Magiera's avatar
Łukasz Magiera committed
428 429
}

Jakub Sztandera's avatar
Jakub Sztandera committed
430 431 432
func genTestCases() []benchCase {
	ret := make([]benchCase, 0, 200)
	for stateful := 0; stateful < 2; stateful++ {
Jakub Sztandera's avatar
Jakub Sztandera committed
433 434
		for subs := uint(0); subs <= 8; subs = subs + 4 {
			for emits := uint(0); emits <= 8; emits = emits + 4 {
Jakub Sztandera's avatar
Jakub Sztandera committed
435 436 437 438 439
				ret = append(ret, benchCase{1 << subs, 1 << emits, stateful == 1})
			}
		}
	}
	return ret
Łukasz Magiera's avatar
Łukasz Magiera committed
440 441
}

Jakub Sztandera's avatar
Jakub Sztandera committed
442 443 444 445
func BenchmarkEvents(b *testing.B) {
	for _, bc := range genTestCases() {
		b.Run(bc.name(), benchMany(bc))
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
446 447
}

Jakub Sztandera's avatar
Jakub Sztandera committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
func benchMany(bc benchCase) func(*testing.B) {
	return func(b *testing.B) {
		b.ReportAllocs()
		subs := bc.subs
		emits := bc.emits
		stateful := bc.stateful
		bus := NewBus()
		var wait sync.WaitGroup
		var ready sync.WaitGroup
		wait.Add(subs + emits)
		ready.Add(subs + emits)

		for i := 0; i < subs; i++ {
			go func() {
				sub, err := bus.Subscribe(new(EventB))
				if err != nil {
					panic(err)
				}
				defer sub.Close()

				ready.Done()
				ready.Wait()
				for i := 0; i < (b.N/emits)*emits; i++ {
					_, ok := <-sub.Out()
					if !ok {
						panic("wat")
					}
				}
				wait.Done()
			}()
		}
Łukasz Magiera's avatar
Łukasz Magiera committed
479

Jakub Sztandera's avatar
Jakub Sztandera committed
480 481 482 483 484 485 486 487 488 489
		for i := 0; i < emits; i++ {
			go func() {
				em, err := bus.Emitter(new(EventB), func(settings interface{}) error {
					settings.(*emitterSettings).makeStateful = stateful
					return nil
				})
				if err != nil {
					panic(err)
				}
				defer em.Close()
Łukasz Magiera's avatar
Łukasz Magiera committed
490

Jakub Sztandera's avatar
Jakub Sztandera committed
491 492
				ready.Done()
				ready.Wait()
Łukasz Magiera's avatar
Łukasz Magiera committed
493

Jakub Sztandera's avatar
Jakub Sztandera committed
494 495 496
				for i := 0; i < b.N/emits; i++ {
					em.Emit(EventB(97))
				}
Łukasz Magiera's avatar
Łukasz Magiera committed
497

Jakub Sztandera's avatar
Jakub Sztandera committed
498 499 500 501 502 503 504
				wait.Done()
			}()
		}
		ready.Wait()
		b.ResetTimer()
		wait.Wait()
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
505
}
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538

var div = 100

func BenchmarkSubscribe(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N/div; i++ {
		bus := NewBus()
		for j := 0; j < div; j++ {
			bus.Subscribe(new(EventA))
		}
	}
}

func BenchmarkEmitter(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N/div; i++ {
		bus := NewBus()
		for j := 0; j < div; j++ {
			bus.Emitter(new(EventA))
		}
	}
}

func BenchmarkSubscribeAndEmitter(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N/div; i++ {
		bus := NewBus()
		for j := 0; j < div; j++ {
			bus.Subscribe(new(EventA))
			bus.Emitter(new(EventA))
		}
	}
}