progress_tracker_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package nginx_log
  2. import (
  3. "testing"
  4. )
  5. func TestProgressTracker(t *testing.T) {
  6. // Create a new progress tracker
  7. tracker := NewProgressTracker("/var/log/nginx/access.log")
  8. // Test adding files
  9. tracker.AddFile("/var/log/nginx/access.log", false)
  10. tracker.AddFile("/var/log/nginx/access.log.1", false)
  11. tracker.AddFile("/var/log/nginx/access.log.2.gz", true)
  12. // Set estimates for files
  13. tracker.SetFileEstimate("/var/log/nginx/access.log", 1000)
  14. tracker.SetFileEstimate("/var/log/nginx/access.log.1", 2000)
  15. tracker.SetFileEstimate("/var/log/nginx/access.log.2.gz", 500)
  16. // Test initial progress
  17. percentage, stats := tracker.GetProgress()
  18. if percentage != 0 {
  19. t.Errorf("Expected initial progress to be 0, got %.2f", percentage)
  20. }
  21. if stats.TotalFiles != 3 {
  22. t.Errorf("Expected 3 total files, got %d", stats.TotalFiles)
  23. }
  24. if stats.EstimatedLines != 3500 {
  25. t.Errorf("Expected 3500 estimated lines, got %d", stats.EstimatedLines)
  26. }
  27. // Start processing first file
  28. tracker.StartFile("/var/log/nginx/access.log")
  29. percentage, stats = tracker.GetProgress()
  30. if stats.ProcessingFiles != 1 {
  31. t.Errorf("Expected 1 processing file, got %d", stats.ProcessingFiles)
  32. }
  33. // Update progress for first file
  34. tracker.UpdateFileProgress("/var/log/nginx/access.log", 500)
  35. percentage, stats = tracker.GetProgress()
  36. expectedPercentage := float64(500) / float64(3500) * 100
  37. if percentage < expectedPercentage-1 || percentage > expectedPercentage+1 {
  38. t.Errorf("Expected progress around %.2f%%, got %.2f%%", expectedPercentage, percentage)
  39. }
  40. // Complete first file
  41. tracker.CompleteFile("/var/log/nginx/access.log", 1000)
  42. percentage, stats = tracker.GetProgress()
  43. if stats.CompletedFiles != 1 {
  44. t.Errorf("Expected 1 completed file, got %d", stats.CompletedFiles)
  45. }
  46. expectedPercentage = float64(1000) / float64(3500) * 100
  47. if percentage < expectedPercentage-1 || percentage > expectedPercentage+1 {
  48. t.Errorf("Expected progress around %.2f%% after first file completion, got %.2f%%", expectedPercentage, percentage)
  49. }
  50. // Complete all files - should only trigger completion notification once
  51. tracker.StartFile("/var/log/nginx/access.log.1")
  52. tracker.CompleteFile("/var/log/nginx/access.log.1", 2000)
  53. // Check that completion hasn't been triggered yet
  54. percentage, stats = tracker.GetProgress()
  55. if stats.IsCompleted {
  56. t.Errorf("Expected IsCompleted to be false when not all files are done")
  57. }
  58. tracker.StartFile("/var/log/nginx/access.log.2.gz")
  59. tracker.CompleteFile("/var/log/nginx/access.log.2.gz", 500)
  60. // Now all files should be complete
  61. percentage, stats = tracker.GetProgress()
  62. if percentage != 100 {
  63. t.Errorf("Expected 100%% progress when all files complete, got %.2f%%", percentage)
  64. }
  65. if stats.CompletedFiles != 3 {
  66. t.Errorf("Expected 3 completed files, got %d", stats.CompletedFiles)
  67. }
  68. if !stats.IsCompleted {
  69. t.Errorf("Expected IsCompleted to be true")
  70. }
  71. // Try to complete a file again - should not trigger another notification
  72. // (this simulates the bug scenario where multiple complete notifications could be sent)
  73. tracker.CompleteFile("/var/log/nginx/access.log", 1000)
  74. // The completion should have already been notified and won't notify again
  75. }
  76. func TestEstimateFileLines(t *testing.T) {
  77. // Test uncompressed file estimation
  78. lines := EstimateFileLines("/var/log/nginx/access.log", 10000, false)
  79. expected := int64(10000 / 100) // 100 bytes per line estimate
  80. if lines != expected {
  81. t.Errorf("Expected %d lines for uncompressed file, got %d", expected, lines)
  82. }
  83. // Test compressed file estimation
  84. lines = EstimateFileLines("/var/log/nginx/access.log.gz", 1000, true)
  85. expected = int64(1000 * 3 / 100) // 3:1 compression ratio, 100 bytes per line
  86. if lines != expected {
  87. t.Errorf("Expected %d lines for compressed file, got %d", expected, lines)
  88. }
  89. // Test zero size file
  90. lines = EstimateFileLines("/var/log/nginx/access.log", 0, false)
  91. if lines != 0 {
  92. t.Errorf("Expected 0 lines for zero size file, got %d", lines)
  93. }
  94. }
  95. func TestProgressTrackerCleanup(t *testing.T) {
  96. logGroupPath := "/test/log/group"
  97. // Get a progress tracker
  98. tracker1 := GetProgressTracker(logGroupPath)
  99. if tracker1 == nil {
  100. t.Errorf("Expected non-nil progress tracker")
  101. }
  102. // Get the same tracker again - should be the same instance
  103. tracker2 := GetProgressTracker(logGroupPath)
  104. if tracker1 != tracker2 {
  105. t.Errorf("Expected same tracker instance for same log group path")
  106. }
  107. // Remove the tracker
  108. RemoveProgressTracker(logGroupPath)
  109. // Get tracker again - should be a new instance
  110. tracker3 := GetProgressTracker(logGroupPath)
  111. if tracker3 == tracker1 {
  112. t.Errorf("Expected new tracker instance after removal")
  113. }
  114. }