|
| 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/core/consumer" |
| 24 | + "github.com/uber/submitqueue/entity" |
| 25 | + "github.com/uber/submitqueue/entity/queue" |
| 26 | + queuemock "github.com/uber/submitqueue/extension/queue/mock" |
| 27 | + "go.uber.org/mock/gomock" |
| 28 | +) |
| 29 | + |
| 30 | +func newTestRegistry(t *testing.T, ctrl *gomock.Controller, publishErr error) consumer.TopicRegistry { |
| 31 | + mockPub := queuemock.NewMockPublisher(ctrl) |
| 32 | + mockPub.EXPECT().Publish(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( |
| 33 | + func(ctx context.Context, topic string, msg queue.Message) error { |
| 34 | + return publishErr |
| 35 | + }, |
| 36 | + ).AnyTimes() |
| 37 | + |
| 38 | + mockQ := queuemock.NewMockQueue(ctrl) |
| 39 | + mockQ.EXPECT().Publisher().Return(mockPub).AnyTimes() |
| 40 | + |
| 41 | + registry, err := consumer.NewTopicRegistry( |
| 42 | + []consumer.TopicConfig{{Key: consumer.TopicKeyLog, Name: "log", Queue: mockQ}}, |
| 43 | + ) |
| 44 | + require.NoError(t, err) |
| 45 | + return registry |
| 46 | +} |
| 47 | + |
| 48 | +func TestPublishLog_Success(t *testing.T) { |
| 49 | + ctrl := gomock.NewController(t) |
| 50 | + registry := newTestRegistry(t, ctrl, nil) |
| 51 | + |
| 52 | + logEntry := entity.NewRequestLog("req/1", entity.RequestStatusStarted, 1, "", nil) |
| 53 | + err := PublishLog(context.Background(), registry, logEntry, "req/1") |
| 54 | + require.NoError(t, err) |
| 55 | +} |
| 56 | + |
| 57 | +func TestPublishLog_PublishFailure(t *testing.T) { |
| 58 | + ctrl := gomock.NewController(t) |
| 59 | + registry := newTestRegistry(t, ctrl, fmt.Errorf("connection refused")) |
| 60 | + |
| 61 | + logEntry := entity.NewRequestLog("req/1", entity.RequestStatusStarted, 1, "", nil) |
| 62 | + err := PublishLog(context.Background(), registry, logEntry, "req/1") |
| 63 | + require.Error(t, err) |
| 64 | +} |
| 65 | + |
| 66 | +func TestPublishBatchLogs_Success(t *testing.T) { |
| 67 | + ctrl := gomock.NewController(t) |
| 68 | + registry := newTestRegistry(t, ctrl, nil) |
| 69 | + |
| 70 | + err := PublishBatchLogs(context.Background(), registry, |
| 71 | + []string{"req/1", "req/2", "req/3"}, |
| 72 | + entity.RequestStatusScored, |
| 73 | + map[string]string{"batch_id": "b/1"}, |
| 74 | + ) |
| 75 | + require.NoError(t, err) |
| 76 | +} |
| 77 | + |
| 78 | +func TestPublishBatchLogs_PartialFailure(t *testing.T) { |
| 79 | + ctrl := gomock.NewController(t) |
| 80 | + |
| 81 | + callCount := 0 |
| 82 | + mockPub := queuemock.NewMockPublisher(ctrl) |
| 83 | + mockPub.EXPECT().Publish(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( |
| 84 | + func(ctx context.Context, topic string, msg queue.Message) error { |
| 85 | + callCount++ |
| 86 | + if callCount == 2 { |
| 87 | + return fmt.Errorf("publish failed") |
| 88 | + } |
| 89 | + return nil |
| 90 | + }, |
| 91 | + ).AnyTimes() |
| 92 | + |
| 93 | + mockQ := queuemock.NewMockQueue(ctrl) |
| 94 | + mockQ.EXPECT().Publisher().Return(mockPub).AnyTimes() |
| 95 | + |
| 96 | + registry, err := consumer.NewTopicRegistry( |
| 97 | + []consumer.TopicConfig{{Key: consumer.TopicKeyLog, Name: "log", Queue: mockQ}}, |
| 98 | + ) |
| 99 | + require.NoError(t, err) |
| 100 | + |
| 101 | + err = PublishBatchLogs(context.Background(), registry, |
| 102 | + []string{"req/1", "req/2", "req/3"}, |
| 103 | + entity.RequestStatusScored, |
| 104 | + map[string]string{"batch_id": "b/1"}, |
| 105 | + ) |
| 106 | + require.Error(t, err) |
| 107 | +} |
| 108 | + |
| 109 | +func TestPublishBatchLogs_Empty(t *testing.T) { |
| 110 | + ctrl := gomock.NewController(t) |
| 111 | + registry := newTestRegistry(t, ctrl, nil) |
| 112 | + |
| 113 | + err := PublishBatchLogs(context.Background(), registry, nil, entity.RequestStatusScored, nil) |
| 114 | + require.NoError(t, err) |
| 115 | +} |
0 commit comments