aboutsummaryrefslogtreecommitdiff
path: root/internal/engine/lifecycle_test.go
blob: 8cea97f34201aa8f33c700f302cacc2890480eaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package engine

import (
	"sync"
	"testing"
)

func TestConcurrentTicksAreSerialized(t *testing.T) {
	e := New()
	count := 0
	e.Subscribe(1, func() bool {
		count++ // safe only when callbacks have a single owner
		return true
	})
	var wg sync.WaitGroup
	for i := 0; i < 8; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for j := 0; j < 100; j++ {
				e.Tick()
			}
		}()
	}
	wg.Wait()
	if count != 800 {
		t.Fatalf("lost ticks: got %d, want 800", count)
	}
}

func TestStopIsIdempotentAndRestartable(t *testing.T) {
	e := New()
	e.Stop()
	defer e.Stop()
	for i := 0; i < 100; i++ {
		e.Start(60000)
		e.Stop()
		e.Stop()
	}
}