|
6 | 6 | package task |
7 | 7 |
|
8 | 8 | import ( |
| 9 | + "bytes" |
9 | 10 | "context" |
10 | 11 | "io" |
11 | 12 | "sync" |
@@ -38,6 +39,39 @@ func TestPanicHandler(t *testing.T) { |
38 | 39 | m.Terminate(nilLogger()) |
39 | 40 | } |
40 | 41 |
|
| 42 | +func TestLoggerFallback(t *testing.T) { |
| 43 | + defer leaktest.AfterTest(t)() |
| 44 | + |
| 45 | + stdoutBuf := &bytes.Buffer{} |
| 46 | + stderrBuf := &bytes.Buffer{} |
| 47 | + loggerConf := logger.Config{ |
| 48 | + Stdout: stdoutBuf, |
| 49 | + Stderr: stderrBuf, |
| 50 | + } |
| 51 | + rootLogger, err := loggerConf.NewLogger("" /* path */) |
| 52 | + if err != nil { |
| 53 | + panic(err) |
| 54 | + } |
| 55 | + |
| 56 | + ctx := context.Background() |
| 57 | + m := NewManager(ctx, rootLogger) |
| 58 | + |
| 59 | + brokenLoggerSupplierFunc := func(name string) (*logger.Logger, error) { |
| 60 | + return nil, errors.New("logger error") |
| 61 | + } |
| 62 | + |
| 63 | + m.Go(func(ctx context.Context, l *logger.Logger) error { |
| 64 | + l.Printf("this should be logged to the root logger") |
| 65 | + return nil |
| 66 | + }, LoggerFunc(brokenLoggerSupplierFunc), Name("def")) |
| 67 | + |
| 68 | + e := <-m.CompletedEvents() |
| 69 | + require.Equal(t, "def", e.Name) |
| 70 | + require.Contains(t, stderrBuf.String(), "logger error") |
| 71 | + require.Contains(t, stdoutBuf.String(), "this should be logged to the root logger") |
| 72 | + m.Terminate(nilLogger()) |
| 73 | +} |
| 74 | + |
41 | 75 | func TestErrorHandler(t *testing.T) { |
42 | 76 | defer leaktest.AfterTest(t)() |
43 | 77 | ctx := context.Background() |
|
0 commit comments