-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathsynctest_go125_test.go
More file actions
96 lines (78 loc) · 2.57 KB
/
Copy pathsynctest_go125_test.go
File metadata and controls
96 lines (78 loc) · 2.57 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
//go:build go1.25 && !goexperiment.synctest && !deadlock_synctest
package deadlock
import (
"os"
"testing"
"testing/synctest"
"time"
)
// TestGo125SynctestTimerCompatibility validates timer handling with Go 1.25 native synctest.
// Uses channels for synchronization since sync.Mutex is not durably blocking in synctest.
func TestGo125SynctestTimerCompatibility(t *testing.T) {
// Skip if GODEBUG=asynctimerchan=0 is not set (required for synctest)
if os.Getenv("GODEBUG") != "asynctimerchan=0" {
t.Skip("Skipping: This test requires GODEBUG=asynctimerchan=0")
}
// This test validates that our timer handling works with synctest
synctest.Test(t, func(t *testing.T) {
t.Log("Testing timer compatibility with Go 1.25 native synctest")
// Test 1: Channel operations work (durably blocking)
ch := make(chan int)
done := make(chan bool)
go func() {
val := <-ch
if val != 42 {
t.Errorf("Expected 42, got %d", val)
}
close(done)
}()
// This should proceed immediately in synctest
ch <- 42
<-done
t.Log("✓ Channel operations work correctly")
// Test 2: time.Sleep should work without blocking the test
time.Sleep(100 * time.Millisecond)
t.Log("✓ time.Sleep completed (virtualized)")
// Test 3: Timer-based operations work
timer := time.NewTimer(50 * time.Millisecond)
select {
case <-timer.C:
t.Log("✓ Timer fired correctly in synctest")
case <-time.After(100 * time.Millisecond):
t.Error("Timer did not fire")
}
t.Log("✓ Go 1.25 synctest compatibility validated")
})
}
// TestGo125SynctestWithDeadlockDetection validates deadlock detection works with synctest.
func TestGo125SynctestWithDeadlockDetection(t *testing.T) {
// Skip if GODEBUG=asynctimerchan=0 is not set (required for synctest)
if os.Getenv("GODEBUG") != "asynctimerchan=0" {
t.Skip("Skipping: This test requires GODEBUG=asynctimerchan=0")
}
// Configure deadlock detection
oldTimeout := Opts.DeadlockTimeout
oldOnDeadlock := Opts.OnPotentialDeadlock
defer func() {
Opts.DeadlockTimeout = oldTimeout
Opts.OnPotentialDeadlock = oldOnDeadlock
}()
Opts.DeadlockTimeout = 50 * time.Millisecond
Opts.OnPotentialDeadlock = func() {
t.Log("Deadlock detected (expected)")
}
synctest.Test(t, func(t *testing.T) {
t.Log("Testing deadlock detection doesn't break synctest")
// Use channels (which ARE durably blocking) for synchronization
ch := make(chan struct{})
done := make(chan bool)
go func() {
time.Sleep(10 * time.Millisecond)
close(ch)
close(done)
}()
<-ch
<-done
t.Log("✓ Deadlock detection coexists with synctest")
})
}