|
5 | 5 | package sync_test
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "runtime" |
8 | 9 | "sync"
|
9 | 10 | "testing"
|
10 | 11 | )
|
@@ -61,99 +62,114 @@ func TestOnceValues(t *testing.T) {
|
61 | 62 | }
|
62 | 63 | }
|
63 | 64 |
|
64 |
| -// TODO: need to implement more complete panic handling for these tests. |
65 |
| -// func testOncePanicX(t *testing.T, calls *int, f func()) { |
66 |
| -// testOncePanicWith(t, calls, f, func(label string, p any) { |
67 |
| -// if p != "x" { |
68 |
| -// t.Fatalf("%s: want panic %v, got %v", label, "x", p) |
69 |
| -// } |
70 |
| -// }) |
71 |
| -// } |
72 |
| - |
73 |
| -// func testOncePanicWith(t *testing.T, calls *int, f func(), check func(label string, p any)) { |
74 |
| -// // Check that the each call to f panics with the same value, but the |
75 |
| -// // underlying function is only called once. |
76 |
| -// for _, label := range []string{"first time", "second time"} { |
77 |
| -// var p any |
78 |
| -// panicked := true |
79 |
| -// func() { |
80 |
| -// defer func() { |
81 |
| -// p = recover() |
82 |
| -// }() |
83 |
| -// f() |
84 |
| -// panicked = false |
85 |
| -// }() |
86 |
| -// if !panicked { |
87 |
| -// t.Fatalf("%s: f did not panic", label) |
88 |
| -// } |
89 |
| -// check(label, p) |
90 |
| -// } |
91 |
| -// if *calls != 1 { |
92 |
| -// t.Errorf("want calls==1, got %d", *calls) |
93 |
| -// } |
94 |
| -// } |
95 |
| - |
96 |
| -// func TestOnceFuncPanic(t *testing.T) { |
97 |
| -// calls := 0 |
98 |
| -// f := sync.OnceFunc(func() { |
99 |
| -// calls++ |
100 |
| -// panic("x") |
101 |
| -// }) |
102 |
| -// testOncePanicX(t, &calls, f) |
103 |
| -// } |
104 |
| - |
105 |
| -// func TestOnceValuePanic(t *testing.T) { |
106 |
| -// calls := 0 |
107 |
| -// f := sync.OnceValue(func() int { |
108 |
| -// calls++ |
109 |
| -// panic("x") |
110 |
| -// }) |
111 |
| -// testOncePanicX(t, &calls, func() { f() }) |
112 |
| -// } |
113 |
| - |
114 |
| -// func TestOnceValuesPanic(t *testing.T) { |
115 |
| -// calls := 0 |
116 |
| -// f := sync.OnceValues(func() (int, int) { |
117 |
| -// calls++ |
118 |
| -// panic("x") |
119 |
| -// }) |
120 |
| -// testOncePanicX(t, &calls, func() { f() }) |
121 |
| -// } |
122 |
| -// |
123 |
| -// func TestOnceFuncPanicNil(t *testing.T) { |
124 |
| -// calls := 0 |
125 |
| -// f := sync.OnceFunc(func() { |
126 |
| -// calls++ |
127 |
| -// panic(nil) |
128 |
| -// }) |
129 |
| -// testOncePanicWith(t, &calls, f, func(label string, p any) { |
130 |
| -// switch p.(type) { |
131 |
| -// case nil, *runtime.PanicNilError: |
132 |
| -// return |
133 |
| -// } |
134 |
| -// t.Fatalf("%s: want nil panic, got %v", label, p) |
135 |
| -// }) |
136 |
| -// } |
137 |
| -// |
138 |
| -// func TestOnceFuncGoexit(t *testing.T) { |
139 |
| -// // If f calls Goexit, the results are unspecified. But check that f doesn't |
140 |
| -// // get called twice. |
141 |
| -// calls := 0 |
142 |
| -// f := sync.OnceFunc(func() { |
143 |
| -// calls++ |
144 |
| -// runtime.Goexit() |
145 |
| -// }) |
146 |
| -// var wg sync.WaitGroup |
147 |
| -// for i := 0; i < 2; i++ { |
148 |
| -// wg.Add(1) |
149 |
| -// go func() { |
150 |
| -// defer wg.Done() |
151 |
| -// defer func() { recover() }() |
152 |
| -// f() |
153 |
| -// }() |
154 |
| -// wg.Wait() |
155 |
| -// } |
156 |
| -// if calls != 1 { |
157 |
| -// t.Errorf("want calls==1, got %d", calls) |
158 |
| -// } |
159 |
| -// } |
| 65 | +func testOncePanicX(t *testing.T, calls *int, f func()) { |
| 66 | + testOncePanicWith(t, calls, f, func(label string, p any) { |
| 67 | + if p != "x" { |
| 68 | + t.Fatalf("%s: want panic %v, got %v", label, "x", p) |
| 69 | + } |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +func testOncePanicWith(t *testing.T, calls *int, f func(), check func(label string, p any)) { |
| 74 | + // Check that the each call to f panics with the same value, but the |
| 75 | + // underlying function is only called once. |
| 76 | + for _, label := range []string{"first time", "second time"} { |
| 77 | + var p any |
| 78 | + panicked := true |
| 79 | + func() { |
| 80 | + defer func() { |
| 81 | + p = recover() |
| 82 | + }() |
| 83 | + f() |
| 84 | + panicked = false |
| 85 | + }() |
| 86 | + if !panicked { |
| 87 | + t.Fatalf("%s: f did not panic", label) |
| 88 | + } |
| 89 | + check(label, p) |
| 90 | + } |
| 91 | + if *calls != 1 { |
| 92 | + t.Errorf("want calls==1, got %d", *calls) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestOnceFuncPanic(t *testing.T) { |
| 97 | + if runtime.GOARCH == "wasm" { |
| 98 | + t.Skip("not supported yet for TinyGo wasm") |
| 99 | + } |
| 100 | + |
| 101 | + calls := 0 |
| 102 | + f := sync.OnceFunc(func() { |
| 103 | + calls++ |
| 104 | + panic("x") |
| 105 | + }) |
| 106 | + testOncePanicX(t, &calls, f) |
| 107 | +} |
| 108 | + |
| 109 | +func TestOnceValuePanic(t *testing.T) { |
| 110 | + if runtime.GOARCH == "wasm" { |
| 111 | + t.Skip("not supported yet for TinyGo wasm") |
| 112 | + } |
| 113 | + |
| 114 | + calls := 0 |
| 115 | + f := sync.OnceValue(func() int { |
| 116 | + calls++ |
| 117 | + panic("x") |
| 118 | + }) |
| 119 | + testOncePanicX(t, &calls, func() { f() }) |
| 120 | +} |
| 121 | + |
| 122 | +func TestOnceValuesPanic(t *testing.T) { |
| 123 | + if runtime.GOARCH == "wasm" { |
| 124 | + t.Skip("not supported yet for TinyGo wasm") |
| 125 | + } |
| 126 | + |
| 127 | + calls := 0 |
| 128 | + f := sync.OnceValues(func() (int, int) { |
| 129 | + calls++ |
| 130 | + panic("x") |
| 131 | + }) |
| 132 | + testOncePanicX(t, &calls, func() { f() }) |
| 133 | +} |
| 134 | + |
| 135 | +func TestOnceFuncPanicNil(t *testing.T) { |
| 136 | + if runtime.GOARCH == "wasm" { |
| 137 | + t.Skip("not supported yet for TinyGo wasm") |
| 138 | + } |
| 139 | + |
| 140 | + calls := 0 |
| 141 | + f := sync.OnceFunc(func() { |
| 142 | + calls++ |
| 143 | + panic(nil) |
| 144 | + }) |
| 145 | + testOncePanicWith(t, &calls, f, func(label string, p any) { |
| 146 | + switch p.(type) { |
| 147 | + case nil, *runtime.PanicNilError: |
| 148 | + return |
| 149 | + } |
| 150 | + t.Fatalf("%s: want nil panic, got %v", label, p) |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +func TestOnceFuncGoexit(t *testing.T) { |
| 155 | + // If f calls Goexit, the results are unspecified. But check that f doesn't |
| 156 | + // get called twice. |
| 157 | + calls := 0 |
| 158 | + f := sync.OnceFunc(func() { |
| 159 | + calls++ |
| 160 | + runtime.Goexit() |
| 161 | + }) |
| 162 | + var wg sync.WaitGroup |
| 163 | + for i := 0; i < 2; i++ { |
| 164 | + wg.Add(1) |
| 165 | + go func() { |
| 166 | + defer wg.Done() |
| 167 | + defer func() { recover() }() |
| 168 | + f() |
| 169 | + }() |
| 170 | + wg.Wait() |
| 171 | + } |
| 172 | + if calls != 1 { |
| 173 | + t.Errorf("want calls==1, got %d", calls) |
| 174 | + } |
| 175 | +} |
0 commit comments