The tool runner's bus tests wait on the bus's events, not on process state or time (H-A-363) #87
@@ -212,6 +212,8 @@ type Conn struct {
|
||||
bound map[string]*binding
|
||||
// bind binds one bucket: jetstream's KeyValue (a stream-info request on the bus) unless a test replaces it.
|
||||
bind func(ctx context.Context, bucket string) (jetstream.KeyValue, error)
|
||||
// pulls are the workers bound and the messages pulled on this connection (seat_pull.go).
|
||||
pulls pulls
|
||||
}
|
||||
|
||||
// answered is one subject the runtime answers on: what to subscribe again with, and how often it was
|
||||
|
||||
@@ -33,8 +33,10 @@ type Pulled struct {
|
||||
Delivered uint64 `json:"delivered"`
|
||||
}
|
||||
|
||||
// pulls are the messages modules pulled and have not settled, by id; each kept with the module it was pulled for, so
|
||||
// one module never settles another's.
|
||||
// pulls are the messages modules pulled on one connection and have not settled, by id, each kept with the module it was
|
||||
// pulled for, so one module never settles another's; and the workers bound on that connection. They are the Conn's, not
|
||||
// the process's: a worker bound on a connection since closed answered every later pull "connection closed" (design
|
||||
// 47 H, H-A-363: state outliving the connection it was made on).
|
||||
type pulls struct {
|
||||
mu sync.Mutex
|
||||
held map[string]pulledMsg
|
||||
@@ -46,8 +48,6 @@ type pulledMsg struct {
|
||||
msg jetstream.Msg
|
||||
}
|
||||
|
||||
var seatPulls = &pulls{held: map[string]pulledMsg{}, cons: map[string]jetstream.Consumer{}}
|
||||
|
||||
func pullID() string {
|
||||
var b [8]byte
|
||||
_, _ = rand.Read(b[:])
|
||||
@@ -111,17 +111,20 @@ func (c *Conn) SeatPull(module, consumer string, wait time.Duration) (*Pulled, e
|
||||
}
|
||||
p := &Pulled{ID: pullID(), Worker: w.Consumer, Subject: msg.Subject(), Body: json.RawMessage(msg.Data()),
|
||||
Headers: headers, Delivered: delivered}
|
||||
seatPulls.mu.Lock()
|
||||
seatPulls.held[p.ID] = pulledMsg{module: module, msg: msg}
|
||||
seatPulls.mu.Unlock()
|
||||
c.pulls.mu.Lock()
|
||||
if c.pulls.held == nil {
|
||||
c.pulls.held = map[string]pulledMsg{}
|
||||
}
|
||||
c.pulls.held[p.ID] = pulledMsg{module: module, msg: msg}
|
||||
c.pulls.mu.Unlock()
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (c *Conn) pullConsumer(module string, w SeatWorker) (jetstream.Consumer, error) {
|
||||
key := module + " " + w.Consumer
|
||||
seatPulls.mu.Lock()
|
||||
bound, ok := seatPulls.cons[key]
|
||||
seatPulls.mu.Unlock()
|
||||
c.pulls.mu.Lock()
|
||||
bound, ok := c.pulls.cons[key]
|
||||
c.pulls.mu.Unlock()
|
||||
if ok {
|
||||
return bound, nil
|
||||
}
|
||||
@@ -135,9 +138,12 @@ func (c *Conn) pullConsumer(module string, w SeatWorker) (jetstream.Consumer, er
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("binding %s's worker %s on %s: %w", module, w.Consumer, w.Stream, err)
|
||||
}
|
||||
seatPulls.mu.Lock()
|
||||
seatPulls.cons[key] = bound
|
||||
seatPulls.mu.Unlock()
|
||||
c.pulls.mu.Lock()
|
||||
if c.pulls.cons == nil {
|
||||
c.pulls.cons = map[string]jetstream.Consumer{}
|
||||
}
|
||||
c.pulls.cons[key] = bound
|
||||
c.pulls.mu.Unlock()
|
||||
return bound, nil
|
||||
}
|
||||
|
||||
@@ -168,15 +174,15 @@ func (c *Conn) SeatSettleAfter(module, id string, delay time.Duration) error {
|
||||
}
|
||||
|
||||
func (c *Conn) settle(module, id, fate string, delay time.Duration) error {
|
||||
seatPulls.mu.Lock()
|
||||
p, ok := seatPulls.held[id]
|
||||
c.pulls.mu.Lock()
|
||||
p, ok := c.pulls.held[id]
|
||||
if ok && p.module != module {
|
||||
ok = false
|
||||
}
|
||||
if ok && fate != SettleProgress {
|
||||
delete(seatPulls.held, id)
|
||||
delete(c.pulls.held, id)
|
||||
}
|
||||
seatPulls.mu.Unlock()
|
||||
c.pulls.mu.Unlock()
|
||||
if !ok {
|
||||
return fmt.Errorf("%s holds no pulled message %s", module, id)
|
||||
}
|
||||
@@ -192,8 +198,8 @@ func (c *Conn) settle(module, id, fate string, delay time.Duration) error {
|
||||
case SettleNakAfter:
|
||||
return p.msg.NakWithDelay(delay)
|
||||
}
|
||||
seatPulls.mu.Lock()
|
||||
seatPulls.held[id] = p // an unknown fate settles nothing
|
||||
seatPulls.mu.Unlock()
|
||||
c.pulls.mu.Lock()
|
||||
c.pulls.held[id] = p // an unknown fate settles nothing
|
||||
c.pulls.mu.Unlock()
|
||||
return fmt.Errorf("a pulled message's fate is ack, nak, nak-after, term or progress, not %q", fate)
|
||||
}
|
||||
|
||||
@@ -19,17 +19,22 @@ func TestAHolderPullsOneMessageWhenItHasRoom(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
first, err := conn.SeatPull("messenger", "SEAT_OPERATOR_CHANNEL_worker", 0)
|
||||
// A pull that wants a message waits for it: the worker learns of a stored message after the publish is answered, so
|
||||
// a pull that waits for nothing can find none yet on a loaded machine (design 47 H, H-A-363). A wait is a bound, not a
|
||||
// measure: it ends at the delivery.
|
||||
first, err := conn.SeatPull("messenger", "SEAT_OPERATOR_CHANNEL_worker", 10*time.Second)
|
||||
if err != nil || first == nil || string(first.Body) != `{"id":"a1"}` || first.Delivered != 1 {
|
||||
t.Fatalf("the first pull = %+v, %v", first, err)
|
||||
}
|
||||
// One pulled, one still waiting on the stream: nothing was fetched ahead.
|
||||
// One pulled and in hand, the other never delivered: nothing was fetched ahead. What is in hand is the worker's own
|
||||
// count, set by the delivery itself; the count still waiting is the worker's figure of the stream, learnt later, so
|
||||
// the second message's first delivery says it instead.
|
||||
info, err := js.ConsumerInfo("SEAT_OPERATOR_CHANNEL", "SEAT_OPERATOR_CHANNEL_worker")
|
||||
if err != nil || info.NumPending != 1 || info.NumAckPending != 1 {
|
||||
t.Fatalf("after one pull: pending %d, in hand %d (%v); want 1 and 1", info.NumPending, info.NumAckPending, err)
|
||||
if err != nil || info.NumAckPending != 1 {
|
||||
t.Fatalf("after one pull: in hand %d (%v); want 1", info.NumAckPending, err)
|
||||
}
|
||||
second, err := conn.SeatPull("messenger", "SEAT_OPERATOR_CHANNEL_worker", 0)
|
||||
if err != nil || second == nil || string(second.Body) != `{"id":"a2"}` {
|
||||
second, err := conn.SeatPull("messenger", "SEAT_OPERATOR_CHANNEL_worker", 10*time.Second)
|
||||
if err != nil || second == nil || string(second.Body) != `{"id":"a2"}` || second.Delivered != 1 {
|
||||
t.Fatalf("the second pull = %+v, %v", second, err)
|
||||
}
|
||||
if none, err := conn.SeatPull("messenger", "SEAT_OPERATOR_CHANNEL_worker", 0); err != nil || none != nil {
|
||||
@@ -54,22 +59,23 @@ func TestAHolderPullsOneMessageWhenItHasRoom(t *testing.T) {
|
||||
if err := conn.SeatSettle("messenger", second.ID, bus.SettleNak); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
again, err := conn.SeatPull("messenger", "SEAT_OPERATOR_CHANNEL_worker", time.Second)
|
||||
again, err := conn.SeatPull("messenger", "SEAT_OPERATOR_CHANNEL_worker", 10*time.Second)
|
||||
if err != nil || again == nil || string(again.Body) != `{"id":"a2"}` || again.Delivered != 2 {
|
||||
t.Fatalf("the message handed back = %+v, %v", again, err)
|
||||
}
|
||||
// Issue 1748: a message settled with a delay is not given again at once, is after it, one delivery higher; a delay
|
||||
// of 0 settles nothing.
|
||||
// of 0 settles nothing. The delay is what the next pull must come within to see nothing, so it is a second, not a
|
||||
// moment a loaded machine can spend between two calls.
|
||||
if err := conn.SeatSettleAfter("messenger", again.ID, 0); err == nil {
|
||||
t.Error("a delay of 0 was accepted")
|
||||
}
|
||||
if err := conn.SeatSettleAfter("messenger", again.ID, 300*time.Millisecond); err != nil {
|
||||
if err := conn.SeatSettleAfter("messenger", again.ID, time.Second); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if early, err := conn.SeatPull("messenger", "SEAT_OPERATOR_CHANNEL_worker", 0); err != nil || early != nil {
|
||||
t.Fatalf("a message given again after a delay came at once: %+v, %v", early, err)
|
||||
}
|
||||
late, err := conn.SeatPull("messenger", "SEAT_OPERATOR_CHANNEL_worker", 2*time.Second)
|
||||
late, err := conn.SeatPull("messenger", "SEAT_OPERATOR_CHANNEL_worker", 10*time.Second)
|
||||
if err != nil || late == nil || late.Delivered != 3 {
|
||||
t.Fatalf("the message given again after its delay = %+v, %v", late, err)
|
||||
}
|
||||
|
||||
@@ -34,11 +34,13 @@ func seatMesh(t *testing.T) (*mt.Mesh, *bus.Conn, nats.JetStreamContext) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// The workers' acknowledgement wait is wider than a test spends holding a message on a loaded machine: a message
|
||||
// held past it is delivered again, and a pull that wants none would get it (design 47 H, H-A-363).
|
||||
for stream, c := range map[string]*nats.ConsumerConfig{
|
||||
"SEAT_OPERATOR_CHANNEL": {Durable: "SEAT_OPERATOR_CHANNEL_worker", AckPolicy: nats.AckExplicitPolicy,
|
||||
FilterSubject: "mesh.seat.operator-channel.accept.>", AckWait: 2 * time.Second},
|
||||
FilterSubject: "mesh.seat.operator-channel.accept.>", AckWait: 30 * time.Second},
|
||||
"SEAT_CHANNEL": {Durable: "SEAT_CHANNEL_TELEGRAM_worker", AckPolicy: nats.AckExplicitPolicy,
|
||||
FilterSubject: "mesh.seat.channel.accept.*.telegram", AckWait: 2 * time.Second},
|
||||
FilterSubject: "mesh.seat.channel.accept.*.telegram", AckWait: 30 * time.Second},
|
||||
} {
|
||||
if _, err := js.AddConsumer(stream, c); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -4,7 +4,6 @@ package bus_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
@@ -26,8 +25,11 @@ func TestAStateReadOnTheBusBindsItsBucketOnce(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(watcher.Close)
|
||||
var infos atomic.Int32
|
||||
if _, err := watcher.Subscribe("$JS.API.STREAM.INFO.KV_"+bucket, func(*nats.Msg) { infos.Add(1) }); err != nil {
|
||||
// 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 {
|
||||
@@ -46,7 +48,7 @@ func TestAStateReadOnTheBusBindsItsBucketOnce(t *testing.T) {
|
||||
if err := watcher.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := infos.Load(); got != 1 {
|
||||
if got := len(infos); got != 1 {
|
||||
t.Fatalf("three reads asked the bucket's stream info %d times, want once", got)
|
||||
}
|
||||
|
||||
@@ -75,7 +77,7 @@ func TestAStateReadOnTheBusBindsItsBucketOnce(t *testing.T) {
|
||||
if err := watcher.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := infos.Load(); got != 3 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user