Test that singleflight behaves correctly after Forget called. See https://github.com/golang/go/issues/31420
(t *testing.T)
| 158 | // Test that singleflight behaves correctly after Forget called. |
| 159 | // See https://github.com/golang/go/issues/31420 |
| 160 | func TestForget(t *testing.T) { |
| 161 | var g Group[string, any] |
| 162 | |
| 163 | var ( |
| 164 | firstStarted = make(chan struct{}) |
| 165 | unblockFirst = make(chan struct{}) |
| 166 | firstFinished = make(chan struct{}) |
| 167 | ) |
| 168 | |
| 169 | go func() { |
| 170 | g.Do("key", func() (i any, e error) { |
| 171 | close(firstStarted) |
| 172 | <-unblockFirst |
| 173 | close(firstFinished) |
| 174 | return i, e |
| 175 | }) |
| 176 | }() |
| 177 | <-firstStarted |
| 178 | g.Forget("key") |
| 179 | |
| 180 | unblockSecond := make(chan struct{}) |
| 181 | secondResult := g.DoChan("key", func() (i any, e error) { |
| 182 | <-unblockSecond |
| 183 | return 2, nil |
| 184 | }) |
| 185 | |
| 186 | close(unblockFirst) |
| 187 | <-firstFinished |
| 188 | |
| 189 | thirdResult := g.DoChan("key", func() (i any, e error) { |
| 190 | return 3, nil |
| 191 | }) |
| 192 | |
| 193 | close(unblockSecond) |
| 194 | <-secondResult |
| 195 | r := <-thirdResult |
| 196 | if r.Val != 2 { |
| 197 | t.Errorf("We should receive result produced by second call, expected: 2, got %d", r.Val) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | func TestDoChan(t *testing.T) { |
| 202 | var g Group[string, any] |