//go:build integration package link import ( "context" "strings" "testing" "time" "github.com/nats-io/nats.go" "github.com/novox/mesh-controller/internal/testbus" ) // Against a real bus whose largest message is small: the limit is read from the connection, the one that fits goes // through, the one over is refused by us with its size and the limit, not by the client's bare error (issue 1689). func TestADeclarationIsMeasuredAgainstTheRealBusLimit(t *testing.T) { s := testbus.StartWithMaxPayload(t, 2048) conn, err := nats.Connect(s.ClientURL()) if err != nil { t.Fatal(err) } defer conn.Close() js, err := conn.JetStream() if err != nil { t.Fatal(err) } if _, err := js.AddStream(&nats.StreamConfig{Name: "NODES", Subjects: []string{"mesh.node.*.declare"}}); err != nil { t.Fatal(err) } bus := OverNATS{Conn: conn, JS: js} if bus.MaxPayload() != 2048 { t.Fatalf("the limit read from the connection is %d, expected 2048", bus.MaxPayload()) } var told []int64 was := DeclarationMeasured DeclarationMeasured = func(_ context.Context, _ string, size, limit int64) { told = append(told, size, limit) } t.Cleanup(func() { DeclarationMeasured = was }) fits := []byte(`{"a":"` + strings.Repeat("x", 600) + `"}`) if err := Declare(t.Context(), bus, fixedSigner{}, "novox", fits, 5*time.Second); err != nil { t.Fatalf("a declaration under the limit: %v", err) } over := []byte(`{"a":"` + strings.Repeat("x", 1600) + `"}`) err = Declare(t.Context(), bus, fixedSigner{}, "novox", over, 5*time.Second) if err == nil || !strings.Contains(err.Error(), "2048 bytes") || strings.Contains(err.Error(), "maximum payload exceeded") { t.Fatalf("a declaration over the limit: %v", err) } if len(told) != 4 || told[1] != 2048 || told[2] <= 2048 { t.Fatalf("measured %v", told) } }