-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontWasm_timer_test.go
More file actions
98 lines (75 loc) · 2.21 KB
/
Copy pathfrontWasm_timer_test.go
File metadata and controls
98 lines (75 loc) · 2.21 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//go:build wasm
package time_test
import (
"testing"
"github.com/tinywasm/time"
)
// WASM tests run in real browser via wasmbrowsertest.
// Cannot use time.Sleep - it would freeze the browser UI.
// We can only test synchronous behavior:
// - Timer returns non-nil
// - Stop() returns true for active timer
// - Stop() returns false for already-stopped timer
//
// The actual callback execution is tested only in backend tests.
func TestAfterFunc_ReturnsTimer(t *testing.T) {
timer := time.AfterFunc(1000, func() {
// Won't execute during test - we can't wait
})
if timer == nil {
t.Error("AfterFunc should return non-nil Timer")
}
// Clean up
timer.Stop()
t.Log("AfterFunc returns Timer - passed")
}
func TestAfterFunc_StopReturnsTrue(t *testing.T) {
timer := time.AfterFunc(1000, func() {})
wasActive := timer.Stop()
if !wasActive {
t.Error("Stop() should return true for active timer")
}
t.Log("AfterFunc Stop returns true - passed")
}
func TestAfterFunc_DoubleStopReturnsFalse(t *testing.T) {
timer := time.AfterFunc(1000, func() {})
timer.Stop() // First stop
wasActive := timer.Stop() // Second stop
if wasActive {
t.Error("Stop() should return false for already-stopped timer")
}
t.Log("AfterFunc double Stop returns false - passed")
}
func TestAfterFunc_CallbackLogic(t *testing.T) {
executed := false
timer := time.AfterFunc(1000, func() {
executed = true
})
// Manually trigger the callback logic
FireTimer(timer)
if !executed {
t.Error("Callback should have been executed by FireTimer")
}
// Verify that Stop() returns false after firing
if timer.Stop() {
t.Error("Stop() should return false after timer fired")
}
// Fire again - should be no-op (safe to call on inactive timer)
FireTimer(timer)
if executed == false {
t.Error("executed flag should still be true")
}
t.Log("AfterFunc callback logic - passed")
}
func TestAfterFunc_NilCallback(t *testing.T) {
// AfterFunc with nil callback should not panic
timer := time.AfterFunc(1000, nil)
if timer == nil {
t.Error("AfterFunc should return non-nil Timer even with nil callback")
}
// Manually trigger - should not panic
FireTimer(timer)
// Clean up
timer.Stop()
t.Log("AfterFunc nil callback - passed")
}