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

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 11
)

type EventA struct{}
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
12
type EventB int
Łukasz Magiera's avatar
Łukasz Magiera committed
13

Łukasz Magiera's avatar
Łukasz Magiera committed
14 15 16 17
func (EventA) String() string {
	return "Oh, Hello"
}

Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
18
func TestEmit(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
19
	bus := NewBus()
20 21
	events := make(chan EventA)
	cancel, err := bus.Subscribe(events)
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
22 23 24
	if err != nil {
		t.Fatal(err)
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
25

Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
	go func() {
		defer cancel()
		<-events
	}()

	emit, cancel, err := bus.Emitter(new(EventA))
	if err != nil {
		t.Fatal(err)
	}
	defer cancel()

	emit(EventA{})
}

func TestSub(t *testing.T) {
	bus := NewBus()
42 43
	events := make(chan EventB)
	cancel, err := bus.Subscribe(events)
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
44 45 46 47 48 49 50 51 52 53 54
	if err != nil {
		t.Fatal(err)
	}

	var event EventB

	var wait sync.WaitGroup
	wait.Add(1)

	go func() {
		defer cancel()
55
		event = <-events
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
		wait.Done()
	}()

	emit, cancel, err := bus.Emitter(new(EventB))
	if err != nil {
		t.Fatal(err)
	}
	defer cancel()

	emit(EventB(7))
	wait.Wait()

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

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

	emit, cancel, err := bus.Emitter(new(EventA))
	if err != nil {
		t.Fatal(err)
	}
	defer cancel()

	emit(EventA{})
}

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

	emit, cancel, err := bus.Emitter(new(EventA))
	if err != nil {
		t.Fatal(err)
	}
	cancel()

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

	emit(EventA{})
}

func TestClosingRaces(t *testing.T) {
	subs := 50000
	emits := 50000

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

	wg.Add(subs + emits)

Łukasz Magiera's avatar
Łukasz Magiera committed
117
	b := NewBus()
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
118 119 120 121 122 123

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

124
			cancel, _ := b.Subscribe(make(chan EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
125 126 127 128 129 130 131 132 133 134 135
			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
136
			_, cancel, _ := b.Emitter(new(EventA))
Łukasz Magiera's avatar
MVP  
Łukasz Magiera committed
137 138 139 140 141 142 143 144 145 146 147
			time.Sleep(10 * time.Millisecond)
			cancel()

			wg.Done()
		}()
	}

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

	wg.Wait()
Łukasz Magiera's avatar
Łukasz Magiera committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

	if len(b.(*bus).nodes) != 0 {
		t.Error("expected no nodes")
	}
}

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

	var r int32

	n := 50000
	var wait sync.WaitGroup
	var ready sync.WaitGroup
	wait.Add(n)
	ready.Add(n)

	for i := 0; i < n; i++ {
		go func() {
167 168
			events := make(chan EventB)
			cancel, err := bus.Subscribe(events)
Łukasz Magiera's avatar
Łukasz Magiera committed
169 170 171 172 173 174
			if err != nil {
				panic(err)
			}
			defer cancel()

			ready.Done()
175
			atomic.AddInt32(&r, int32(<-events))
Łukasz Magiera's avatar
Łukasz Magiera committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
			wait.Done()
		}()
	}

	emit, cancel, err := bus.Emitter(new(EventB))
	if err != nil {
		t.Fatal(err)
	}
	defer cancel()

	ready.Wait()

	emit(EventB(7))
	wait.Wait()

