-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathruntime_message_context_test.go
More file actions
82 lines (74 loc) · 2.12 KB
/
Copy pathruntime_message_context_test.go
File metadata and controls
82 lines (74 loc) · 2.12 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
package spx
import (
"testing"
"time"
"github.com/goplus/spx/v3/internal/coroutine"
)
func TestIndependentBroadcastTreeDoesNotInheritReceiverTurn(t *testing.T) {
for _, fromScript := range []bool{false, true} {
name := "external"
if fromScript {
name = "script"
}
t.Run(name, func(t *testing.T) {
co, game := setupRuntimeEventGame(t)
calls := 0
game.OnMsg__1("cycle", func() {
calls++
if calls == 1 {
game.Broadcast__0("cycle")
}
})
game.Broadcast__0("cycle")
co.Update()
if calls != 1 {
t.Fatalf("nested broadcast calls = %d, want repeated receiver deferred", calls)
}
// A new root restarts the deferred receiver without advancing the
// frame or sharing the first tree's claimed receiver turn.
if fromScript {
threads := co.StartBatch([]coroutine.Task{{
Owner: game,
Run: func(coroutine.Thread) { game.Broadcast__0("cycle") },
}}, coroutine.BatchAsync)
co.JoinAll(threads)
} else {
game.Broadcast__0("cycle")
}
co.Update()
if calls != 2 {
t.Fatalf("independent broadcast calls = %d, want new root to run in the same frame", calls)
}
})
}
}
func TestMessageExecutionContextReleasedOnCancelAndReset(t *testing.T) {
co, game := setupRuntimeEventGame(t)
gate := co.NewLatch()
started, cleaned := 0, 0
for range 3 {
game.OnMsg__1("held", func() {
started++
defer func() { cleaned++ }()
gate.Wait()
})
}
game.Broadcast__0("held")
co.Update()
if started != 3 {
t.Fatalf("started receivers = %d, want 3", started)
}
if !co.StopAllAndWait(time.Second) {
t.Fatal("canceled receivers did not finish cleanup")
}
if cleaned != 3 || len(game.scriptEvents.messageExecutions) != 0 {
t.Fatalf("after cancellation: cleaned = %d, retained contexts = %d", cleaned, len(game.scriptEvents.messageExecutions))
}
game.scriptEvents.manager.Reset()
calls := 0
game.OnMsg__1("held", func() { calls++ })
game.BroadcastAndWait__0("held")
if calls != 1 || len(game.scriptEvents.messageExecutions) != 0 {
t.Fatalf("after reset and completion: calls = %d, retained contexts = %d", calls, len(game.scriptEvents.messageExecutions))
}
}