Files
mesh-tools/node-tools/internal/bus/state_bind_integration_test.go
T
jschoubben a75d86236b Tool runner bus: pulled workers belong to their connection; two tests wait for what they assert
A worker bound for a pull was kept for the process, so a later connection's pull
used a worker bound on a closed connection and answered "connection closed"
(every run of TestAHolderPullsOneMessageWhenItHasRoom after the first). The
bound workers and the pulled messages are now the Conn's.

The two tests that failed main.165 and main.115 asserted on bus figures before
the bus had delivered them: the worker's pending count right after a publish, a
no-wait pull right after a publish, a callback counter read when a flush
returned, a 300 ms delay and a 2 s ack wait a loaded machine overruns. They now
count binds on a channel the watcher's reader fills, pull with a wait that ends
at the delivery, prove "nothing fetched ahead" by the second message's first
delivery, and use bounds a loaded machine stays within.

Builds: design 47 H H-A-363 (fix-a-bug-met-twice; whole-module tests under load, H-A-327)
2026-11-21 23:39:25 +01:00

84 lines
2.8 KiB
Go

//go:build integration
package bus_test
import (
"strings"
"testing"
"github.com/nats-io/nats.go"
mt "github.com/novox/mesh-tools/node-tools/internal/meshtest"
)
// **On the bus, a read of a state is one answer: its bucket is bound once, and bound again when it was made afresh or
// is gone** (design 47 H lead answers H-A-251). The runtime asked the bucket's stream info before every get, so each
// read was two answers; a third of a night's bus flood came this way.
func TestAStateReadOnTheBusBindsItsBucketOnce(t *testing.T) {
conn, kv := owedWatcher(t)
const bucket = "mesh-delivery-assigner_pins"
if _, err := kv.Put("a", []byte(`{"v":1}`)); err != nil {
t.Fatal(err)
}
watcher, err := nats.Connect(mt.URL(t))
if err != nil {
t.Fatal(err)
}
t.Cleanup(watcher.Close)
// Each bind the runtime asks is heard onto a channel by the watcher's reader itself, so what the bus sent before a
// flush's answer is on it when the flush returns. A counter a callback raised was read before the callback ran, on a
// loaded machine, and counted one bind short (design 47 H, H-A-363).
infos := make(chan *nats.Msg, 16)
if _, err := watcher.ChanSubscribe("$JS.API.STREAM.INFO.KV_"+bucket, infos); err != nil {
t.Fatal(err)
}
if err := watcher.Flush(); err != nil {
t.Fatal(err)
}
read := func() {
t.Helper()
e, err := conn.StateGet("gitea", "mesh-delivery-assigner.pins", "a")
if err != nil || e == nil {
t.Fatalf("read: %+v, %v", e, err)
}
}
read()
read()
read()
if err := watcher.Flush(); err != nil {
t.Fatal(err)
}
if got := len(infos); got != 1 {
t.Fatalf("three reads asked the bucket's stream info %d times, want once", got)
}
// The bucket deleted under the runtime: the read binds again, once, and says the bucket is not on the bus.
js, _ := watcher.JetStream()
if err := js.DeleteKeyValue(bucket); err != nil {
t.Fatal(err)
}
_, err = conn.StateGet("gitea", "mesh-delivery-assigner.pins", "a")
if err == nil || !strings.Contains(err.Error(), "not on the bus yet") {
t.Fatalf("a read of a deleted bucket answered %v, want that its bucket is not on the bus", err)
}
// And made afresh: read again from the new bucket.
again, err := js.CreateKeyValue(&nats.KeyValueConfig{Bucket: bucket, History: 1})
if err != nil {
t.Fatal(err)
}
if _, err := again.Put("a", []byte(`{"v":2}`)); err != nil {
t.Fatal(err)
}
e, err := conn.StateGet("gitea", "mesh-delivery-assigner.pins", "a")
if err != nil || e == nil || string(e.Value) != `{"v":2}` {
t.Fatalf("a read of the bucket made afresh: %+v, %v", e, err)
}
read()
if err := watcher.Flush(); err != nil {
t.Fatal(err)
}
if got := len(infos); got != 3 {
t.Fatalf("the bucket's stream info was asked %d times in all, want 3: once, once more when it was gone, once when made afresh", got)
}
}