aboutsummaryrefslogtreecommitdiff
path: root/internal/engine/lifecycle_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/engine/lifecycle_test.go')
-rw-r--r--internal/engine/lifecycle_test.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/internal/engine/lifecycle_test.go b/internal/engine/lifecycle_test.go
new file mode 100644
index 0000000..8cea97f
--- /dev/null
+++ b/internal/engine/lifecycle_test.go
@@ -0,0 +1,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()
+ }
+}