|
| 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 controller |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "github.com/uber-go/tally/v4" |
| 25 | + "github.com/uber/submitqueue/core/errs" |
| 26 | + "github.com/uber/submitqueue/entity" |
| 27 | + "github.com/uber/submitqueue/extension/storage" |
| 28 | + storagemock "github.com/uber/submitqueue/extension/storage/mock" |
| 29 | + pb "github.com/uber/submitqueue/gateway/protopb" |
| 30 | + "go.uber.org/mock/gomock" |
| 31 | + "go.uber.org/zap" |
| 32 | +) |
| 33 | + |
| 34 | +func TestStatus_ReturnsCurrentState(t *testing.T) { |
| 35 | + ctrl := gomock.NewController(t) |
| 36 | + |
| 37 | + store := storagemock.NewMockRequestLogStore(ctrl) |
| 38 | + store.EXPECT().List(gomock.Any(), "test-queue/1").Return([]entity.RequestLog{ |
| 39 | + {RequestID: "test-queue/1", TimestampMs: 100, Status: entity.RequestStatusAccepted}, |
| 40 | + {RequestID: "test-queue/1", TimestampMs: 200, Status: entity.RequestStatusValidating, LastError: "boom", Metadata: map[string]string{"k": "v"}}, |
| 41 | + }, nil) |
| 42 | + |
| 43 | + controller := NewStatusController(zap.NewNop().Sugar(), tally.NoopScope, store) |
| 44 | + |
| 45 | + resp, err := controller.Status(context.Background(), &pb.StatusRequest{Sqid: "test-queue/1"}) |
| 46 | + |
| 47 | + require.NoError(t, err) |
| 48 | + assert.Equal(t, string(entity.RequestStatusValidating), resp.Status) |
| 49 | + assert.Equal(t, "boom", resp.LastError) |
| 50 | + assert.Equal(t, map[string]string{"k": "v"}, resp.Metadata) |
| 51 | +} |
| 52 | + |
| 53 | +func TestStatus_Errors(t *testing.T) { |
| 54 | + tests := []struct { |
| 55 | + name string |
| 56 | + sqid string |
| 57 | + setupStore func(*storagemock.MockRequestLogStore) |
| 58 | + wantInvalid bool |
| 59 | + wantNotFound bool |
| 60 | + wantUserError bool |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "empty sqid is an invalid request", |
| 64 | + sqid: "", |
| 65 | + wantInvalid: true, |
| 66 | + wantUserError: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "unknown sqid maps to not found", |
| 70 | + sqid: "missing/1", |
| 71 | + setupStore: func(s *storagemock.MockRequestLogStore) { |
| 72 | + s.EXPECT().List(gomock.Any(), "missing/1").Return(nil, storage.ErrNotFound) |
| 73 | + }, |
| 74 | + wantNotFound: true, |
| 75 | + wantUserError: true, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "store failure propagates as infra error", |
| 79 | + sqid: "test-queue/1", |
| 80 | + setupStore: func(s *storagemock.MockRequestLogStore) { |
| 81 | + s.EXPECT().List(gomock.Any(), "test-queue/1").Return(nil, fmt.Errorf("log backend down")) |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tt := range tests { |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + ctrl := gomock.NewController(t) |
| 89 | + store := storagemock.NewMockRequestLogStore(ctrl) |
| 90 | + if tt.setupStore != nil { |
| 91 | + tt.setupStore(store) |
| 92 | + } |
| 93 | + |
| 94 | + controller := NewStatusController(zap.NewNop().Sugar(), tally.NoopScope, store) |
| 95 | + |
| 96 | + _, err := controller.Status(context.Background(), &pb.StatusRequest{Sqid: tt.sqid}) |
| 97 | + |
| 98 | + require.Error(t, err) |
| 99 | + // IsRequestNotFound is a precise type check; IsInvalidRequest matches any |
| 100 | + // user error (see errs.userError.Is), so only assert it where it is the |
| 101 | + // defining classification. |
| 102 | + assert.Equal(t, tt.wantNotFound, IsRequestNotFound(err)) |
| 103 | + assert.Equal(t, tt.wantUserError, errs.IsUserError(err)) |
| 104 | + assert.False(t, errs.IsRetryable(err)) |
| 105 | + if tt.wantInvalid { |
| 106 | + assert.True(t, IsInvalidRequest(err)) |
| 107 | + } |
| 108 | + |
| 109 | + if tt.wantNotFound { |
| 110 | + var typed *RequestNotFoundError |
| 111 | + require.ErrorAs(t, err, &typed) |
| 112 | + assert.Equal(t, tt.sqid, typed.Sqid) |
| 113 | + } |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
0 commit comments