|
| 1 | +// Copyright (c) 2025 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 request |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 24 | + "github.com/uber/submitqueue/submitqueue/extension/storage" |
| 25 | + storagemock "github.com/uber/submitqueue/submitqueue/extension/storage/mock" |
| 26 | + "go.uber.org/mock/gomock" |
| 27 | +) |
| 28 | + |
| 29 | +func TestAdmissionWriter_Create(t *testing.T) { |
| 30 | + summary := testRequestSummary() |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + setup func(*gomock.Controller, *storagemock.MockStorage) |
| 34 | + wantErr error |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "creates all projections", |
| 38 | + setup: func(ctrl *gomock.Controller, store *storagemock.MockStorage) { |
| 39 | + summaryStore := storagemock.NewMockRequestSummaryStore(ctrl) |
| 40 | + uriStore := storagemock.NewMockRequestURIStore(ctrl) |
| 41 | + queueStore := storagemock.NewMockRequestQueueSummaryStore(ctrl) |
| 42 | + store.EXPECT().GetRequestSummaryStore().Return(summaryStore).AnyTimes() |
| 43 | + store.EXPECT().GetRequestURIStore().Return(uriStore).AnyTimes() |
| 44 | + store.EXPECT().GetRequestQueueSummaryStore().Return(queueStore).AnyTimes() |
| 45 | + summaryStore.EXPECT().Create(gomock.Any(), summary).Return(nil) |
| 46 | + uriStore.EXPECT().Create(gomock.Any(), entity.RequestURI{ChangeURI: "uri/1", ReceivedAtMs: 10, RequestID: "q/1"}).Return(nil) |
| 47 | + uriStore.EXPECT().Create(gomock.Any(), entity.RequestURI{ChangeURI: "uri/2", ReceivedAtMs: 10, RequestID: "q/1"}).Return(nil) |
| 48 | + queueStore.EXPECT().Create(gomock.Any(), queueSummaryFromSummary(summary)).Return(nil) |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "identical retry succeeds", |
| 53 | + setup: func(ctrl *gomock.Controller, store *storagemock.MockStorage) { |
| 54 | + summaryStore := storagemock.NewMockRequestSummaryStore(ctrl) |
| 55 | + uriStore := storagemock.NewMockRequestURIStore(ctrl) |
| 56 | + queueStore := storagemock.NewMockRequestQueueSummaryStore(ctrl) |
| 57 | + store.EXPECT().GetRequestSummaryStore().Return(summaryStore).AnyTimes() |
| 58 | + store.EXPECT().GetRequestURIStore().Return(uriStore).AnyTimes() |
| 59 | + store.EXPECT().GetRequestQueueSummaryStore().Return(queueStore).AnyTimes() |
| 60 | + summaryStore.EXPECT().Create(gomock.Any(), summary).Return(storage.ErrAlreadyExists) |
| 61 | + summaryStore.EXPECT().Get(gomock.Any(), "q/1").Return(summary, nil) |
| 62 | + uriStore.EXPECT().Create(gomock.Any(), gomock.Any()).Return(storage.ErrAlreadyExists).Times(2) |
| 63 | + queueStore.EXPECT().Create(gomock.Any(), queueSummaryFromSummary(summary)).Return(storage.ErrAlreadyExists) |
| 64 | + queueStore.EXPECT().Get(gomock.Any(), "q", int64(10), "q/1").Return(queueSummaryFromSummary(summary), nil) |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "conflicting summary retry fails", |
| 69 | + setup: func(ctrl *gomock.Controller, store *storagemock.MockStorage) { |
| 70 | + summaryStore := storagemock.NewMockRequestSummaryStore(ctrl) |
| 71 | + store.EXPECT().GetRequestSummaryStore().Return(summaryStore).AnyTimes() |
| 72 | + summaryStore.EXPECT().Create(gomock.Any(), summary).Return(storage.ErrAlreadyExists) |
| 73 | + conflict := summary |
| 74 | + conflict.Queue = "other" |
| 75 | + summaryStore.EXPECT().Get(gomock.Any(), "q/1").Return(conflict, nil) |
| 76 | + }, |
| 77 | + wantErr: storage.ErrAlreadyExists, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "URI write failure stops queue projection", |
| 81 | + setup: func(ctrl *gomock.Controller, store *storagemock.MockStorage) { |
| 82 | + summaryStore := storagemock.NewMockRequestSummaryStore(ctrl) |
| 83 | + uriStore := storagemock.NewMockRequestURIStore(ctrl) |
| 84 | + store.EXPECT().GetRequestSummaryStore().Return(summaryStore).AnyTimes() |
| 85 | + store.EXPECT().GetRequestURIStore().Return(uriStore).AnyTimes() |
| 86 | + summaryStore.EXPECT().Create(gomock.Any(), summary).Return(nil) |
| 87 | + uriStore.EXPECT().Create(gomock.Any(), gomock.Any()).Return(fmt.Errorf("URI down")) |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + for _, tt := range tests { |
| 93 | + t.Run(tt.name, func(t *testing.T) { |
| 94 | + ctrl := gomock.NewController(t) |
| 95 | + store := storagemock.NewMockStorage(ctrl) |
| 96 | + tt.setup(ctrl, store) |
| 97 | + err := NewAdmissionWriter(store).Create(context.Background(), summary) |
| 98 | + if tt.wantErr != nil { |
| 99 | + require.ErrorIs(t, err, tt.wantErr) |
| 100 | + } else if tt.name == "URI write failure stops queue projection" { |
| 101 | + require.Error(t, err) |
| 102 | + } else { |
| 103 | + require.NoError(t, err) |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func testRequestSummary() entity.RequestSummary { |
| 110 | + return entity.RequestSummary{ |
| 111 | + RequestID: "q/1", Queue: "q", ChangeURIs: []string{"uri/1", "uri/2"}, ReceivedAtMs: 10, |
| 112 | + Status: entity.RequestStatusAccepted, StatusTimestampMs: 10, Version: 1, Metadata: map[string]string{}, |
| 113 | + } |
| 114 | +} |
0 commit comments