//go:build integration package main // The module's live path on a bus of this test alone (design 47 G §561, §563, A-150, A-155): the night's own broker account. import ( "context" "testing" "time" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" "git.novox.be/novox/mesh-sdk/go/process" "git.novox.be/novox/mesh-sdk/go/testbus" ) // A put-off is a key of the state put-off with a time to live of its own: it is listed while it lasts, and the bus removing it // at the end of its time is heard as the expiry, once; a key released by the holder is heard as nothing. func TestAPutOffKeyIsListedAndItsExpiryIsHeardFromTheBus(t *testing.T) { url := testbus.URL(t) nc, err := nats.Connect(url) if err != nil { t.Fatal(err) } defer nc.Close() js, _ := jetstream.New(nc) ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: "restic_" + PutOffStateName, LimitMarkerTTL: time.Second}); err != nil { t.Fatal(err) } conn, err := process.Connect(process.Account{URL: url, Node: "ace", Module: "restic", User: "ace.restic", Password: "unused"}) if err != nil { t.Fatal(err) } defer conn.Close() st := stateOfPutOffs{conn.State(PutOffStateName)} now := time.Date(2026, 11, 12, 3, 0, 0, 0, time.UTC) expired := make(chan string, 4) if _, err := st.Hold("postgres.store", PutOff{Restore: "act-7", Since: now}, 2*time.Second, func() { expired <- "store" }); err != nil { t.Fatal(err) } release, err := st.Hold("postgres.dumps", PutOff{Restore: "act-7", Since: now}, time.Hour, func() { expired <- "dumps" }) if err != nil { t.Fatal(err) } all, err := st.All() if err != nil || len(all) != 2 || all["postgres.store"].Restore != "act-7" || !all["postgres.store"].Since.Equal(now) { t.Fatalf("the keys %+v, %v", all, err) } select { case who := <-expired: if who != "store" { t.Fatalf("%s expired", who) } case <-time.After(30 * time.Second): t.Fatal("the bus did not remove the key at the end of its time") } release() for { all, err = st.All() if err != nil { t.Fatal(err) } if len(all) == 0 { break } select { case who := <-expired: t.Fatalf("%s expired after its release", who) case <-time.After(100 * time.Millisecond): } } }