List sorted the session keys by file mtime and truncated them to the limit before assemble dropped empty sessions, so a newest empty file ate the page: List(1) answered no session at all though List(0) found two, and on an mtime tie an empty one could win the key tie-break. The limit now falls on the assembled sessions: keys are walked newest first and a nonempty one takes an empty one's place until limit are collected; the most-recently-active-first order is kept. Reproducer by @Ethereal49 in #1320, red before (limit=1 returned 0 nonempty sessions, want 1) and green after; the TestList flake they saw under -race -count=20 does not repeat with it. Fixes #1320. Signed-off-by: Charan Rathore <180254320+charan-rathore@users.noreply.github.com>
32 lines
727 B
Go
32 lines
727 B
Go
package sessions
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A newer empty session must not eat the whole page: List(1) skips it
|
|
// and takes the next nonempty one (#1320; reproducer by @Ethereal49).
|
|
func TestListLimitSkipsNewerEmptySession(t *testing.T) {
|
|
setup(t)
|
|
now := time.Now()
|
|
for _, f := range allFiles() {
|
|
mod := now.Add(-time.Hour)
|
|
if strings.HasPrefix(f.key, "claude:22222222-") {
|
|
mod = now
|
|
}
|
|
if err := os.Chtimes(f.path, mod, mod); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
Reset()
|
|
if got := List(0); len(got) != 2 {
|
|
t.Fatalf("fixture: got %d nonempty sessions, want 2", len(got))
|
|
}
|
|
if got := List(1); len(got) != 1 {
|
|
t.Fatalf("limit=1 returned %d nonempty sessions, want 1", len(got))
|
|
}
|
|
}
|