MCPcopy
hub / github.com/minio/minio-go / TestPanicDoSharedByDoChan

Function TestPanicDoSharedByDoChan

pkg/singleflight/singleflight_test.go:344–392  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

342}
343
344func TestPanicDoSharedByDoChan(t *testing.T) {
345 if os.Getenv("TEST_PANIC_DOCHAN") != "" {
346 blocked := make(chan struct{})
347 unblock := make(chan struct{})
348
349 g := new(Group[string, any])
350 go func() {
351 defer func() {
352 recover()
353 }()
354 g.Do("", func() (any, error) {
355 close(blocked)
356 <-unblock
357 panic("Panicking in Do")
358 })
359 }()
360
361 <-blocked
362 ch := g.DoChan("", func() (any, error) {
363 panic("DoChan unexpectedly executed callback")
364 })
365 close(unblock)
366 <-ch
367 t.Fatalf("DoChan unexpectedly returned")
368 }
369
370 t.Parallel()
371
372 cmd := exec.Command(executable(t), "-test.run="+t.Name(), "-test.v")
373 cmd.Env = append(os.Environ(), "TEST_PANIC_DOCHAN=1")
374 out := new(bytes.Buffer)
375 cmd.Stdout = out
376 cmd.Stderr = out
377 if err := cmd.Start(); err != nil {
378 t.Fatal(err)
379 }
380
381 err := cmd.Wait()
382 t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
383 if err == nil {
384 t.Errorf("Test subprocess passed; want a crash due to panic in Do shared by DoChan")
385 }
386 if bytes.Contains(out.Bytes(), []byte("DoChan unexpectedly")) {
387 t.Errorf("Test subprocess failed with an unexpected failure mode.")
388 }
389 if !bytes.Contains(out.Bytes(), []byte("Panicking in Do")) {
390 t.Errorf("Test subprocess failed, but the crash isn't caused by panicking in Do")
391 }
392}
393
394func ExampleGroup() {
395 g := new(Group[string, any])

Callers

nothing calls this directly

Calls 4

executableFunction · 0.85
DoMethod · 0.80
DoChanMethod · 0.80
ContainsMethod · 0.45

Tested by

no test coverage detected