Skip to content

Commit e96490c

Browse files
committed
test: add unit test for clamping logic in incremental indexing
1 parent d4fa5a5 commit e96490c

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

internal/cron/incremental_indexing.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,28 @@ func needsIncrementalIndexing(log *nginx_log.NginxLogWithIndex, persistence logI
129129
// Fallback: use aggregated data cautiously by clamping the stored size so grouped entries
130130
// do not trigger false positives when rotation files are aggregated together.
131131
lastModified := time.Unix(log.LastModified, 0)
132-
lastSize := log.LastSize
133-
if lastSize == 0 || lastSize > fileSize {
134-
lastSize = fileSize
132+
rawLastSize := log.LastSize
133+
clampedLastSize := rawLastSize
134+
if clampedLastSize == 0 {
135+
clampedLastSize = fileSize
136+
} else if clampedLastSize > fileSize {
137+
clampedLastSize = fileSize
135138
}
136139

137140
// If the file was never indexed, queue it once.
138141
if log.LastIndexed == 0 {
139142
return true
140143
}
141144

142-
if fileModTime.After(lastModified) && fileSize > lastSize {
145+
if fileModTime.After(lastModified) && fileSize > clampedLastSize {
143146
logger.Debugf("File %s needs incremental indexing (fallback path): mod_time=%s, size=%d",
144147
log.Path, fileModTime.Format("2006-01-02 15:04:05"), fileSize)
145148
return true
146149
}
147150

148-
if fileSize < lastSize {
149-
logger.Debugf("File %s needs full re-indexing (fallback path) due to size decrease: old_size=%d, new_size=%d",
150-
log.Path, lastSize, fileSize)
151+
if fileSize < clampedLastSize {
152+
logger.Debugf("File %s needs full re-indexing (fallback path) due to size decrease: old_size=%d (raw=%d), new_size=%d",
153+
log.Path, clampedLastSize, rawLastSize, fileSize)
151154
return true
152155
}
153156

internal/cron/incremental_indexing_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,37 @@ import (
1111
"github.com/0xJacky/Nginx-UI/model"
1212
)
1313

14+
// Test that grouped (aggregated) log metadata with oversized LastSize values
15+
// does not incorrectly trigger rotation detection in the fallback path.
16+
func TestNeedsIncrementalIndexingAggregatedSizeRespectsClamp(t *testing.T) {
17+
t.Parallel()
18+
19+
tmpDir := t.TempDir()
20+
logPath := filepath.Join(tmpDir, "access.log")
21+
22+
if err := os.WriteFile(logPath, []byte("nginx-ui"), 0o644); err != nil {
23+
t.Fatalf("failed to create temp log file: %v", err)
24+
}
25+
26+
info, err := os.Stat(logPath)
27+
if err != nil {
28+
t.Fatalf("failed to stat temp log file: %v", err)
29+
}
30+
31+
// Simulate aggregated metadata where LastSize is larger than the live file.
32+
logEntry := &nginx_log.NginxLogWithIndex{
33+
Path: logPath,
34+
Type: "access",
35+
LastModified: info.ModTime().Unix(),
36+
LastIndexed: time.Now().Unix(),
37+
LastSize: info.Size() * 5,
38+
}
39+
40+
if needsIncrementalIndexing(logEntry, nil) {
41+
t.Fatalf("aggregated size should not trigger re-indexing when clamped")
42+
}
43+
}
44+
1445
type stubLogIndexProvider struct {
1546
idx *model.NginxLogIndex
1647
err error

0 commit comments

Comments
 (0)