|
| 1 | +package request |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "go.uber.org/mock/gomock" |
| 11 | + |
| 12 | + "github.com/uber/submitqueue/entity" |
| 13 | + "github.com/uber/submitqueue/extension/storage" |
| 14 | + storagemock "github.com/uber/submitqueue/extension/storage/mock" |
| 15 | +) |
| 16 | + |
| 17 | +func TestGetCurrentStateFromRequestLog(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + logs []entity.RequestLog |
| 21 | + expected CurrentState |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "single record", |
| 25 | + logs: []entity.RequestLog{ |
| 26 | + {RequestID: "q/1", TimestampMs: 1000, Status: "new", RequestVersion: 1, LastError: "", Metadata: map[string]string{}}, |
| 27 | + }, |
| 28 | + expected: CurrentState{Status: "new", LastError: "", Metadata: map[string]string{}}, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "terminal status wins over later non-terminal", |
| 32 | + logs: []entity.RequestLog{ |
| 33 | + {RequestID: "q/1", TimestampMs: 1000, Status: "new", RequestVersion: 1, LastError: "", Metadata: map[string]string{}}, |
| 34 | + {RequestID: "q/1", TimestampMs: 2000, Status: "landed", RequestVersion: 3, LastError: "", Metadata: map[string]string{"batch": "b1"}}, |
| 35 | + {RequestID: "q/1", TimestampMs: 3000, Status: "processing", RequestVersion: 0, LastError: "", Metadata: map[string]string{}}, |
| 36 | + }, |
| 37 | + expected: CurrentState{Status: "landed", LastError: "", Metadata: map[string]string{"batch": "b1"}}, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "terminal error status with last error", |
| 41 | + logs: []entity.RequestLog{ |
| 42 | + {RequestID: "q/1", TimestampMs: 1000, Status: "new", RequestVersion: 1, LastError: "", Metadata: map[string]string{}}, |
| 43 | + {RequestID: "q/1", TimestampMs: 2000, Status: "error", RequestVersion: 4, LastError: "merge conflict", Metadata: map[string]string{"step": "merge"}}, |
| 44 | + }, |
| 45 | + expected: CurrentState{Status: "error", LastError: "merge conflict", Metadata: map[string]string{"step": "merge"}}, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "multiple terminal records picks highest version", |
| 49 | + logs: []entity.RequestLog{ |
| 50 | + {RequestID: "q/1", TimestampMs: 1000, Status: "error", RequestVersion: 2, LastError: "timeout", Metadata: map[string]string{}}, |
| 51 | + {RequestID: "q/1", TimestampMs: 2000, Status: "landed", RequestVersion: 5, LastError: "", Metadata: map[string]string{"final": "true"}}, |
| 52 | + {RequestID: "q/1", TimestampMs: 3000, Status: "error", RequestVersion: 3, LastError: "conflict", Metadata: map[string]string{}}, |
| 53 | + }, |
| 54 | + expected: CurrentState{Status: "landed", LastError: "", Metadata: map[string]string{"final": "true"}}, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "same version terminal records uses timestamp tiebreaker", |
| 58 | + logs: []entity.RequestLog{ |
| 59 | + {RequestID: "q/1", TimestampMs: 1000, Status: "error", RequestVersion: 3, LastError: "first", Metadata: map[string]string{}}, |
| 60 | + {RequestID: "q/1", TimestampMs: 2000, Status: "error", RequestVersion: 3, LastError: "second", Metadata: map[string]string{}}, |
| 61 | + }, |
| 62 | + expected: CurrentState{Status: "error", LastError: "second", Metadata: map[string]string{}}, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "terminal status without version is not terminal", |
| 66 | + logs: []entity.RequestLog{ |
| 67 | + {RequestID: "q/1", TimestampMs: 1000, Status: "landed", RequestVersion: 0, LastError: "", Metadata: map[string]string{}}, |
| 68 | + {RequestID: "q/1", TimestampMs: 2000, Status: "processing", RequestVersion: 0, LastError: "", Metadata: map[string]string{"source": "gw"}}, |
| 69 | + }, |
| 70 | + expected: CurrentState{Status: "processing", LastError: "", Metadata: map[string]string{"source": "gw"}}, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "no terminal records falls back to latest timestamp", |
| 74 | + logs: []entity.RequestLog{ |
| 75 | + {RequestID: "q/1", TimestampMs: 1000, Status: "new", RequestVersion: 1, LastError: "", Metadata: map[string]string{}}, |
| 76 | + {RequestID: "q/1", TimestampMs: 3000, Status: "validated", RequestVersion: 2, LastError: "", Metadata: map[string]string{}}, |
| 77 | + {RequestID: "q/1", TimestampMs: 2000, Status: "processing", RequestVersion: 0, LastError: "", Metadata: map[string]string{}}, |
| 78 | + }, |
| 79 | + expected: CurrentState{Status: "validated", LastError: "", Metadata: map[string]string{}}, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + ctrl := gomock.NewController(t) |
| 86 | + mockStore := storagemock.NewMockRequestLogStore(ctrl) |
| 87 | + mockStore.EXPECT().List(gomock.Any(), "q/1").Return(tt.logs, nil) |
| 88 | + |
| 89 | + result, err := GetCurrentStateFromRequestLog(context.Background(), mockStore, "q/1") |
| 90 | + require.NoError(t, err) |
| 91 | + assert.Equal(t, tt.expected, result) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestGetCurrentStateFromRequestLog_NoRecords(t *testing.T) { |
| 97 | + ctrl := gomock.NewController(t) |
| 98 | + mockStore := storagemock.NewMockRequestLogStore(ctrl) |
| 99 | + mockStore.EXPECT().List(gomock.Any(), "q/1").Return(nil, storage.ErrNotFound) |
| 100 | + |
| 101 | + _, err := GetCurrentStateFromRequestLog(context.Background(), mockStore, "q/1") |
| 102 | + assert.Error(t, err) |
| 103 | + assert.True(t, storage.IsNotFound(err)) |
| 104 | +} |
| 105 | + |
| 106 | +func TestGetCurrentStateFromRequestLog_StoreError(t *testing.T) { |
| 107 | + ctrl := gomock.NewController(t) |
| 108 | + mockStore := storagemock.NewMockRequestLogStore(ctrl) |
| 109 | + mockStore.EXPECT().List(gomock.Any(), "q/1").Return(nil, fmt.Errorf("db connection lost")) |
| 110 | + |
| 111 | + _, err := GetCurrentStateFromRequestLog(context.Background(), mockStore, "q/1") |
| 112 | + assert.Error(t, err) |
| 113 | +} |
0 commit comments