v0.1.0 Release
This commit is contained in:
@ -13,6 +13,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestLoggerHeartbeat verifies that heartbeat messages are logged correctly
|
||||
func TestLoggerHeartbeat(t *testing.T) {
|
||||
logger, tmpDir := createTestLogger(t)
|
||||
defer logger.Shutdown()
|
||||
@ -39,6 +40,7 @@ func TestLoggerHeartbeat(t *testing.T) {
|
||||
assert.Contains(t, string(content), "num_goroutine")
|
||||
}
|
||||
|
||||
// TestDroppedLogs confirms that the logger correctly tracks dropped logs when the buffer is full
|
||||
func TestDroppedLogs(t *testing.T) {
|
||||
logger := NewLogger()
|
||||
|
||||
@ -96,6 +98,7 @@ func TestDroppedLogs(t *testing.T) {
|
||||
assert.True(t, foundInterval, "Expected PROC heartbeat with dropped_since_last")
|
||||
}
|
||||
|
||||
// TestAdaptiveDiskCheck ensures the adaptive disk check mechanism functions without panicking
|
||||
func TestAdaptiveDiskCheck(t *testing.T) {
|
||||
logger, _ := createTestLogger(t)
|
||||
defer logger.Shutdown()
|
||||
@ -122,6 +125,7 @@ func TestAdaptiveDiskCheck(t *testing.T) {
|
||||
logger.Flush(time.Second)
|
||||
}
|
||||
|
||||
// TestDroppedLogRecoveryOnDroppedHeartbeat verifies the total drop count remains accurate even if a heartbeat is dropped
|
||||
func TestDroppedLogRecoveryOnDroppedHeartbeat(t *testing.T) {
|
||||
logger := NewLogger()
|
||||
|
||||
@ -139,38 +143,37 @@ func TestDroppedLogRecoveryOnDroppedHeartbeat(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer logger.Shutdown()
|
||||
|
||||
// 1. Flood the logger to guarantee drops. Let's aim to drop exactly 50 logs.
|
||||
// 1. Flood the logger to guarantee drops, aiming to drop exactly 50 logs
|
||||
const floodCount = 50
|
||||
for i := 0; i < int(cfg.BufferSize)+floodCount; i++ {
|
||||
logger.Info("flood", i)
|
||||
}
|
||||
|
||||
// Wait for the first heartbeat to be generated. It will carry the count of ~50 drops.
|
||||
// Wait for the first heartbeat to be generated and report ~50 drops
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
|
||||
// 2. Immediately put the logger into a "disk full" state.
|
||||
// This will cause the processor to drop the first heartbeat record.
|
||||
// 2. Immediately put the logger into a "disk full" state, causing processor to drop the first heartbeat
|
||||
diskFullCfg := logger.GetConfig()
|
||||
diskFullCfg.MinDiskFreeKB = 9999999999
|
||||
err = logger.ApplyConfig(diskFullCfg)
|
||||
require.NoError(t, err)
|
||||
// Force a disk check to ensure the state is updated to not OK.
|
||||
// Force a disk check to ensure the state is updated to not OK
|
||||
logger.performDiskCheck(true)
|
||||
assert.False(t, logger.state.DiskStatusOK.Load(), "Disk status should be not OK")
|
||||
|
||||
// 3. Now, "fix" the disk so the next heartbeat can be written successfully.
|
||||
// 3. Now, "fix" the disk so the next heartbeat can be written successfully
|
||||
diskOKCfg := logger.GetConfig()
|
||||
diskOKCfg.MinDiskFreeKB = 0
|
||||
err = logger.ApplyConfig(diskOKCfg)
|
||||
require.NoError(t, err)
|
||||
logger.performDiskCheck(true) // Ensure state is updated back to OK.
|
||||
logger.performDiskCheck(true) // Ensure state is updated back to OK
|
||||
assert.True(t, logger.state.DiskStatusOK.Load(), "Disk status should be OK")
|
||||
|
||||
// 4. Wait for the second heartbeat to be generated and written to the file.
|
||||
// 4. Wait for the second heartbeat to be generated and written to the file
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
logger.Flush(time.Second)
|
||||
|
||||
// 5. Verify the log file content.
|
||||
// 5. Verify the log file content
|
||||
content, err := os.ReadFile(filepath.Join(cfg.Directory, "log.log"))
|
||||
require.NoError(t, err)
|
||||
|
||||
@ -179,14 +182,14 @@ func TestDroppedLogRecoveryOnDroppedHeartbeat(t *testing.T) {
|
||||
lines := strings.Split(string(content), "\n")
|
||||
|
||||
for _, line := range lines {
|
||||
// Find the last valid heartbeat with drop stats.
|
||||
// Find the last valid heartbeat with drop stats
|
||||
if strings.Contains(line, `"level":"PROC"`) && strings.Contains(line, "dropped_since_last") {
|
||||
foundHeartbeat = true
|
||||
var entry map[string]interface{}
|
||||
var entry map[string]any
|
||||
err := json.Unmarshal([]byte(line), &entry)
|
||||
require.NoError(t, err, "Failed to parse heartbeat log line: %s", line)
|
||||
|
||||
fields := entry["fields"].([]interface{})
|
||||
fields := entry["fields"].([]any)
|
||||
for i := 0; i < len(fields)-1; i += 2 {
|
||||
if key, ok := fields[i].(string); ok {
|
||||
if key == "dropped_since_last" {
|
||||
@ -203,10 +206,10 @@ func TestDroppedLogRecoveryOnDroppedHeartbeat(t *testing.T) {
|
||||
require.True(t, foundHeartbeat, "Did not find the final heartbeat with drop stats")
|
||||
|
||||
// ASSERT THE CURRENT BEHAVIOR:
|
||||
// The 'dropped_since_last' count from the first heartbeat (~50) was lost when that heartbeat was dropped.
|
||||
// The only new drop in the next interval was the heartbeat record itself.
|
||||
// The 'dropped_since_last' count from the first heartbeat (~50) was lost when that heartbeat was dropped
|
||||
// The only new drop in the next interval was the heartbeat record itself
|
||||
assert.Equal(t, float64(1), intervalDropCount, "The interval drop count should only reflect the single dropped heartbeat from the previous interval.")
|
||||
|
||||
// The 'total_dropped_logs' counter should be accurate, reflecting the initial flood (~50) + the one dropped heartbeat.
|
||||
// The 'total_dropped_logs' counter should be accurate, reflecting the initial flood (~50) + the one dropped heartbeat
|
||||
assert.True(t, totalDropCount >= float64(floodCount), "Total drop count should be at least the number of flooded logs plus the dropped heartbeat.")
|
||||
}
|
||||
Reference in New Issue
Block a user