Files
mesh-controller/internal/link/checks_running_started_test.go
T

62 lines
1.7 KiB
Go

package link
import (
"context"
"encoding/json"
"sync"
"testing"
"time"
"github.com/nats-io/nats.go/jetstream"
)
type recordedRunning struct {
mu sync.Mutex
bodies [][]byte
}
func (r *recordedRunning) Put(_ context.Context, _ string, value []byte) (uint64, error) {
r.mu.Lock()
defer r.mu.Unlock()
r.bodies = append(r.bodies, value)
return uint64(len(r.bodies)), nil
}
func (r *recordedRunning) Delete(context.Context, string, ...jetstream.KVDeleteOpt) error { return nil }
func (r *recordedRunning) puts() [][]byte {
r.mu.Lock()
defer r.mu.Unlock()
return append([][]byte(nil), r.bodies...)
}
// 1155/AC2: a check starting puts its key, and puts it again at each heartbeat, and every value carries the same Started,
// the time the check started.
func TestACheckRunningKeepsItsStartAcrossHeartbeats(t *testing.T) { // 1155/AC2
store := &recordedRunning{}
started := time.Date(2026, 10, 18, 19, 0, 0, 0, time.UTC)
stop := keepRunningIn(context.Background(), store, "novox.a.7", aPullCheck("b1", 7, "head-7-000000000", "trunk-1-000000", false),
"builder-1", 5*time.Millisecond, started)
deadline := time.Now().Add(5 * time.Second)
for len(store.puts()) < 3 && time.Now().Before(deadline) {
time.Sleep(5 * time.Millisecond)
}
stop()
puts := store.puts()
if len(puts) < 3 {
t.Fatalf("the key was put %d times, want the first and two heartbeats", len(puts))
}
for i, body := range puts {
var got CheckRunning
if err := json.Unmarshal(body, &got); err != nil {
t.Fatal(err)
}
if !got.Started.Equal(started) {
t.Fatalf("put %d carries Started %v, want %v", i, got.Started, started)
}
if got.Builder != "builder-1" || got.Build != "b1" {
t.Fatalf("put %d lost the rest of the value: %+v", i, got)
}
}
}