fix(dispenser): tolerate unusable AP observations during card preparation
This commit is contained in:
@@ -111,14 +111,11 @@ func TestWorkerClearanceValidation(t *testing.T) {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p := &scriptedTransport{chunks: [][]byte{vendorACK, apReply(tc.st), vendorACK}}
|
||||
c := &Client{port: p, deliveryPending: true, deliveryStarted: time.Now().Add(-deliveryMinimumWait), lastStatus: status(0x30), lastStatusT: time.Now(), statusTTL: time.Hour}
|
||||
r := workerRequest(c, context.Background(), cmdToEncoder)
|
||||
if (r.err == nil) != tc.clear || c.deliveryPending == tc.clear {
|
||||
r := workerRequest(c, context.Background(), cmdStatus)
|
||||
if c.deliveryPending == tc.clear {
|
||||
t.Errorf("FC7(% X): err=%v pending=%v, want clear=%v", tc.st, r.err, c.deliveryPending, tc.clear)
|
||||
}
|
||||
want := []string{"AP"}
|
||||
if tc.clear {
|
||||
want = append(want, "FC7")
|
||||
}
|
||||
if got := wireCommands(p); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("commands=%v, want %v", got, want)
|
||||
}
|
||||
@@ -129,7 +126,7 @@ func TestWorkerClearanceValidation(t *testing.T) {
|
||||
frame[len(frame)-1] ^= 1
|
||||
p := &scriptedTransport{chunks: [][]byte{vendorACK, frame}}
|
||||
c := &Client{port: p, deliveryPending: true}
|
||||
r := workerRequest(c, context.Background(), cmdToEncoder)
|
||||
r := workerRequest(c, context.Background(), cmdStatus)
|
||||
if r.err == nil || !c.deliveryPending {
|
||||
t.Errorf("bad BCC: err=%v pending=%v", r.err, c.deliveryPending)
|
||||
}
|
||||
@@ -235,71 +232,6 @@ func TestDeliveryClearWaitsTwoSeconds(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliveryClearanceWaitStops(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
response cmdResp
|
||||
cancel bool
|
||||
timeout bool
|
||||
}{
|
||||
{name: "cancel", response: cmdResp{status: status(0x37), deliveryPending: true}, cancel: true},
|
||||
{name: "timeout", response: cmdResp{status: status(0x37), deliveryPending: true}, timeout: true},
|
||||
{name: "transport", response: cmdResp{err: io.ErrUnexpectedEOF}},
|
||||
{name: "movement", response: cmdResp{status: []byte{0x31, 0x30, 0x32, 0x30}, deliveryPending: true}},
|
||||
{name: "empty", response: cmdResp{status: status(0x38), deliveryPending: true}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
responses := make([]cmdResp, 20)
|
||||
for i := range responses {
|
||||
responses[i] = tc.response
|
||||
}
|
||||
c, d := newSequenceTestClient(t, responses...)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
if tc.cancel {
|
||||
c.sequenceTiming.wait = func(context.Context, time.Duration) error { cancel(); return ctx.Err() }
|
||||
}
|
||||
started := c.sequenceTiming.now()
|
||||
_, err := c.PrepareCurrentCard(ctx)
|
||||
if err == nil {
|
||||
t.Fatal("preparation succeeded while clearance unavailable")
|
||||
}
|
||||
if tc.cancel && !errors.Is(err, context.Canceled) {
|
||||
t.Errorf("error=%v, want canceled", err)
|
||||
}
|
||||
if tc.timeout {
|
||||
if !errors.Is(err, context.DeadlineExceeded) || c.sequenceTiming.now().Sub(started) != deliveryClearanceTimeout {
|
||||
t.Errorf("timeout err=%v elapsed=%s", err, c.sequenceTiming.now().Sub(started))
|
||||
}
|
||||
}
|
||||
for _, cmd := range d.commands {
|
||||
if cmd != cmdStatus {
|
||||
t.Errorf("clearance dispatched command %v, want only AP", cmd)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliveryClearanceDoesNotConsumeRecoveryWindow(t *testing.T) {
|
||||
responses := make([]cmdResp, 15)
|
||||
for i := range responses {
|
||||
responses[i] = cmdResp{status: status(0x37), deliveryPending: true}
|
||||
}
|
||||
responses = append(responses, positionResponses(0x30, 17)...)
|
||||
c, d := newSequenceTestClient(t, responses...)
|
||||
start := c.now()
|
||||
if _, err := c.PrepareCurrentCard(context.Background()); !errors.Is(err, ErrPreparationExhausted) {
|
||||
t.Fatalf("preparation error=%v, want exhausted", err)
|
||||
}
|
||||
if elapsed := c.now().Sub(start); elapsed != 33*time.Second {
|
||||
t.Errorf("elapsed=%s, want clearance 15s + shakes 18s", elapsed)
|
||||
}
|
||||
if commandCount(d.commands, cmdReset) != 3 || commandCount(d.commands, cmdToEncoder) != 4 {
|
||||
t.Errorf("commands=%v, want 3 RS and 4 FC7", d.commands)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliveryPendingIsNotPersisted(t *testing.T) {
|
||||
c := NewClient(nil, 1)
|
||||
defer c.Close()
|
||||
@@ -328,32 +260,6 @@ func TestDeliveryCancellationAfterACKDoesNotSetPending(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliveryClearanceRetainsFullPreparationTimeout(t *testing.T) {
|
||||
responses := make([]cmdResp, 8)
|
||||
for i := range responses {
|
||||
responses[i] = cmdResp{status: status(0x37), deliveryPending: true}
|
||||
}
|
||||
responses = append(responses, positionResponses(0x30, 2)...)
|
||||
c, d := newSequenceTestClient(t, responses...)
|
||||
wait := c.sequenceTiming.wait
|
||||
c.sequenceTiming.wait = func(ctx context.Context, duration time.Duration) error {
|
||||
if commandCount(d.commands, cmdToEncoder) > 0 {
|
||||
duration = sequenceTimeout
|
||||
}
|
||||
return wait(ctx, duration)
|
||||
}
|
||||
start := c.now()
|
||||
if _, err := c.PrepareCurrentCard(context.Background()); !errors.Is(err, ErrPreparationExhausted) {
|
||||
t.Fatalf("preparation error=%v, want exhausted", err)
|
||||
}
|
||||
if elapsed := c.now().Sub(start); elapsed != 8*time.Second+sequenceTimeout {
|
||||
t.Errorf("elapsed=%s, want clearance 8s + preparation 32s", elapsed)
|
||||
}
|
||||
if commandCount(d.commands, cmdReset) != 0 || commandCount(d.commands, cmdToEncoder) != 1 {
|
||||
t.Errorf("commands=%v, want one FC7 and no RS after deadline", d.commands)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkerSerializesClearanceAndFC7(t *testing.T) {
|
||||
p := &scriptedTransport{chunks: [][]byte{vendorACK, apReply(status(0x30)), vendorACK, vendorACK, vendorACK, apReply(status(0x37))}}
|
||||
c, _ := deliveryWorkerClient(t, p)
|
||||
@@ -388,10 +294,8 @@ func TestWorkerSerializesClearanceAndFC7(t *testing.T) {
|
||||
if r := <-fc0; r.err != nil {
|
||||
t.Fatal(r.err)
|
||||
}
|
||||
if err := c.ToEncoder(ctx); !errors.Is(err, errDeliveryPending) {
|
||||
t.Errorf("FC7 after queued FC0=%v, want deferred", err)
|
||||
}
|
||||
want := []string{"AP", "FC7", "FC0", "AP"}
|
||||
|
||||
want := []string{"AP", "FC7", "FC0"}
|
||||
if got := wireCommands(p); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("serialized commands=%v, want %v", got, want)
|
||||
}
|
||||
@@ -426,10 +330,10 @@ func TestPreparationWireFailuresNeverShake(t *testing.T) {
|
||||
p.chunks = append(p.chunks, frame)
|
||||
}
|
||||
c, _ := deliveryWorkerClient(t, p)
|
||||
if _, err := c.PrepareCurrentCard(context.Background()); err == nil || errors.Is(err, ErrPreparationExhausted) {
|
||||
t.Errorf("frame % X: err=%v, want transport/protocol failure", frame, err)
|
||||
if _, err := c.PrepareCurrentCard(context.Background()); err != nil {
|
||||
t.Errorf("frame % X: err=%v, want encoder opportunity", frame, err)
|
||||
}
|
||||
want := []string{"AP", "FC7", "AP"}
|
||||
want := []string{"AP", "FC7", "AP", "AP", "AP", "AP", "AP"}
|
||||
if got := wireCommands(p); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("frame % X commands=%v, want %v without RS or resends", frame, got, want)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user