56 lines
1.9 KiB
Go
56 lines
1.9 KiB
Go
package link
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
)
|
|
|
|
// An answer too large for one message is recorded as paged, and a call's record says so: `calls` no
|
|
// longer reads "answered" for an answer nobody received (novox/hq issue 314).
|
|
func TestAPagedAnswerIsSaidInCalls(t *testing.T) {
|
|
l, a := NewCallLog(), newAnswers(t)
|
|
big := strings.Repeat("x", 5000)
|
|
l.serveCallWithin("mesh-controller", "conditions", nil, "_INBOX.operator.ABCDEFGHIJKLMNOPQRSTUV",
|
|
func(context.Context, json.RawMessage) (any, error) { return big, nil }, a.respond, 2048, nil)
|
|
got := a.only()
|
|
said, _ := got["error"].(string)
|
|
paged, _ := got["paged"].(map[string]any)
|
|
if !strings.Contains(said, "more than the bus carries") || paged == nil || paged["parts"].(float64) < 3 {
|
|
t.Fatalf("answered %v", got)
|
|
}
|
|
recent, _ := l.Recent()
|
|
if len(recent) != 1 || recent[0].Paged == "" || strings.Contains(string(recent[0].Answer), "in full") {
|
|
t.Fatalf("kept %+v", recent[0])
|
|
}
|
|
}
|
|
|
|
// An answer the bus refuses anyway is said to its caller and to `calls`, never only to the journal.
|
|
func TestAnAnswerTheBusRefusesIsSaid(t *testing.T) {
|
|
l := NewCallLog()
|
|
var sent [][]byte
|
|
respond := func(body []byte) error {
|
|
if len(body) > 1000 {
|
|
return nats.ErrMaxPayload
|
|
}
|
|
sent = append(sent, body)
|
|
return nil
|
|
}
|
|
l.serveCallWithin("mesh-controller", "status", nil, "_INBOX.x.1",
|
|
func(context.Context, json.RawMessage) (any, error) { return strings.Repeat("y", 4000), nil }, respond, 0, nil)
|
|
if len(sent) != 1 || !strings.Contains(string(sent[0]), "would not carry it") {
|
|
t.Fatalf("the caller was told %q", sent)
|
|
}
|
|
recent, _ := l.Recent()
|
|
if recent[0].Refused == "" {
|
|
t.Fatalf("calls does not say the answer was not sent: %+v", recent[0])
|
|
}
|
|
}
|
|
|
|
// A part goes only to the caller its answer was paged to, on the verb that answered it.
|
|
|
|
// A part that cannot be had fails the whole answer, naming it — never a shorter answer.
|