|
| 1 | +// Copyright (c) 2026 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package batch |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "go.uber.org/mock/gomock" |
| 25 | + |
| 26 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 27 | + "github.com/uber/submitqueue/submitqueue/extension/storage" |
| 28 | + storagemock "github.com/uber/submitqueue/submitqueue/extension/storage/mock" |
| 29 | +) |
| 30 | + |
| 31 | +const testQueue = "monorepo" |
| 32 | + |
| 33 | +// record builds a QueueBatchState for testQueue. |
| 34 | +func record(state entity.BatchState, batchID string) entity.QueueBatchState { |
| 35 | + return entity.QueueBatchState{Queue: testQueue, State: state, BatchID: batchID} |
| 36 | +} |
| 37 | + |
| 38 | +// batchIn builds a hydrated Batch for testQueue in the given state. |
| 39 | +func batchIn(id string, state entity.BatchState) entity.Batch { |
| 40 | + return entity.Batch{ID: id, Queue: testQueue, State: state, Version: 1} |
| 41 | +} |
| 42 | + |
| 43 | +func TestListByStates(t *testing.T) { |
| 44 | + storeErr := errors.New("storage failed") |
| 45 | + |
| 46 | + tests := map[string]struct { |
| 47 | + states []entity.BatchState |
| 48 | + setup func(*storagemock.MockBatchStore, *storagemock.MockQueueBatchStateStore) |
| 49 | + want []entity.Batch |
| 50 | + wantErr error |
| 51 | + }{ |
| 52 | + "empty states lists nothing": { |
| 53 | + states: nil, |
| 54 | + setup: func(*storagemock.MockBatchStore, *storagemock.MockQueueBatchStateStore) {}, |
| 55 | + }, |
| 56 | + "hydrates every bucket and dedupes across them": { |
| 57 | + states: []entity.BatchState{entity.BatchStateCreated, entity.BatchStateSpeculating}, |
| 58 | + setup: func(batchStore *storagemock.MockBatchStore, recordStore *storagemock.MockQueueBatchStateStore) { |
| 59 | + // b2 appears in both buckets (mid-move duplicate): it must be hydrated |
| 60 | + // and returned exactly once. |
| 61 | + recordStore.EXPECT().List(gomock.Any(), testQueue, entity.BatchStateCreated). |
| 62 | + Return([]entity.QueueBatchState{record(entity.BatchStateCreated, "b1"), record(entity.BatchStateCreated, "b2")}, nil) |
| 63 | + recordStore.EXPECT().List(gomock.Any(), testQueue, entity.BatchStateSpeculating). |
| 64 | + Return([]entity.QueueBatchState{record(entity.BatchStateSpeculating, "b2"), record(entity.BatchStateSpeculating, "b3")}, nil) |
| 65 | + batchStore.EXPECT().Get(gomock.Any(), "b1").Return(batchIn("b1", entity.BatchStateCreated), nil) |
| 66 | + batchStore.EXPECT().Get(gomock.Any(), "b2").Return(batchIn("b2", entity.BatchStateSpeculating), nil) |
| 67 | + batchStore.EXPECT().Get(gomock.Any(), "b3").Return(batchIn("b3", entity.BatchStateSpeculating), nil) |
| 68 | + }, |
| 69 | + want: []entity.Batch{ |
| 70 | + batchIn("b1", entity.BatchStateCreated), |
| 71 | + batchIn("b2", entity.BatchStateSpeculating), |
| 72 | + batchIn("b3", entity.BatchStateSpeculating), |
| 73 | + }, |
| 74 | + }, |
| 75 | + "classifies by hydrated state, not by bucket": { |
| 76 | + states: []entity.BatchState{entity.BatchStateCreated}, |
| 77 | + setup: func(batchStore *storagemock.MockBatchStore, recordStore *storagemock.MockQueueBatchStateStore) { |
| 78 | + // A stale record files b1 under created, but the batch has moved on to |
| 79 | + // speculating — a state outside the requested set, so it is dropped. |
| 80 | + recordStore.EXPECT().List(gomock.Any(), testQueue, entity.BatchStateCreated). |
| 81 | + Return([]entity.QueueBatchState{record(entity.BatchStateCreated, "b1")}, nil) |
| 82 | + batchStore.EXPECT().Get(gomock.Any(), "b1").Return(batchIn("b1", entity.BatchStateSpeculating), nil) |
| 83 | + }, |
| 84 | + }, |
| 85 | + "stale bucket still surfaces a batch whose true state is requested": { |
| 86 | + states: []entity.BatchState{entity.BatchStateCreated, entity.BatchStateSpeculating}, |
| 87 | + setup: func(batchStore *storagemock.MockBatchStore, recordStore *storagemock.MockQueueBatchStateStore) { |
| 88 | + // Only a stale created record exists for b1, but its hydrated state is |
| 89 | + // speculating — requested, so the batch is returned under its true state. |
| 90 | + recordStore.EXPECT().List(gomock.Any(), testQueue, entity.BatchStateCreated). |
| 91 | + Return([]entity.QueueBatchState{record(entity.BatchStateCreated, "b1")}, nil) |
| 92 | + recordStore.EXPECT().List(gomock.Any(), testQueue, entity.BatchStateSpeculating). |
| 93 | + Return(nil, nil) |
| 94 | + batchStore.EXPECT().Get(gomock.Any(), "b1").Return(batchIn("b1", entity.BatchStateSpeculating), nil) |
| 95 | + }, |
| 96 | + want: []entity.Batch{batchIn("b1", entity.BatchStateSpeculating)}, |
| 97 | + }, |
| 98 | + "duplicate input states are listed once": { |
| 99 | + states: []entity.BatchState{entity.BatchStateCreated, entity.BatchStateCreated}, |
| 100 | + setup: func(batchStore *storagemock.MockBatchStore, recordStore *storagemock.MockQueueBatchStateStore) { |
| 101 | + recordStore.EXPECT().List(gomock.Any(), testQueue, entity.BatchStateCreated). |
| 102 | + Return([]entity.QueueBatchState{record(entity.BatchStateCreated, "b1")}, nil). |
| 103 | + Times(1) |
| 104 | + batchStore.EXPECT().Get(gomock.Any(), "b1").Return(batchIn("b1", entity.BatchStateCreated), nil) |
| 105 | + }, |
| 106 | + want: []entity.Batch{batchIn("b1", entity.BatchStateCreated)}, |
| 107 | + }, |
| 108 | + "list failure surfaces": { |
| 109 | + states: []entity.BatchState{entity.BatchStateCreated}, |
| 110 | + setup: func(batchStore *storagemock.MockBatchStore, recordStore *storagemock.MockQueueBatchStateStore) { |
| 111 | + recordStore.EXPECT().List(gomock.Any(), testQueue, entity.BatchStateCreated).Return(nil, storeErr) |
| 112 | + }, |
| 113 | + wantErr: storeErr, |
| 114 | + }, |
| 115 | + "hydrate failure surfaces": { |
| 116 | + states: []entity.BatchState{entity.BatchStateCreated}, |
| 117 | + setup: func(batchStore *storagemock.MockBatchStore, recordStore *storagemock.MockQueueBatchStateStore) { |
| 118 | + recordStore.EXPECT().List(gomock.Any(), testQueue, entity.BatchStateCreated). |
| 119 | + Return([]entity.QueueBatchState{record(entity.BatchStateCreated, "b1")}, nil) |
| 120 | + batchStore.EXPECT().Get(gomock.Any(), "b1").Return(entity.Batch{}, storeErr) |
| 121 | + }, |
| 122 | + wantErr: storeErr, |
| 123 | + }, |
| 124 | + "dangling record is an error, not a skip": { |
| 125 | + states: []entity.BatchState{entity.BatchStateCreated}, |
| 126 | + setup: func(batchStore *storagemock.MockBatchStore, recordStore *storagemock.MockQueueBatchStateStore) { |
| 127 | + recordStore.EXPECT().List(gomock.Any(), testQueue, entity.BatchStateCreated). |
| 128 | + Return([]entity.QueueBatchState{record(entity.BatchStateCreated, "b1")}, nil) |
| 129 | + batchStore.EXPECT().Get(gomock.Any(), "b1").Return(entity.Batch{}, storage.WrapNotFound(errors.New("no rows"))) |
| 130 | + }, |
| 131 | + wantErr: storage.ErrNotFound, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + for name, tt := range tests { |
| 136 | + t.Run(name, func(t *testing.T) { |
| 137 | + mockStorage, mockBatchStore, mockRecordStore := testStores(t) |
| 138 | + tt.setup(mockBatchStore, mockRecordStore) |
| 139 | + |
| 140 | + got, err := ListByStates(context.Background(), mockStorage, testQueue, tt.states) |
| 141 | + if tt.wantErr != nil { |
| 142 | + require.Error(t, err) |
| 143 | + assert.ErrorIs(t, err, tt.wantErr) |
| 144 | + return |
| 145 | + } |
| 146 | + require.NoError(t, err) |
| 147 | + assert.ElementsMatch(t, tt.want, got) |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
0 commit comments