node-tools: a stopping tool runner answers no call; its callers meet no responders (design 47 H2 H-A-570, issue 721) #91
@@ -205,6 +205,9 @@ type Conn struct {
|
||||
// answersDown is set once the runner took down every subscription it answers calls on at its stop (StopAnswering):
|
||||
// from then on it subscribes none again, so every later call meets no responders here (design 47 H2 H-A-570 (1)).
|
||||
answersDown bool
|
||||
// subscribedHook, when a test set it (SetSubscribedHook), is called between making an answering subscription and keeping
|
||||
// it, so a test runs the stop in between (tests only).
|
||||
subscribedHook func(subject string)
|
||||
// RetryAfter is how long after a refusal a subscription is asked for again, by attempt; past the
|
||||
// last, it is given up and said. A variable so a test need not wait minutes.
|
||||
RetryAfter []time.Duration
|
||||
@@ -746,11 +749,13 @@ func (c *Conn) answerCalledOn(subject, queue string, h CalledHandler) (func(), e
|
||||
if err != nil {
|
||||
return func() {}, err
|
||||
}
|
||||
c.track(sub)
|
||||
c.subscribed(subject)
|
||||
a := &answered{subject: subject, queue: queue, cb: cb, sub: sub}
|
||||
c.mu.Lock()
|
||||
c.answering[answeringKey(subject, queue)] = a
|
||||
c.mu.Unlock()
|
||||
// Kept, or taken down, under the one lock the stop takes its subscriptions down under, so a serve racing the stop is
|
||||
// never missed by it (design 47 H2 H-A-570 (1)).
|
||||
if err := c.keepAnswering(sub, func() { c.answering[answeringKey(subject, queue)] = a }); err != nil {
|
||||
return func() {}, err
|
||||
}
|
||||
return func() {
|
||||
c.mu.Lock()
|
||||
a.stopped = true
|
||||
@@ -1133,11 +1138,42 @@ func (c *Conn) answersAreDown(subject string) error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.answersDown {
|
||||
return fmt.Errorf("%s is not served: at its stop the tool runner took down every subscription it answers calls on", subject)
|
||||
return answersDownError(subject)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func answersDownError(subject string) error {
|
||||
return fmt.Errorf("%s is not served: at its stop the tool runner took down every subscription it answers calls on", subject)
|
||||
}
|
||||
|
||||
// keepAnswering keeps a subscription just made that answers calls, with keep registering it, in one section under c.mu,
|
||||
// the lock StopAnswering takes its subscriptions down under: when the stop began meanwhile, it drains the subscription
|
||||
// instead and refuses it, so none outlives the stop (design 47 H2 H-A-570 (1)).
|
||||
func (c *Conn) keepAnswering(sub *nats.Subscription, keep func()) error {
|
||||
c.mu.Lock()
|
||||
if c.answersDown {
|
||||
c.mu.Unlock()
|
||||
_ = sub.Drain()
|
||||
return answersDownError(sub.Subject)
|
||||
}
|
||||
c.subs = append(c.subs, sub)
|
||||
keep()
|
||||
c.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// subscribed calls the connection's subscribedHook, when a test set one, between making an answering subscription and
|
||||
// keeping it.
|
||||
func (c *Conn) subscribed(subject string) {
|
||||
c.mu.Lock()
|
||||
hook := c.subscribedHook
|
||||
c.mu.Unlock()
|
||||
if hook != nil {
|
||||
hook(subject)
|
||||
}
|
||||
}
|
||||
|
||||
// Close unsubscribes everything and drains, so an in-flight reply is finished rather than dropped.
|
||||
func (c *Conn) Close() {
|
||||
c.mu.Lock()
|
||||
|
||||
@@ -290,4 +290,17 @@ func TestARunnerAtItsStopAnswersNoCallAndItsCallersMeetNoResponders(t *testing.T
|
||||
if err := <-held; err != nil {
|
||||
t.Errorf("the call in flight at the stop ended %v, want it answered", err)
|
||||
}
|
||||
// A serve racing the stop (a membership change, a bundle's new tool): the stop begins between its subscription and its
|
||||
// keeping, and still the subject is not left answered, its subscription taken down and the serve refused.
|
||||
raced := dial(t, url, "ledger", "anchor", "")
|
||||
var once sync.Once
|
||||
bus.SetSubscribedHook(raced, func(string) { once.Do(raced.StopAnswering) })
|
||||
if _, err := raced.HandleSubjectCalled("mesh.mod.ledger.tool.raced", func(string, string, json.RawMessage) (any, error) {
|
||||
return map[string]any{"raced": true}, nil
|
||||
}); err == nil {
|
||||
t.Error("a serve that raced the stop was kept, want it refused")
|
||||
}
|
||||
if answered, err := asker.AskWithin("ledger.raced", map[string]any{}, "", 5*time.Second); !errors.Is(err, bus.ErrNoResponders) {
|
||||
t.Errorf("a call of a tool whose serve raced the stop was answered %s, %v; want no responders", answered.Result, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,3 +6,11 @@ func SetTickHook(f func()) (restore func()) {
|
||||
tickHook = f
|
||||
return func() { tickHook = old }
|
||||
}
|
||||
|
||||
// SetSubscribedHook makes c call f with the subject between making each answering subscription and keeping it, so a test can
|
||||
// run the stop in between (design 47 H2 H-A-570 (1)).
|
||||
func SetSubscribedHook(c *Conn, f func(subject string)) {
|
||||
c.mu.Lock()
|
||||
c.subscribedHook = f
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
@@ -244,14 +244,17 @@ func (c *Conn) SeatAnswer(module, subject string, answer func(subject string, bo
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.track(sub)
|
||||
// Kept among the proofs the stop takes down (design 47 H2 H-A-570 (1)).
|
||||
c.mu.Lock()
|
||||
if c.proofs == nil {
|
||||
c.proofs = map[*nats.Subscription]bool{}
|
||||
c.subscribed(subject)
|
||||
// Kept among the proofs the stop takes down, or taken down at once when the stop began meanwhile (design 47 H2 H-A-570
|
||||
// (1)).
|
||||
if err := c.keepAnswering(sub, func() {
|
||||
if c.proofs == nil {
|
||||
c.proofs = map[*nats.Subscription]bool{}
|
||||
}
|
||||
c.proofs[sub] = true
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.proofs[sub] = true
|
||||
c.mu.Unlock()
|
||||
var once sync.Once
|
||||
return func() {
|
||||
once.Do(func() {
|
||||
|
||||
@@ -230,8 +230,16 @@ func TestAProofIsAskedAndAnsweredAndNeverKept(t *testing.T) {
|
||||
}
|
||||
}
|
||||
// 721/AC4: at its stop the runner takes its proofs' subscriptions down with the rest (design 47 H2 H-A-570 (1)): a proof
|
||||
// after it meets no responders, never an answer of the old runner's, and a proof answered after it is not subscribed.
|
||||
conn.StopAnswering()
|
||||
// after it meets no responders, never an answer of the old runner's, and a proof answered after it is not subscribed. The
|
||||
// stop begins here while a second answerer of the proofs subscribes, between its subscription and its keeping: that one
|
||||
// is taken down and refused too.
|
||||
var once sync.Once
|
||||
bus.SetSubscribedHook(conn, func(string) { once.Do(conn.StopAnswering) })
|
||||
if _, err := conn.SeatAnswer("messenger", "mesh.seat.intake.proof.code.*", func(string, json.RawMessage) (json.RawMessage, error) {
|
||||
return json.RawMessage(`{"raced":true}`), nil
|
||||
}); err == nil {
|
||||
t.Error("a proof answerer that raced the stop was kept, want it refused")
|
||||
}
|
||||
if raw, err := conn.SeatProve("telegram", "mesh.seat.intake.proof.code.telegram", json.RawMessage(`{"code":"123456"}`)); err == nil ||
|
||||
!strings.Contains(err.Error(), "nothing answers") {
|
||||
t.Errorf("a proof after the stop was answered %s, %v; want no responders", raw, err)
|
||||
|
||||
Reference in New Issue
Block a user