Łukasz Magiera's avatar
Gofmt  
Łukasz Magiera committed
191
	if int(r) != 7*n {
Łukasz Magiera's avatar
Łukasz Magiera committed
192 193 194 195
		t.Error("got wrong result")
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
196
func TestSubType(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
197
	bus := NewBus()
Łukasz Magiera's avatar
Łukasz Magiera committed
198 199 200 201 202 203 204 205 206 207
	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
208 209 210

	go func() {
		defer cancel()
Łukasz Magiera's avatar
Łukasz Magiera committed
211 212
		event = <-events
		wait.Done()
Łukasz Magiera's avatar
Łukasz Magiera committed
213 214
	}()

Łukasz Magiera's avatar
Łukasz Magiera committed
215
	emit, cancel, err := bus.Emitter(new(EventA))
Łukasz Magiera's avatar
Łukasz Magiera committed
216
	if err != nil {
Łukasz Magiera's avatar
Łukasz Magiera committed
217
		t.Fatal(err)
Łukasz Magiera's avatar
Łukasz Magiera committed
218 219 220
	}
	defer cancel()

Łukasz Magiera's avatar
Łukasz Magiera committed
221 222
	emit(EventA{})
	wait.Wait()
Łukasz Magiera's avatar
Łukasz Magiera committed
223

Łukasz Magiera's avatar
Łukasz Magiera committed
224 225
	if event.String() != "Oh, Hello" {
		t.Error("didn't get the correct message")
Łukasz Magiera's avatar
Łukasz Magiera committed
226
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
227
}
Łukasz Magiera's avatar
Łukasz Magiera committed
228

Łukasz Magiera's avatar
Łukasz Magiera committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 287 288 289 290 291 292 293 294 295 296 297
func TestNonStateful(t *testing.T) {
	bus := NewBus()
	emit, cancelE1, err := bus.Emitter(new(EventB))
	if err != nil {
		t.Fatal(err)
	}
	defer cancelE1()

	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:
	}

	emit(EventB(1))

	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()
	emit, cancelE1, err := bus.Emitter(new(EventB), Stateful)
	if err != nil {
		t.Fatal(err)
	}
	defer cancelE1()

	emit(EventB(2))

	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
298 299 300 301 302 303 304 305 306 307 308
	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() {
309 310
			events := make(chan EventB)
			cancel, err := bus.Subscribe(events)
Łukasz Magiera's avatar
Łukasz Magiera committed
311 312 313 314 315 316
			if err != nil {
				panic(err)
			}
			defer cancel()

			ready.Done()
Łukasz Magiera's avatar
Gofmt  
Łukasz Magiera committed
317
			for i := 0; i < emits*msgs; i++ {
318
				atomic.AddInt64(&r, int64(<-events))
Łukasz Magiera's avatar
Łukasz Magiera committed
319 320 321 322 323 324 325
			}
			wait.Done()
		}()
	}

	for i := 0; i < emits; i++ {
		go func() {
Łukasz Magiera's avatar
Łukasz Magiera committed
326 327 328
			emit, cancel, err := bus.Emitter(new(EventB), func(settings *EmitterSettings) {
				settings.makeStateful = stateful
			})
Łukasz Magiera's avatar
Łukasz Magiera committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
			if err != nil {
				panic(err)
			}
			defer cancel()

			ready.Wait()

			for i := 0; i < msgs; i++ {
				emit(EventB(97))
			}

			wait.Done()
		}()
	}

	wait.Wait()

Łukasz Magiera's avatar
Gofmt  
Łukasz Magiera committed
346
	if int(r) != 97*subs*emits*msgs {
Łukasz Magiera's avatar
Łukasz Magiera committed
347 348 349 350 351
		t.Fatal("got wrong result")
	}
}

func TestBothMany(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
352
	testMany(t, 10000, 100, 10, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
353 354 355 356
}

func BenchmarkSubs(b *testing.B) {
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
357
	testMany(b, b.N, 100, 100, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
358 359 360 361
}

func BenchmarkEmits(b *testing.B) {
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
362
	testMany(b, 100, b.N, 100, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
363 364 365 366
}

func BenchmarkMsgs(b *testing.B) {
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
367
	testMany(b, 100, 100, b.N, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
368 369 370 371
}

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

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

func BenchmarkMs1e2m4(b *testing.B) {
	b.N = 1000000
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
383
	testMany(b, 10, 100, 10000, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
384 385 386
}

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

Łukasz Magiera's avatar
Łukasz Magiera committed
392 393 394
func BenchmarkMs0e0m6(b *testing.B) {
	b.N = 1000000
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
395 396 397 398 399 400 401 402 403 404 405 406 407
	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
408 409
}

Łukasz Magiera's avatar
Łukasz Magiera committed
410 411 412
func BenchmarkMs0e6m0(b *testing.B) {
	b.N = 1000000
	b.ReportAllocs()
Łukasz Magiera's avatar
Łukasz Magiera committed
413
	testMany(b, 1, 1000000, 1, false)
Łukasz Magiera's avatar
Łukasz Magiera committed
414 415 416 417 418
}

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