fix(dispenser): tolerate unusable AP observations during card preparation

This commit is contained in:
2026-09-28 19:43:35 +01:00
parent e26331ffa9
commit b9b2a524dd
8 changed files with 403 additions and 248 deletions
+16 -40
View File
@@ -52,6 +52,10 @@ func newSequenceTestClient(t *testing.T, statusResponses ...cmdResp) (*Client, *
case <-client.done:
return
case request := <-client.reqCh:
clearance := request.typ == cmdDeliveryClearance
if clearance {
request.typ = cmdStatus
}
device.commands = append(device.commands, request.typ)
device.commandTimes = append(device.commandTimes, now)
if device.onCommand != nil {
@@ -63,6 +67,10 @@ func newSequenceTestClient(t *testing.T, statusResponses ...cmdResp) (*Client, *
continue
}
response := device.statusResponses[0]
if clearance && request.ctx.Err() == nil {
response.status = usableObservation(response.status, response.err, "fake clearance")
response.err = nil
}
device.statusResponses = device.statusResponses[1:]
request.respCh <- response
continue
@@ -369,8 +377,8 @@ func TestPreparationTransportAndDispatchFailuresDoNotShake(t *testing.T) {
failure := errors.New("serial failure")
for _, response := range []cmdResp{{err: failure}, {status: []byte{0x30}}, {status: status(0x40)}} {
c, d := newSequenceTestClient(t, cmdResp{status: status(0x30)}, response)
if _, err := c.PrepareCurrentCard(context.Background()); err == nil || errors.Is(err, ErrPreparationExhausted) {
t.Errorf("AP failure %v error=%v, want transport/malformed error", response, err)
if _, err := c.PrepareCurrentCard(context.Background()); err != nil {
t.Errorf("AP failure %v error=%v, want encoder opportunity", response, err)
}
if commandCount(d.commands, cmdToEncoder) != 1 || commandCount(d.commands, cmdReset) != 0 {
t.Errorf("AP failure commands=%v, want FC7 once and no RS", d.commands)
@@ -391,6 +399,9 @@ func TestPreparationTransportAndDispatchFailuresDoNotShake(t *testing.T) {
func TestPreparationStopsAtCancellationAndDeadlineStages(t *testing.T) {
for _, stage := range []string{"initial AP", "FC7", "poll", "RS", "settle"} {
for _, callerCanceled := range []bool{false, true} {
if stage == "initial AP" && !callerCanceled {
continue
}
t.Run(fmt.Sprintf("%s/cancel=%t", stage, callerCanceled), func(t *testing.T) {
c, d := newSequenceTestClient(t, positionResponses(0x30, 30)...)
ctx, cancel := context.WithCancel(context.Background())
@@ -465,7 +476,7 @@ func TestPreparationCallerDeadlineIsNotRetryableExhaustion(t *testing.T) {
}
}
func TestPreparationExpiredEncoderSampleDoesNotHandoff(t *testing.T) {
func TestPreparationExpiredEncoderSampleHandsOff(t *testing.T) {
c, d := newSequenceTestClient(t, cmdResp{status: status(0x30)}, cmdResp{status: status(0x37)})
now := c.now()
c.sequenceTiming.now = func() time.Time { return now }
@@ -475,45 +486,10 @@ func TestPreparationExpiredEncoderSampleDoesNotHandoff(t *testing.T) {
}
}
_, err := c.PrepareCurrentCard(context.Background())
if !errors.Is(err, ErrPreparationExhausted) {
t.Errorf("late encoder sample: err=%v, want preparation exhausted", err)
if err != nil {
t.Errorf("late encoder sample: err=%v, want handoff", err)
}
if len(d.commands) != 3 {
t.Errorf("late encoder sample commands=%v, want AP FC7 AP", d.commands)
}
}
func TestBeginPrepareNextCardStopsWhileDeliveryPending(t *testing.T) {
for _, mode := range []string{"cancel", "deadline", "clearance timeout", "transport failure"} {
t.Run(mode, func(t *testing.T) {
c, d := newSequenceTestClient(t)
d.commandErrors[cmdToEncoder] = []error{errDeliveryPending}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wantErr := error(context.Canceled)
c.sequenceTiming.wait = func(context.Context, time.Duration) error {
cancel()
return context.Canceled
}
switch mode {
case "deadline":
wantErr = context.DeadlineExceeded
c.sequenceTiming.wait = func(context.Context, time.Duration) error { return context.DeadlineExceeded }
case "clearance timeout":
wantErr = context.DeadlineExceeded
now := c.now()
c.sequenceTiming.now = func() time.Time { return now }
c.sequenceTiming.wait = func(context.Context, time.Duration) error { now = now.Add(deliveryClearanceTimeout); return nil }
case "transport failure":
wantErr = errors.New("AP read failed")
d.commandErrors[cmdToEncoder] = []error{wantErr}
}
if err := c.BeginPrepareNextCard(ctx); !errors.Is(err, wantErr) {
t.Errorf("BeginPrepareNextCard(%s) = %v, want %v", mode, err, wantErr)
}
if !reflect.DeepEqual(d.commands, []cmdType{cmdToEncoder}) {
t.Errorf("BeginPrepareNextCard(%s) commands = %v, want one guarded FC7 request", mode, d.commands)
}
})
}
}