fix(dispenser): simplify card preparation and recovery
This commit is contained in:
@@ -9,8 +9,6 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type fakeSequenceDevice struct {
|
||||
@@ -18,6 +16,7 @@ type fakeSequenceDevice struct {
|
||||
commandErrors map[cmdType][]error
|
||||
commands []cmdType
|
||||
commandTimes []time.Time
|
||||
onCommand func(cmdType)
|
||||
}
|
||||
|
||||
func newSequenceTestClient(t *testing.T, statusResponses ...cmdResp) (*Client, *fakeSequenceDevice) {
|
||||
@@ -55,6 +54,9 @@ func newSequenceTestClient(t *testing.T, statusResponses ...cmdResp) (*Client, *
|
||||
case request := <-client.reqCh:
|
||||
device.commands = append(device.commands, request.typ)
|
||||
device.commandTimes = append(device.commandTimes, now)
|
||||
if device.onCommand != nil {
|
||||
device.onCommand(request.typ)
|
||||
}
|
||||
if request.typ == cmdStatus {
|
||||
if len(device.statusResponses) == 0 {
|
||||
request.respCh <- cmdResp{err: errors.New("unexpected status read")}
|
||||
@@ -94,200 +96,6 @@ func commandCount(commands []cmdType, target cmdType) int {
|
||||
return count
|
||||
}
|
||||
|
||||
func prepareFailureResponses(code byte, count int) []cmdResp {
|
||||
responses := make([]cmdResp, count)
|
||||
for i := range responses {
|
||||
responses[i] = cmdResp{status: []byte{code, 0x30, 0x30, 0x30}}
|
||||
}
|
||||
return responses
|
||||
}
|
||||
|
||||
func TestPreparationResetRecovery(t *testing.T) {
|
||||
for _, operation := range []string{"current", "next"} {
|
||||
for _, code := range []byte{0x32, 0x36} {
|
||||
t.Run(operation+string(rune(code)), func(t *testing.T) {
|
||||
responses := prepareFailureResponses(code, 9) // Initial read, seven polls, post-reset read.
|
||||
responses = append(responses, cmdResp{status: status(0x33)})
|
||||
client, device := newSequenceTestClient(t, responses...)
|
||||
// An apparently fresh passive cache must never replace physical reads.
|
||||
client.lastStatus = status(0x33)
|
||||
client.lastStatusT = time.Now()
|
||||
client.statusTTL = time.Hour
|
||||
prepare := client.PrepareCurrentCard
|
||||
if operation == "next" {
|
||||
prepare = client.PrepareNextCard
|
||||
}
|
||||
if _, err := prepare(context.Background()); err != nil {
|
||||
t.Fatalf("prepare(%s, %X) = %v, want success", operation, code, err)
|
||||
}
|
||||
want := []cmdType{cmdStatus, cmdToEncoder}
|
||||
for i := 0; i < 7; i++ {
|
||||
want = append(want, cmdStatus)
|
||||
}
|
||||
want = append(want, cmdReset, cmdStatus, cmdToEncoder, cmdStatus)
|
||||
if !reflect.DeepEqual(device.commands, want) {
|
||||
t.Errorf("prepare commands = %v, want %v", device.commands, want)
|
||||
}
|
||||
if len(device.commandTimes) != len(want) {
|
||||
t.Fatalf("command times = %d, want %d", len(device.commandTimes), len(want))
|
||||
}
|
||||
if elapsed := device.commandTimes[9].Sub(device.commandTimes[1]); elapsed != sequenceRetryAfter {
|
||||
t.Errorf("RS elapsed = %s, want %s", elapsed, sequenceRetryAfter)
|
||||
}
|
||||
if elapsed := device.commandTimes[10].Sub(device.commandTimes[9]); elapsed != sequenceResetWait {
|
||||
t.Errorf("post-reset status elapsed = %s, want %s", elapsed, sequenceResetWait)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPreparationFailureAfterReset(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
code byte
|
||||
wantError string
|
||||
}{
|
||||
{"prepare failure times out", 0x32, "timed out"},
|
||||
{"combined failure is final", 0x36, "Command cannot execute"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
client, device := newSequenceTestClient(t, prepareFailureResponses(test.code, 30)...)
|
||||
_, err := client.PrepareCurrentCard(context.Background())
|
||||
if err == nil || !strings.Contains(err.Error(), test.wantError) {
|
||||
t.Errorf("PrepareCurrentCard(%X) error = %v, want %q", test.code, err, test.wantError)
|
||||
}
|
||||
if resets, commands := commandCount(device.commands, cmdReset), commandCount(device.commands, cmdToEncoder); resets != 1 || commands != 2 {
|
||||
t.Errorf("recovery commands = %v, want one RS and two FC7", device.commands)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationHardErrorsNeverReset(t *testing.T) {
|
||||
for _, failure := range [][]byte{
|
||||
{0x32, 0x30, 0x32, 0x30}, // Jam alongside prepare failure.
|
||||
{0x36, 0x30, 0x34, 0x30}, // Overlap alongside combined failure.
|
||||
{0x32, 0x30, 0x30, 0x38}, // Empty.
|
||||
{0x36, 0x32, 0x30, 0x30}, // Independent dispense error.
|
||||
{0x32, 0x31, 0x30, 0x30}, // Independent capture error.
|
||||
{0x34, 0x30, 0x30, 0x30}, // Command rejection alone.
|
||||
{0x30, 0x30, 0x30, 0x40}, // Unknown.
|
||||
{0x32}, // Malformed.
|
||||
} {
|
||||
responses := prepareFailureResponses(0x32, 7)
|
||||
responses = append(responses, cmdResp{status: failure})
|
||||
client, device := newSequenceTestClient(t, responses...)
|
||||
if _, err := client.PrepareCurrentCard(context.Background()); err == nil {
|
||||
t.Errorf("PrepareCurrentCard(% X) error = nil, want hard error", failure)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdReset); got != 0 {
|
||||
t.Errorf("PrepareCurrentCard(% X) resets = %d, want 0", failure, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationProgressAtRecoveryThreshold(t *testing.T) {
|
||||
for _, moving := range [][]byte{{0x31, 0x30, 0x30, 0x30}, {0x32, 0x38, 0x30, 0x30}} {
|
||||
responses := prepareFailureResponses(0x32, 7)
|
||||
responses = append(responses, cmdResp{status: moving}, cmdResp{status: moving}, cmdResp{status: status(0x33)})
|
||||
client, device := newSequenceTestClient(t, responses...)
|
||||
if _, err := client.PrepareCurrentCard(context.Background()); err != nil {
|
||||
t.Fatalf("PrepareCurrentCard(moving % X) = %v, want success", moving, err)
|
||||
}
|
||||
if commandCount(device.commands, cmdReset) != 0 || commandCount(device.commands, cmdToEncoder) != 1 {
|
||||
t.Errorf("PrepareCurrentCard(moving % X) commands = %v, want initial FC7 only", moving, device.commands)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationRequiresConsecutiveFailures(t *testing.T) {
|
||||
responses := prepareFailureResponses(0x32, 7)
|
||||
responses = append(responses,
|
||||
cmdResp{status: []byte{0x31, 0x30, 0x30, 0x30}},
|
||||
cmdResp{status: []byte{0x32, 0x30, 0x30, 0x30}},
|
||||
cmdResp{status: status(0x33)},
|
||||
)
|
||||
client, device := newSequenceTestClient(t, responses...)
|
||||
if _, err := client.PrepareCurrentCard(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdReset); got != 0 {
|
||||
t.Errorf("PrepareCurrentCard(interrupted failure sequence) resets = %d, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationPostResetStatus(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
responses []cmdResp
|
||||
wantFC7 int
|
||||
wantError string
|
||||
}{
|
||||
{"already at encoder", []cmdResp{{status: status(0x33)}}, 1, ""},
|
||||
{"moving then encoder", []cmdResp{{status: []byte{0x31, 0x30, 0x30, 0x30}}, {status: status(0x33)}}, 1, ""},
|
||||
{"moving then stationary", []cmdResp{{status: []byte{0x31, 0x30, 0x30, 0x30}}, {status: status(0x30)}, {status: status(0x33)}}, 2, ""},
|
||||
{"hard error", []cmdResp{{status: []byte{0x32, 0x30, 0x32, 0x30}}}, 1, "Card jammed"},
|
||||
{"read failure", []cmdResp{{err: errors.New("post-reset read failed")}}, 1, "post-reset read failed"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
responses := append(prepareFailureResponses(0x32, 8), test.responses...)
|
||||
client, device := newSequenceTestClient(t, responses...)
|
||||
_, err := client.PrepareCurrentCard(context.Background())
|
||||
if test.wantError == "" && err != nil {
|
||||
t.Errorf("PrepareCurrentCard(%s) = %v, want success", test.name, err)
|
||||
}
|
||||
if test.wantError != "" && (err == nil || !strings.Contains(err.Error(), test.wantError)) {
|
||||
t.Errorf("PrepareCurrentCard(%s) = %v, want %q", test.name, err, test.wantError)
|
||||
}
|
||||
if commandCount(device.commands, cmdReset) != 1 || commandCount(device.commands, cmdToEncoder) != test.wantFC7 {
|
||||
t.Errorf("PrepareCurrentCard(%s) commands = %v, want one RS and %d FC7", test.name, device.commands, test.wantFC7)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationRecoveryCommandFailures(t *testing.T) {
|
||||
for _, command := range []cmdType{cmdReset, cmdToEncoder} {
|
||||
client, device := newSequenceTestClient(t, prepareFailureResponses(0x32, 9)...)
|
||||
failure := errors.New("command failed")
|
||||
device.commandErrors[command] = []error{failure}
|
||||
wantFC7 := 1
|
||||
if command == cmdToEncoder {
|
||||
device.commandErrors[command] = []error{nil, failure}
|
||||
wantFC7 = 2
|
||||
}
|
||||
_, err := client.PrepareCurrentCard(context.Background())
|
||||
if !errors.Is(err, failure) {
|
||||
t.Errorf("PrepareCurrentCard(failed command %v) = %v, want %v", command, err, failure)
|
||||
}
|
||||
if commandCount(device.commands, cmdReset) != 1 || commandCount(device.commands, cmdToEncoder) != wantFC7 {
|
||||
t.Errorf("PrepareCurrentCard(failed command %v) commands = %v, want one RS and %d FC7", command, device.commands, wantFC7)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationCancellationDuringResetSettle(t *testing.T) {
|
||||
client, device := newSequenceTestClient(t, prepareFailureResponses(0x32, 9)...)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
wait := client.sequenceTiming.wait
|
||||
client.sequenceTiming.wait = func(ctx context.Context, duration time.Duration) error {
|
||||
if duration == sequenceResetWait {
|
||||
cancel()
|
||||
}
|
||||
return wait(ctx, duration)
|
||||
}
|
||||
_, err := client.PrepareCurrentCard(ctx)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Errorf("PrepareCurrentCard(cancel during reset) = %v, want context.Canceled", err)
|
||||
}
|
||||
if commandCount(device.commands, cmdReset) != 1 || commandCount(device.commands, cmdToEncoder) != 1 || device.commands[len(device.commands)-1] != cmdReset {
|
||||
t.Errorf("PrepareCurrentCard(cancel during reset) commands = %v, want no commands after RS", device.commands)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetPacket(t *testing.T) {
|
||||
got := createPacket([]byte{0x30, 0x30}, commandRS)
|
||||
want := []byte{0x02, 0x30, 0x30, 0x00, 0x02, 0x52, 0x53, 0x03, 0x02}
|
||||
@@ -296,234 +104,6 @@ func TestResetPacket(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationDeadlineDuringResetSettle(t *testing.T) {
|
||||
client, device := newSequenceTestClient(t, prepareFailureResponses(0x32, 4)...)
|
||||
wait := client.sequenceTiming.wait
|
||||
waits := 0
|
||||
client.sequenceTiming.wait = func(ctx context.Context, duration time.Duration) error {
|
||||
waits++
|
||||
if waits == 1 {
|
||||
// Simulate a delayed poll, leaving only one second for recovery.
|
||||
duration = 15 * time.Second
|
||||
} else if duration != time.Second {
|
||||
t.Errorf("reset settle duration = %s, want remaining 1s", duration)
|
||||
}
|
||||
return wait(ctx, duration)
|
||||
}
|
||||
_, err := client.PrepareCurrentCard(context.Background())
|
||||
if err == nil || !strings.Contains(err.Error(), "timed out") {
|
||||
t.Errorf("PrepareCurrentCard(late recovery) = %v, want timeout", err)
|
||||
}
|
||||
want := []cmdType{cmdStatus, cmdToEncoder, cmdStatus, cmdStatus, cmdReset}
|
||||
if !reflect.DeepEqual(device.commands, want) {
|
||||
t.Errorf("PrepareCurrentCard(late recovery) commands = %v, want %v", device.commands, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationPlainRetryConsumesRecoveryBudget(t *testing.T) {
|
||||
responses := make([]cmdResp, 8)
|
||||
for i := range responses {
|
||||
responses[i] = cmdResp{status: status(0x34)}
|
||||
}
|
||||
responses = append(responses, prepareFailureResponses(0x32, 20)...)
|
||||
client, device := newSequenceTestClient(t, responses...)
|
||||
_, err := client.PrepareCurrentCard(context.Background())
|
||||
if err == nil || !strings.Contains(err.Error(), "timed out") {
|
||||
t.Errorf("PrepareCurrentCard(failure after plain retry) = %v, want timeout", err)
|
||||
}
|
||||
if commandCount(device.commands, cmdReset) != 0 || commandCount(device.commands, cmdToEncoder) != 2 {
|
||||
t.Errorf("PrepareCurrentCard(failure after plain retry) commands = %v, want two FC7 and no RS", device.commands)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationFirstAttemptDoesNotReset(t *testing.T) {
|
||||
for _, failures := range []int{0, 2} {
|
||||
responses := []cmdResp{{status: status(0x34)}}
|
||||
responses = append(responses, prepareFailureResponses(0x32, failures)...)
|
||||
responses = append(responses, cmdResp{status: status(0x33)})
|
||||
client, device := newSequenceTestClient(t, responses...)
|
||||
if _, err := client.PrepareCurrentCard(context.Background()); err != nil {
|
||||
t.Errorf("PrepareCurrentCard(%d transient failures) = %v, want success", failures, err)
|
||||
}
|
||||
if commandCount(device.commands, cmdReset) != 0 || commandCount(device.commands, cmdToEncoder) != 1 {
|
||||
t.Errorf("PrepareCurrentCard(%d transient failures) commands = %v, want one FC7 and no RS", failures, device.commands)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareCurrentCardAcceptsEncoderPositionWithStaleDiagnostics(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
status []byte
|
||||
wantStock string
|
||||
}{
|
||||
{
|
||||
name: "dispense error and jam",
|
||||
status: []byte{0x30, 0x32, 0x32, 0x33},
|
||||
wantStock: "Card jammed",
|
||||
},
|
||||
{
|
||||
name: "supply diagnostics",
|
||||
status: []byte{0x32, 0x30, 0x31, 0x33},
|
||||
wantStock: "Card pre-empty",
|
||||
},
|
||||
{
|
||||
name: "combined stale jam and supply diagnostics",
|
||||
status: []byte{0x30, 0x32, 0x33, 0x33},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var logOutput bytes.Buffer
|
||||
standardLogger := log.StandardLogger()
|
||||
previousOutput := standardLogger.Out
|
||||
standardLogger.SetOutput(&logOutput)
|
||||
t.Cleanup(func() {
|
||||
standardLogger.SetOutput(previousOutput)
|
||||
})
|
||||
|
||||
client, device := newSequenceTestClient(t, cmdResp{status: test.status})
|
||||
|
||||
stock, err := client.PrepareCurrentCard(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stock != test.wantStock {
|
||||
t.Fatalf("stock status = %q, want %q", stock, test.wantStock)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdToEncoder); got != 0 {
|
||||
t.Fatalf("to-encoder commands = %d, want 0", got)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdStatus); got != 1 {
|
||||
t.Fatalf("status reads = %d, want 1", got)
|
||||
}
|
||||
logged := logOutput.String()
|
||||
if !strings.Contains(logged, "card confirmed at encoder") ||
|
||||
!strings.Contains(logged, statusDescription(test.status)) ||
|
||||
!strings.Contains(logged, "raw status") {
|
||||
t.Fatalf("encoder diagnostic log = %q", logged)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareCurrentCardToleratesTransientPrepareFailureUntilEncoder(t *testing.T) {
|
||||
client, device := newSequenceTestClient(t,
|
||||
cmdResp{status: []byte{0x32, 0x30, 0x30, 0x30}},
|
||||
cmdResp{status: []byte{0x32, 0x30, 0x30, 0x30}},
|
||||
cmdResp{status: []byte{0x32, 0x30, 0x30, 0x30}},
|
||||
cmdResp{status: []byte{0x32, 0x30, 0x30, 0x33}},
|
||||
)
|
||||
|
||||
stock, err := client.PrepareCurrentCard(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stock != "Preparing card fails" {
|
||||
t.Fatalf("stock status = %q, want Preparing card fails", stock)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdToEncoder); got != 1 {
|
||||
t.Fatalf("to-encoder commands = %d, want 1", got)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdStatus); got != 4 {
|
||||
t.Fatalf("status reads = %d, want 4", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareCurrentCardDoesNotMaskHardErrorAlongsideTransientPrepareFailure(t *testing.T) {
|
||||
client, _ := newSequenceTestClient(t,
|
||||
cmdResp{status: []byte{0x32, 0x30, 0x32, 0x30}},
|
||||
)
|
||||
|
||||
_, err := client.PrepareCurrentCard(context.Background())
|
||||
if err == nil || !strings.Contains(err.Error(), "Card jammed") {
|
||||
t.Fatalf("error = %v, want Card jammed", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareCurrentCardPollingAllowsTransientMovementWithStaleErrors(t *testing.T) {
|
||||
client, device := newSequenceTestClient(t,
|
||||
cmdResp{status: status(0x34)},
|
||||
cmdResp{status: []byte{0x31, 0x38, 0x32, 0x30}},
|
||||
cmdResp{status: []byte{0x30, 0x32, 0x32, 0x33}},
|
||||
)
|
||||
|
||||
stock, err := client.PrepareCurrentCard(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stock != "Card jammed" {
|
||||
t.Fatalf("stock status = %q, want Card jammed", stock)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdToEncoder); got != 1 {
|
||||
t.Fatalf("to-encoder commands = %d, want 1", got)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdStatus); got != 3 {
|
||||
t.Fatalf("status reads = %d, want 3", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareCurrentCardRejectsFailureWithoutEncoderOrMovement(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
response cmdResp
|
||||
wantError string
|
||||
wantStock string
|
||||
}{
|
||||
{
|
||||
name: "status read error",
|
||||
response: cmdResp{err: errors.New("serial read failed")},
|
||||
wantError: "serial read failed",
|
||||
},
|
||||
{
|
||||
name: "malformed status",
|
||||
response: cmdResp{status: []byte{0x30, 0x30, 0x31}},
|
||||
wantError: "malformed dispenser status",
|
||||
},
|
||||
{
|
||||
name: "unknown status",
|
||||
response: cmdResp{status: []byte{0x30, 0x30, 0x30, 0x40}},
|
||||
wantError: "unknown dispenser status",
|
||||
},
|
||||
{
|
||||
name: "jammed preparation",
|
||||
response: cmdResp{status: []byte{0x30, 0x30, 0x32, 0x34}},
|
||||
wantError: "Card jammed",
|
||||
wantStock: "Card jammed",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
client, _ := newSequenceTestClient(t, test.response)
|
||||
|
||||
stock, err := client.PrepareCurrentCard(context.Background())
|
||||
if err == nil || !strings.Contains(err.Error(), test.wantError) {
|
||||
t.Fatalf("error = %v, want containing %q", err, test.wantError)
|
||||
}
|
||||
if stock != test.wantStock {
|
||||
t.Fatalf("stock status = %q, want %q", stock, test.wantStock)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareCurrentCardReturnsAuthoritativeEmptyWellError(t *testing.T) {
|
||||
client, device := newSequenceTestClient(t, cmdResp{status: status(0x38)})
|
||||
|
||||
stock, err := client.PrepareCurrentCard(context.Background())
|
||||
if !errors.Is(err, ErrCardWellEmpty) {
|
||||
t.Fatalf("error = %v, want ErrCardWellEmpty", err)
|
||||
}
|
||||
if stock != "Card empty" {
|
||||
t.Fatalf("stock status = %q, want Card empty", stock)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdToEncoder); got != 0 {
|
||||
t.Fatalf("to-encoder commands = %d, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliverCurrentCardSucceedsAfterCommandAcceptanceWithoutStatusPolling(t *testing.T) {
|
||||
staleStatus := []byte{0x30, 0x32, 0x32, 0x33}
|
||||
client, device := newSequenceTestClient(t, cmdResp{status: staleStatus})
|
||||
@@ -586,42 +166,6 @@ func TestDeliverCurrentCardPreservesContextCancellation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareCurrentCardRetriesOnceThenTimesOut(t *testing.T) {
|
||||
responses := make([]cmdResp, 17)
|
||||
for index := range responses {
|
||||
responses[index] = cmdResp{status: status(0x34)}
|
||||
}
|
||||
client, device := newSequenceTestClient(t, responses...)
|
||||
|
||||
_, err := client.PrepareCurrentCard(context.Background())
|
||||
if err == nil || !strings.Contains(err.Error(), "timed out") {
|
||||
t.Fatalf("error = %v, want preparation timeout", err)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdToEncoder); got != 2 {
|
||||
t.Fatalf("to-encoder commands = %d, want initial command plus one halfway retry", got)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdStatus); got != 17 {
|
||||
t.Fatalf("status reads = %d, want 17", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareCurrentCardPropagatesContextCancellation(t *testing.T) {
|
||||
client, _ := newSequenceTestClient(t,
|
||||
cmdResp{status: status(0x34)},
|
||||
cmdResp{status: status(0x34)},
|
||||
)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
client.sequenceTiming.wait = func(context.Context, time.Duration) error {
|
||||
cancel()
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
_, err := client.PrepareCurrentCard(ctx)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("error = %v, want context.Canceled", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBeginPrepareNextCardDispatchesWithoutReadinessPolling(t *testing.T) {
|
||||
client, device := newSequenceTestClient(t)
|
||||
|
||||
@@ -652,160 +196,6 @@ func TestBeginPrepareNextCardReturnsDispatchFailureWithoutPolling(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareCardAtEncoderRequiresConfirmedEncoderState(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
responses []cmdResp
|
||||
wantError string
|
||||
wantToEncoder int
|
||||
}{
|
||||
{
|
||||
name: "already at encoder",
|
||||
responses: []cmdResp{{status: status(0x33)}},
|
||||
wantToEncoder: 0,
|
||||
},
|
||||
{
|
||||
name: "moves ready card to encoder",
|
||||
responses: []cmdResp{
|
||||
{status: status(0x34)},
|
||||
{status: status(0x33)},
|
||||
},
|
||||
wantToEncoder: 1,
|
||||
},
|
||||
{
|
||||
name: "empty card well",
|
||||
responses: []cmdResp{{status: status(0x38)}},
|
||||
wantError: CardWellEmptyMessage,
|
||||
wantToEncoder: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
client, device := newSequenceTestClient(t, test.responses...)
|
||||
|
||||
_, err := client.PrepareNextCard(context.Background())
|
||||
if test.wantError == "" && err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if test.wantError != "" && (err == nil || !strings.Contains(err.Error(), test.wantError)) {
|
||||
t.Fatalf("error = %v, want containing %q", err, test.wantError)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdToEncoder); got != test.wantToEncoder {
|
||||
t.Fatalf("to-encoder commands = %d, want %d", got, test.wantToEncoder)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPositionFlags(t *testing.T) {
|
||||
for position := byte(0x30); position <= 0x3F; position++ {
|
||||
t.Run(fmt.Sprintf("%02X", position), func(t *testing.T) {
|
||||
st := status(position)
|
||||
wantEncoder := position&0x02 != 0
|
||||
wantEmpty := position&0x08 != 0
|
||||
if got := isAtEncoderPosition(st); got != wantEncoder {
|
||||
t.Errorf("isAtEncoderPosition(% X) = %t, want %t", st, got, wantEncoder)
|
||||
}
|
||||
if got := isCardWellEmpty(st); got != wantEmpty {
|
||||
t.Errorf("isCardWellEmpty(% X) = %t, want %t", st, got, wantEmpty)
|
||||
}
|
||||
if err := validateDispenserStatusData(st); err != nil {
|
||||
t.Errorf("validateDispenserStatusData(% X) = %v, want nil", st, err)
|
||||
}
|
||||
ready, err := preparationStatus("test", st, true)
|
||||
if ready != wantEncoder {
|
||||
t.Errorf("preparationStatus(% X) ready = %t, want %t", st, ready, wantEncoder)
|
||||
}
|
||||
wantEmptyError := wantEmpty && !wantEncoder
|
||||
if errors.Is(err, ErrCardWellEmpty) != wantEmptyError || (err != nil && !wantEmptyError) {
|
||||
t.Errorf("preparationStatus(% X) error = %v, want empty error %t", st, err, wantEmptyError)
|
||||
}
|
||||
wantStock := ""
|
||||
if wantEmpty {
|
||||
wantStock = "Card empty"
|
||||
}
|
||||
if got := stockTake(st); got != wantStock {
|
||||
t.Errorf("stockTake(% X) = %q, want %q", st, got, wantStock)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoderPositionPrecedesDiagnostics(t *testing.T) {
|
||||
for _, position := range []byte{0x32, 0x33, 0x36, 0x37, 0x3A, 0x3B, 0x3E, 0x3F} {
|
||||
for _, diagnostics := range [][]byte{
|
||||
{0x30, 0x30, 0x30},
|
||||
{0x32, 0x30, 0x30},
|
||||
{0x36, 0x30, 0x30},
|
||||
{0x34, 0x32, 0x32},
|
||||
{0x30, 0x31, 0x34},
|
||||
{0xFF, 0xFF, 0xFF},
|
||||
} {
|
||||
st := append(append([]byte(nil), diagnostics...), position)
|
||||
for _, operation := range []string{"current", "next"} {
|
||||
t.Run(fmt.Sprintf("%s/% X", operation, st), func(t *testing.T) {
|
||||
client, device := newSequenceTestClient(t, cmdResp{status: st})
|
||||
prepare := client.PrepareCurrentCard
|
||||
if operation == "next" {
|
||||
prepare = client.PrepareNextCard
|
||||
}
|
||||
if _, err := prepare(context.Background()); err != nil {
|
||||
t.Errorf("prepare(% X) = %v, want success", st, err)
|
||||
}
|
||||
want := []cmdType{cmdStatus}
|
||||
if !reflect.DeepEqual(device.commands, want) {
|
||||
t.Errorf("prepare(% X) commands = %v, want %v", st, device.commands, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCombinedEncoderPositionStopsRecovery(t *testing.T) {
|
||||
for _, afterReset := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("afterReset=%t", afterReset), func(t *testing.T) {
|
||||
count := 7 // Encoder confirmation at the recovery threshold, before RS.
|
||||
wantResets := 0
|
||||
if afterReset {
|
||||
count = 8 // Encoder confirmation on the fresh post-reset read.
|
||||
wantResets = 1
|
||||
}
|
||||
responses := append(prepareFailureResponses(0x32, count), cmdResp{status: []byte{0x36, 0x32, 0x34, 0x3F}})
|
||||
client, device := newSequenceTestClient(t, responses...)
|
||||
if _, err := client.PrepareCurrentCard(context.Background()); err != nil {
|
||||
t.Errorf("PrepareCurrentCard(combined encoder) = %v, want success", err)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdReset); got != wantResets {
|
||||
t.Errorf("PrepareCurrentCard(combined encoder) RS count = %d, want %d", got, wantResets)
|
||||
}
|
||||
if got := commandCount(device.commands, cmdToEncoder); got != 1 {
|
||||
t.Errorf("PrepareCurrentCard(combined encoder) FC7 count = %d, want 1", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidPositionEncoding(t *testing.T) {
|
||||
for _, st := range [][]byte{nil, {0x30, 0x30, 0x30}, status(0x02), status(0x2F), status(0x40), status(0x72), status(0xFF)} {
|
||||
if isAtEncoderPosition(st) || isCardWellEmpty(st) {
|
||||
t.Errorf("invalid status % X confirmed encoder or empty, want neither", st)
|
||||
}
|
||||
if ready, err := preparationStatus("test", st, true); ready || err == nil {
|
||||
t.Errorf("preparationStatus(% X) = (%t, %v), want false and error", st, ready, err)
|
||||
}
|
||||
}
|
||||
st := []byte{0xFF, 0xFF, 0xFF, 0x37, 0xFF}
|
||||
if ready, err := preparationStatus("test", st, false); !ready || err != nil {
|
||||
t.Errorf("preparationStatus(% X) = (%t, %v), want encoder precedence", st, ready, err)
|
||||
}
|
||||
st = append(status(0x35), 0xFF)
|
||||
if ready, err := preparationStatus("test", st, true); ready || err == nil {
|
||||
t.Errorf("preparationStatus(% X) = (%t, %v), want malformed error", st, ready, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPositionDescriptions(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
position byte
|
||||
@@ -825,3 +215,305 @@ func TestPositionDescriptions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func positionResponses(position byte, count int) []cmdResp {
|
||||
responses := make([]cmdResp, count)
|
||||
for i := range responses {
|
||||
responses[i] = cmdResp{status: status(position)}
|
||||
}
|
||||
return responses
|
||||
}
|
||||
|
||||
func TestPreparationClasses(t *testing.T) {
|
||||
classes := map[preparationClass][]byte{
|
||||
encoderConfirmed: {0x32, 0x33, 0x36, 0x37, 0x3A, 0x3B, 0x3E, 0x3F},
|
||||
positionUncertain: {0x31, 0x34, 0x35, 0x39, 0x3C, 0x3D},
|
||||
positionWellEmpty: {0x38}, positionNoCard: {0x30},
|
||||
}
|
||||
for want, positions := range classes {
|
||||
for _, position := range positions {
|
||||
st := []byte{0xFF, 0x36, 0x32, position}
|
||||
if got, err := classifyPreparationStatus(st); got != want || err != nil {
|
||||
t.Errorf("classify(% X)=%s,%v, want %s", st, got, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, st := range [][]byte{nil, {0x30}, status(0x02), status(0x40), status(0xFF), {0x30, 0x30, 0x30, 0x37, 0x30}} {
|
||||
if got, err := classifyPreparationStatus(st); err == nil {
|
||||
t.Errorf("classify(% X)=%s,nil, want malformed error", st, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationAllPositionsBothMethods(t *testing.T) {
|
||||
for _, next := range []bool{false, true} {
|
||||
for position := byte(0x30); position <= 0x3F; position++ {
|
||||
t.Run(fmt.Sprintf("next=%t/position=%X", next, position), func(t *testing.T) {
|
||||
responses := positionResponses(position, 40)
|
||||
for i := range responses {
|
||||
responses[i].status[0], responses[i].status[1], responses[i].status[2] = 0xFF, 0x38, 0x34
|
||||
}
|
||||
c, d := newSequenceTestClient(t, responses...)
|
||||
// Passive encoder data must never replace fresh AP.
|
||||
c.lastStatus, c.lastStatusT, c.statusTTL = status(0x33), time.Now(), time.Hour
|
||||
prepare := c.PrepareCurrentCard
|
||||
if next {
|
||||
prepare = c.PrepareNextCard
|
||||
}
|
||||
started := c.now()
|
||||
_, err := prepare(context.Background())
|
||||
wantFC7, wantRS, wantElapsed := 0, 0, time.Duration(0)
|
||||
var wantErr error
|
||||
switch position {
|
||||
case 0x30:
|
||||
wantFC7, wantRS, wantElapsed, wantErr = 4, 3, 18*time.Second, ErrPreparationExhausted
|
||||
case 0x38:
|
||||
wantErr = ErrCardWellEmpty
|
||||
case 0x31, 0x34, 0x35, 0x39, 0x3C, 0x3D:
|
||||
wantFC7, wantElapsed = 1, 4*time.Second
|
||||
}
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Errorf("prepare(%X) error=%v, want %v", position, err, wantErr)
|
||||
}
|
||||
if commandCount(d.commands, cmdToEncoder) != wantFC7 || commandCount(d.commands, cmdReset) != wantRS {
|
||||
t.Errorf("prepare(%X) commands=%v, want FC7=%d RS=%d", position, d.commands, wantFC7, wantRS)
|
||||
}
|
||||
if got := c.now().Sub(started); got != wantElapsed {
|
||||
t.Errorf("prepare(%X) elapsed=%s, want %s", position, got, wantElapsed)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestThreeShakesAreRequestLocalAndSettled(t *testing.T) {
|
||||
c, d := newSequenceTestClient(t, positionResponses(0x30, 34)...)
|
||||
for request := 0; request < 2; request++ {
|
||||
start := len(d.commands)
|
||||
if _, err := c.PrepareCurrentCard(context.Background()); !errors.Is(err, ErrPreparationExhausted) {
|
||||
t.Fatalf("request %d error=%v, want exhausted", request, err)
|
||||
}
|
||||
want := []cmdType{cmdStatus, cmdToEncoder}
|
||||
for attempt := 0; attempt < 4; attempt++ {
|
||||
want = append(want, cmdStatus, cmdStatus, cmdStatus, cmdStatus)
|
||||
if attempt < 3 {
|
||||
want = append(want, cmdReset, cmdToEncoder)
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(d.commands[start:], want) {
|
||||
t.Errorf("request %d commands=%v, want %v", request, d.commands[start:], want)
|
||||
}
|
||||
var lastFC7 time.Time
|
||||
for i := start; i < len(d.commands); i++ {
|
||||
if d.commands[i] == cmdReset && d.commandTimes[i].Sub(lastFC7) != 3*time.Second {
|
||||
t.Errorf("RS at %s after FC7, want 3s", d.commandTimes[i].Sub(lastFC7))
|
||||
}
|
||||
if d.commands[i] == cmdToEncoder {
|
||||
if i > start && d.commands[i-1] == cmdReset && d.commandTimes[i].Sub(d.commandTimes[i-1]) != 2*time.Second {
|
||||
t.Errorf("FC7 settle=%s, want 2s", d.commandTimes[i].Sub(d.commandTimes[i-1]))
|
||||
}
|
||||
lastFC7 = d.commandTimes[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationReclassifiesEverySample(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
positions []byte
|
||||
wantRS int
|
||||
wantTime time.Duration
|
||||
wantErr error
|
||||
}{
|
||||
{"uncertain changes do not restart timer", []byte{0x30, 0x31, 0x34, 0x35, 0x39, 0x3C}, 0, 4 * time.Second, nil},
|
||||
{"uncertain becomes encoder", []byte{0x30, 0x34, 0x37}, 0, time.Second, nil},
|
||||
{"uncertain becomes empty", []byte{0x30, 0x34, 0x38}, 0, time.Second, ErrCardWellEmpty},
|
||||
{"no sensors becomes empty", []byte{0x30, 0x30, 0x38}, 0, time.Second, ErrCardWellEmpty},
|
||||
{"uncertain returns to shake path twice", []byte{0x30, 0x34, 0x30, 0x30, 0x30, 0x34, 0x34, 0x30, 0x30, 0x37}, 2, 10 * time.Second, nil},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var responses []cmdResp
|
||||
for _, p := range tc.positions {
|
||||
responses = append(responses, cmdResp{status: status(p)})
|
||||
}
|
||||
c, d := newSequenceTestClient(t, responses...)
|
||||
start := c.now()
|
||||
_, err := c.PrepareCurrentCard(context.Background())
|
||||
if !errors.Is(err, tc.wantErr) || c.now().Sub(start) != tc.wantTime || commandCount(d.commands, cmdReset) != tc.wantRS {
|
||||
t.Errorf("prepare(%s) error=%v elapsed=%s commands=%v, want error=%v time=%s RS=%d", tc.name, err, c.now().Sub(start), d.commands, tc.wantErr, tc.wantTime, tc.wantRS)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEveryShakeReclassifiesEncoderEmptyAndUncertain(t *testing.T) {
|
||||
for shake := 1; shake <= 3; shake++ {
|
||||
for _, position := range []byte{0x37, 0x38, 0x35} {
|
||||
responses := positionResponses(0x30, 1+4*shake)
|
||||
responses = append(responses, positionResponses(position, 5)...)
|
||||
c, d := newSequenceTestClient(t, responses...)
|
||||
_, err := c.PrepareCurrentCard(context.Background())
|
||||
var wantErr error
|
||||
if position == 0x38 {
|
||||
wantErr = ErrCardWellEmpty
|
||||
}
|
||||
if !errors.Is(err, wantErr) || commandCount(d.commands, cmdReset) != shake || commandCount(d.commands, cmdToEncoder) != shake+1 {
|
||||
t.Errorf("shake %d position=%X error=%v commands=%v, want %v and no further shake", shake, position, err, d.commands, wantErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 commandCount(d.commands, cmdToEncoder) != 1 || commandCount(d.commands, cmdReset) != 0 {
|
||||
t.Errorf("AP failure commands=%v, want FC7 once and no RS", d.commands)
|
||||
}
|
||||
}
|
||||
for _, command := range []cmdType{cmdToEncoder, cmdReset} {
|
||||
c, d := newSequenceTestClient(t, positionResponses(0x30, 10)...)
|
||||
d.commandErrors[command] = []error{failure}
|
||||
if _, err := c.PrepareCurrentCard(context.Background()); !errors.Is(err, failure) {
|
||||
t.Errorf("command %v error=%v, want serial failure", command, err)
|
||||
}
|
||||
if commandCount(d.commands, command) != 1 {
|
||||
t.Errorf("command %v retried: %v", command, d.commands)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationStopsAtCancellationAndDeadlineStages(t *testing.T) {
|
||||
for _, stage := range []string{"initial AP", "FC7", "poll", "RS", "settle"} {
|
||||
for _, callerCanceled := range []bool{false, true} {
|
||||
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())
|
||||
t.Cleanup(cancel)
|
||||
now := c.now()
|
||||
c.sequenceTiming.now = func() time.Time { return now }
|
||||
stoppedAt := -1
|
||||
stop := func() {
|
||||
if stoppedAt >= 0 {
|
||||
return
|
||||
}
|
||||
stoppedAt = len(d.commands)
|
||||
if callerCanceled {
|
||||
cancel()
|
||||
} else {
|
||||
now = now.Add(sequenceTimeout)
|
||||
}
|
||||
}
|
||||
c.sequenceTiming.wait = func(_ context.Context, duration time.Duration) error {
|
||||
now = now.Add(duration)
|
||||
if (stage == "poll" && duration == sequencePollInterval) || (stage == "settle" && duration == sequenceResetWait) {
|
||||
stop()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
d.onCommand = func(cmd cmdType) {
|
||||
if (stage == "initial AP" && cmd == cmdStatus) || (stage == "FC7" && cmd == cmdToEncoder) || (stage == "RS" && cmd == cmdReset) {
|
||||
stop()
|
||||
}
|
||||
}
|
||||
_, err := c.PrepareCurrentCard(ctx)
|
||||
wantErr := error(ErrPreparationExhausted)
|
||||
if callerCanceled {
|
||||
wantErr = context.Canceled
|
||||
} else if stage == "initial AP" {
|
||||
wantErr = context.DeadlineExceeded
|
||||
}
|
||||
if !errors.Is(err, wantErr) || len(d.commands) != stoppedAt {
|
||||
t.Errorf("stage=%s cancel=%t error=%v commands=%v stoppedAt=%d, want %v and no commands after stop", stage, callerCanceled, err, d.commands, stoppedAt, wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPositionFlagsRemainDiagnostic(t *testing.T) {
|
||||
for position := byte(0x30); position <= 0x3F; position++ {
|
||||
st := status(position)
|
||||
if got := isAtEncoderPosition(st); got != (position&0x02 != 0) {
|
||||
t.Errorf("encoder sensor(%X)=%t", position, got)
|
||||
}
|
||||
if got := isCardWellEmpty(st); got != (position&0x08 != 0) {
|
||||
t.Errorf("stock empty flag(%X)=%t", position, got)
|
||||
}
|
||||
wantStock := ""
|
||||
if position&0x08 != 0 {
|
||||
wantStock = "Card empty"
|
||||
}
|
||||
if got := stockTake(st); got != wantStock {
|
||||
t.Errorf("stockTake(%X)=%q, want %q", position, got, wantStock)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationCallerDeadlineIsNotRetryableExhaustion(t *testing.T) {
|
||||
c, d := newSequenceTestClient(t)
|
||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second))
|
||||
defer cancel()
|
||||
_, err := c.PrepareCurrentCard(ctx)
|
||||
if !errors.Is(err, context.DeadlineExceeded) || errors.Is(err, ErrPreparationExhausted) || len(d.commands) != 0 {
|
||||
t.Errorf("expired caller: err=%v commands=%v, want caller deadline and no commands", err, d.commands)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparationExpiredEncoderSampleDoesNotHandoff(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 }
|
||||
d.onCommand = func(cmd cmdType) {
|
||||
if cmd == cmdStatus && commandCount(d.commands, cmdStatus) == 2 {
|
||||
now = now.Add(sequenceTimeout)
|
||||
}
|
||||
}
|
||||
_, err := c.PrepareCurrentCard(context.Background())
|
||||
if !errors.Is(err, ErrPreparationExhausted) {
|
||||
t.Errorf("late encoder sample: err=%v, want preparation exhausted", 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user