init monlet repo with stage 0-2 (docs, contract, agent MVP)
This commit is contained in:
129
agent/internal/spool/spool_test.go
Normal file
129
agent/internal/spool/spool_test.go
Normal file
@@ -0,0 +1,129 @@
|
||||
package spool
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/monlet/agent/internal/scheduler"
|
||||
)
|
||||
|
||||
func mkEvent(id string) scheduler.Event {
|
||||
return scheduler.Event{EventID: id, CheckID: "c1", Status: "ok"}
|
||||
}
|
||||
|
||||
func TestEnqueuePeekCommit(t *testing.T) {
|
||||
s, err := Open(t.TempDir(), 10, 1<<20)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 3; i++ {
|
||||
if err := s.Enqueue(mkEvent(fmt.Sprintf("e%d", i))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
items, err := s.Peek(10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(items) != 3 {
|
||||
t.Fatalf("got %d items", len(items))
|
||||
}
|
||||
if items[0].Event.EventID != "e0" {
|
||||
t.Fatalf("FIFO violated: %q", items[0].Event.EventID)
|
||||
}
|
||||
s.Commit([]uint64{items[0].Seq, items[1].Seq})
|
||||
if s.Len() != 1 {
|
||||
t.Fatalf("len=%d", s.Len())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDropOldestByCount(t *testing.T) {
|
||||
s, err := Open(t.TempDir(), 3, 1<<20)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 5; i++ {
|
||||
_ = s.Enqueue(mkEvent(fmt.Sprintf("e%d", i)))
|
||||
}
|
||||
if s.Len() != 3 {
|
||||
t.Fatalf("len=%d", s.Len())
|
||||
}
|
||||
items, _ := s.Peek(10)
|
||||
if items[0].Event.EventID != "e2" {
|
||||
t.Fatalf("expected oldest dropped, got %q", items[0].Event.EventID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDropOldestByBytes(t *testing.T) {
|
||||
big := strings.Repeat("x", 1000)
|
||||
s, err := Open(t.TempDir(), 1_000_000, 3000)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
ev := mkEvent(fmt.Sprintf("e%d", i))
|
||||
ev.Output = big
|
||||
_ = s.Enqueue(ev)
|
||||
}
|
||||
if s.Bytes() > 3000 {
|
||||
t.Fatalf("bytes=%d > 3000", s.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestReopenAppliesLimits(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s, _ := Open(dir, 10, 1<<20)
|
||||
for i := 0; i < 5; i++ {
|
||||
_ = s.Enqueue(mkEvent(fmt.Sprintf("e%d", i)))
|
||||
}
|
||||
// Reopen with tighter limit: should drop oldest down to maxEvents.
|
||||
s2, err := Open(dir, 2, 1<<20)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s2.Len() != 2 {
|
||||
t.Fatalf("expected limits applied on Open, len=%d", s2.Len())
|
||||
}
|
||||
items, _ := s2.Peek(10)
|
||||
if items[0].Event.EventID != "e3" {
|
||||
t.Fatalf("expected oldest dropped, got %q", items[0].Event.EventID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPeekSkipsCorruptAndCleansList(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s, _ := Open(dir, 10, 1<<20)
|
||||
_ = s.Enqueue(mkEvent("e0"))
|
||||
_ = s.Enqueue(mkEvent("e1"))
|
||||
// Corrupt the first file on disk.
|
||||
items, _ := s.Peek(10)
|
||||
if err := os.WriteFile(filepath.Join(dir, fmt.Sprintf("%014d.json", items[0].Seq)), []byte("not-json"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, _ := s.Peek(10)
|
||||
if len(out) != 1 || out[0].Event.EventID != "e1" {
|
||||
t.Fatalf("got %+v", out)
|
||||
}
|
||||
if s.Len() != 1 {
|
||||
t.Fatalf("corrupt entry not pruned from list, len=%d", s.Len())
|
||||
}
|
||||
}
|
||||
|
||||
func TestReopenPreservesOrder(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s, _ := Open(dir, 100, 1<<20)
|
||||
for i := 0; i < 3; i++ {
|
||||
_ = s.Enqueue(mkEvent(fmt.Sprintf("e%d", i)))
|
||||
}
|
||||
s2, err := Open(dir, 100, 1<<20)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := s2.Peek(10)
|
||||
if len(items) != 3 || items[0].Event.EventID != "e0" || items[2].Event.EventID != "e2" {
|
||||
t.Fatalf("bad reopen: %+v", items)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user