Skip to content

Commit

Permalink
Tests for Common/Domain: Adding tests for replication queue message h…
Browse files Browse the repository at this point in the history
…andling and ack update (#5730)

* Common/Domain: Adding tests for replication_queue:ReplicationMessages, UpdateAckLevel
  • Loading branch information
abhishekj720 authored Mar 6, 2024
1 parent b443f02 commit cb39e82
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions common/domain/replication_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}
}

0 comments on commit cb39e82

Please sign in to comment.