From cb39e8277c8d0876f2d60eddb4272909dd0371b2 Mon Sep 17 00:00:00 2001 From: Abhishek Jha Date: Wed, 6 Mar 2024 08:53:23 -0800 Subject: [PATCH] Tests for Common/Domain: Adding tests for replication queue message handling and ack update (#5730) * Common/Domain: Adding tests for replication_queue:ReplicationMessages, UpdateAckLevel --- common/domain/replication_queue_test.go | 95 +++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/common/domain/replication_queue_test.go b/common/domain/replication_queue_test.go index 365934f3a13..a795401f18a 100644 --- a/common/domain/replication_queue_test.go +++ b/common/domain/replication_queue_test.go @@ -115,3 +115,98 @@ func TestReplicationQueueImpl_PublishToDLQ(t *testing.T) { }) } } + +func TestGetReplicationMessages(t *testing.T) { + + tests := []struct { + name string + lastID int64 + maxCount int + task *types.ReplicationTask + wantErr bool + setupMock func(q *persistence.MockQueueManager) + }{ + { + name: "successful message retrieval", + lastID: 100, + maxCount: 10, + task: &types.ReplicationTask{}, + wantErr: false, + setupMock: func(q *persistence.MockQueueManager) { + q.EXPECT().ReadMessages(gomock.Any(), gomock.Eq(int64(100)), gomock.Eq(10)).Return(persistence.QueueMessageList{}, nil) + }, + }, + { + name: "read messages fails", + lastID: 100, + maxCount: 10, + wantErr: true, + setupMock: func(q *persistence.MockQueueManager) { + q.EXPECT().ReadMessages(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("read error")) + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctrl := gomock.NewController(t) + mockQueue := persistence.NewMockQueueManager(ctrl) + rq := NewReplicationQueue(mockQueue, "testCluster", nil, nil) + + tt.setupMock(mockQueue) + _, _, err := rq.GetReplicationMessages(context.Background(), tt.lastID, tt.maxCount) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + ctrl.Finish() + }) + } +} + +func TestUpdateAckLevel(t *testing.T) { + tests := []struct { + name string + lastID int64 + cluster string + wantErr bool + setupMock func(q *persistence.MockQueueManager) + }{ + { + name: "successful ack level update", + lastID: 100, + cluster: "testCluster", + wantErr: false, + setupMock: func(q *persistence.MockQueueManager) { + q.EXPECT().UpdateAckLevel(gomock.Any(), gomock.Eq(int64(100)), gomock.Eq("testCluster")).Return(nil) + }, + }, + { + name: "ack level update fails", + lastID: 100, + cluster: "testCluster", + wantErr: true, + setupMock: func(q *persistence.MockQueueManager) { + q.EXPECT().UpdateAckLevel(gomock.Any(), gomock.Eq(int64(100)), gomock.Eq("testCluster")).Return(errors.New("update error")) + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctrl := gomock.NewController(t) + mockQueue := persistence.NewMockQueueManager(ctrl) + + rq := NewReplicationQueue(mockQueue, "testCluster", nil, nil) + tt.setupMock(mockQueue) + err := rq.UpdateAckLevel(context.Background(), tt.lastID, tt.cluster) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + ctrl.Finish() + }) + } +}