Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for Common/Domain: Adding tests for replication queue message handling and ack update #5730

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
}
}
Loading