(t *testing.T)
| 60 | } |
| 61 | |
| 62 | func TestLogRotationInterval(t *testing.T) { |
| 63 | LongRunningTest(t) |
| 64 | |
| 65 | // override min rotation interval for testing |
| 66 | originalMinRotationInterval := minLogRotationInterval |
| 67 | minLogRotationInterval = time.Millisecond * 10 |
| 68 | defer func() { minLogRotationInterval = originalMinRotationInterval }() |
| 69 | |
| 70 | rotationInterval := time.Millisecond * 100 |
| 71 | config := &FileLoggerConfig{ |
| 72 | Enabled: Ptr(true), |
| 73 | CollationBufferSize: Ptr(0), |
| 74 | Rotation: logRotationConfig{ |
| 75 | RotationInterval: NewConfigDuration(rotationInterval), |
| 76 | compress: Ptr(false), |
| 77 | }, |
| 78 | } |
| 79 | |
| 80 | logPath := lumberjackTempDir(t) |
| 81 | countBefore := numFilesInDir(t, logPath, false) |
| 82 | t.Logf("countBefore: %d", countBefore) |
| 83 | |
| 84 | ctx := TestCtx(t) |
| 85 | fl, err := NewFileLogger(ctx, config, LevelTrace, "test", logPath, 0, nil, nil) |
| 86 | require.NoError(t, err) |
| 87 | defer func() { |
| 88 | assert.NoError(t, fl.Close()) |
| 89 | // Wait for Lumberjack to finish its async log compression work |
| 90 | // we have no way of waiting for this to finish, or even stopping the millRun() process inside Lumberjack. |
| 91 | time.Sleep(time.Second) |
| 92 | }() |
| 93 | |
| 94 | fl.logf("test 1") |
| 95 | countAfter1 := numFilesInDir(t, logPath, false) |
| 96 | t.Logf("countAfter1: %d", countAfter1) |
| 97 | assert.Greater(t, countAfter1, countBefore) |
| 98 | |
| 99 | time.Sleep(rotationInterval * 5) |
| 100 | countAfterSleep := numFilesInDir(t, logPath, false) |
| 101 | t.Logf("countAfterSleep: %d", countAfterSleep) |
| 102 | assert.Greater(t, countAfterSleep, countAfter1) |
| 103 | |
| 104 | fl.logf("test 2") |
| 105 | countAfter2 := numFilesInDir(t, logPath, false) |
| 106 | t.Logf("countAfter2: %d", countAfter2) |
| 107 | assert.GreaterOrEqual(t, countAfter2, countAfterSleep) |
| 108 | |
| 109 | } |
| 110 | |
| 111 | // Benchmark the time it takes to write x bytes of data to a logger, and optionally rotate and compress it. |
| 112 | func BenchmarkLogRotation(b *testing.B) { |
nothing calls this directly
no test coverage detected