mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2026-05-06 14:03:40 +08:00
test: add unit test for clamping logic in incremental indexing
This commit is contained in:
@@ -129,9 +129,12 @@ func needsIncrementalIndexing(log *nginx_log.NginxLogWithIndex, persistence logI
|
||||
// Fallback: use aggregated data cautiously by clamping the stored size so grouped entries
|
||||
// do not trigger false positives when rotation files are aggregated together.
|
||||
lastModified := time.Unix(log.LastModified, 0)
|
||||
lastSize := log.LastSize
|
||||
if lastSize == 0 || lastSize > fileSize {
|
||||
lastSize = fileSize
|
||||
rawLastSize := log.LastSize
|
||||
clampedLastSize := rawLastSize
|
||||
if clampedLastSize == 0 {
|
||||
clampedLastSize = fileSize
|
||||
} else if clampedLastSize > fileSize {
|
||||
clampedLastSize = fileSize
|
||||
}
|
||||
|
||||
// If the file was never indexed, queue it once.
|
||||
@@ -139,15 +142,15 @@ func needsIncrementalIndexing(log *nginx_log.NginxLogWithIndex, persistence logI
|
||||
return true
|
||||
}
|
||||
|
||||
if fileModTime.After(lastModified) && fileSize > lastSize {
|
||||
if fileModTime.After(lastModified) && fileSize > clampedLastSize {
|
||||
logger.Debugf("File %s needs incremental indexing (fallback path): mod_time=%s, size=%d",
|
||||
log.Path, fileModTime.Format("2006-01-02 15:04:05"), fileSize)
|
||||
return true
|
||||
}
|
||||
|
||||
if fileSize < lastSize {
|
||||
logger.Debugf("File %s needs full re-indexing (fallback path) due to size decrease: old_size=%d, new_size=%d",
|
||||
log.Path, lastSize, fileSize)
|
||||
if fileSize < clampedLastSize {
|
||||
logger.Debugf("File %s needs full re-indexing (fallback path) due to size decrease: old_size=%d (raw=%d), new_size=%d",
|
||||
log.Path, clampedLastSize, rawLastSize, fileSize)
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,37 @@ import (
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
)
|
||||
|
||||
// Test that grouped (aggregated) log metadata with oversized LastSize values
|
||||
// does not incorrectly trigger rotation detection in the fallback path.
|
||||
func TestNeedsIncrementalIndexingAggregatedSizeRespectsClamp(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
logPath := filepath.Join(tmpDir, "access.log")
|
||||
|
||||
if err := os.WriteFile(logPath, []byte("nginx-ui"), 0o644); err != nil {
|
||||
t.Fatalf("failed to create temp log file: %v", err)
|
||||
}
|
||||
|
||||
info, err := os.Stat(logPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to stat temp log file: %v", err)
|
||||
}
|
||||
|
||||
// Simulate aggregated metadata where LastSize is larger than the live file.
|
||||
logEntry := &nginx_log.NginxLogWithIndex{
|
||||
Path: logPath,
|
||||
Type: "access",
|
||||
LastModified: info.ModTime().Unix(),
|
||||
LastIndexed: time.Now().Unix(),
|
||||
LastSize: info.Size() * 5,
|
||||
}
|
||||
|
||||
if needsIncrementalIndexing(logEntry, nil) {
|
||||
t.Fatalf("aggregated size should not trigger re-indexing when clamped")
|
||||
}
|
||||
}
|
||||
|
||||
type stubLogIndexProvider struct {
|
||||
idx *model.NginxLogIndex
|
||||
err error
|
||||
|
||||
Reference in New Issue
Block a user