fix(dispenser): simplify card preparation and recovery
This commit is contained in:
@@ -140,6 +140,13 @@ func TestIssueDoorCardPhysicalOutcomeContract(t *testing.T) {
|
||||
wantCalls: []string{"prepare current"},
|
||||
wantCardWell: "Card empty",
|
||||
},
|
||||
{
|
||||
name: "exhausted preparation is retryable without encoding or delivery",
|
||||
dispenser: fakeDoorCardDispenser{prepareCurrent: dispenserCallResult{err: errors.Join(errors.New("preparation"), dispenser.ErrPreparationExhausted)}},
|
||||
wantHTTP: http.StatusBadGateway,
|
||||
wantMessage: "preparation\ncard preparation exhausted",
|
||||
wantCalls: []string{"prepare current"},
|
||||
},
|
||||
{
|
||||
name: "encoding and accepted delivery command succeed",
|
||||
wantHTTP: http.StatusOK,
|
||||
@@ -148,13 +155,13 @@ func TestIssueDoorCardPhysicalOutcomeContract(t *testing.T) {
|
||||
wantLockSequence: 1,
|
||||
},
|
||||
{
|
||||
name: "successful encoding with delivery command failure is unavailable",
|
||||
name: "successful encoding with delivery command failure still prestages and succeeds",
|
||||
dispenser: fakeDoorCardDispenser{
|
||||
deliverCurrent: dispenserCallResult{status: "Card jammed", err: errors.New("delivery jammed")},
|
||||
},
|
||||
wantHTTP: http.StatusServiceUnavailable,
|
||||
wantMessage: "Card delivery could not be confirmed",
|
||||
wantCalls: []string{"prepare current", "deliver current"},
|
||||
wantHTTP: http.StatusOK,
|
||||
wantMessage: "Card issued successfully",
|
||||
wantCalls: []string{"prepare current", "deliver current", "begin prepare next"},
|
||||
wantLockSequence: 1,
|
||||
wantCardWell: "Card jammed",
|
||||
},
|
||||
@@ -179,36 +186,35 @@ func TestIssueDoorCardPhysicalOutcomeContract(t *testing.T) {
|
||||
wantLockSequence: 1,
|
||||
},
|
||||
{
|
||||
name: "encoding failure with safe recovery remains retryable",
|
||||
name: "encoding failure ends after delivery",
|
||||
lockErr: encodingErr,
|
||||
wantHTTP: http.StatusBadGateway,
|
||||
wantMessage: encodingErr.Error(),
|
||||
wantCalls: []string{"prepare current", "deliver current", "prepare next"},
|
||||
wantCalls: []string{"prepare current", "deliver current"},
|
||||
wantLockSequence: 1,
|
||||
},
|
||||
{
|
||||
name: "encoding failure with failed-card delivery failure is unavailable",
|
||||
name: "encoding failure remains retryable despite FC0 failure",
|
||||
dispenser: fakeDoorCardDispenser{
|
||||
deliverCurrent: dispenserCallResult{status: "Card jammed", err: errors.New("delivery jammed")},
|
||||
},
|
||||
lockErr: encodingErr,
|
||||
wantHTTP: http.StatusServiceUnavailable,
|
||||
wantMessage: "Dispenser recovery failed; another encoding attempt is not safe",
|
||||
wantHTTP: http.StatusBadGateway,
|
||||
wantMessage: encodingErr.Error(),
|
||||
wantCalls: []string{"prepare current", "deliver current"},
|
||||
wantLockSequence: 1,
|
||||
wantCardWell: "Card jammed",
|
||||
},
|
||||
{
|
||||
name: "encoding failure with empty card well has stable message",
|
||||
name: "encoding failure never prepares next card",
|
||||
dispenser: fakeDoorCardDispenser{
|
||||
prepareNext: dispenserCallResult{status: "Card empty", err: dispenser.ErrCardWellEmpty},
|
||||
},
|
||||
lockErr: encodingErr,
|
||||
wantHTTP: http.StatusServiceUnavailable,
|
||||
wantMessage: dispenser.CardWellEmptyMessage,
|
||||
wantCalls: []string{"prepare current", "deliver current", "prepare next"},
|
||||
wantHTTP: http.StatusBadGateway,
|
||||
wantMessage: encodingErr.Error(),
|
||||
wantCalls: []string{"prepare current", "deliver current"},
|
||||
wantLockSequence: 1,
|
||||
wantCardWell: "Card empty",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -321,3 +327,29 @@ func TestIssueDoorCardLogsNextCardDispatchFailureAndStillSucceeds(t *testing.T)
|
||||
t.Fatalf("dispatch failure log = %q", logged)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssueDoorCardLogsDeliveryFailureWithoutReplacingEncoderOutcome(t *testing.T) {
|
||||
for _, encodingErr := range []error{nil, errors.New("original encoder failure")} {
|
||||
var output bytes.Buffer
|
||||
logger := log.StandardLogger()
|
||||
previous := logger.Out
|
||||
logger.SetOutput(&output)
|
||||
d := &fakeDoorCardDispenser{deliverCurrent: dispenserCallResult{err: errors.New("FC0 dispatch failed")}}
|
||||
lock := &fakeDoorCardLockServer{sequenceErr: encodingErr}
|
||||
recorder, response, _ := performIssueDoorCardRequest(t, d, lock)
|
||||
logger.SetOutput(previous)
|
||||
wantHTTP := http.StatusOK
|
||||
if encodingErr != nil {
|
||||
wantHTTP = http.StatusBadGateway
|
||||
}
|
||||
if recorder.Code != wantHTTP {
|
||||
t.Errorf("issueDoorCard(%v) HTTP = %d, want %d", encodingErr, recorder.Code, wantHTTP)
|
||||
}
|
||||
if encodingErr != nil && response.Message != encodingErr.Error() {
|
||||
t.Errorf("issueDoorCard message = %q, want %q", response.Message, encodingErr.Error())
|
||||
}
|
||||
if !strings.Contains(output.String(), "FC0 dispatch failed") || !strings.Contains(output.String(), "Card delivery") {
|
||||
t.Errorf("issueDoorCard delivery log = %q, want FC0 failure and Card delivery", output.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user