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

Added tests for starting, stopping and purging daemon for replicationQueue #5973

Merged
merged 16 commits into from
Jun 5, 2024
Merged
Changes from 15 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
114 changes: 114 additions & 0 deletions common/domain/replication_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ import (
"context"
"encoding/binary"
"errors"
"sync/atomic"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/uber/cadence/common"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/types"
)
Expand All @@ -39,6 +42,93 @@ const (
preambleVersion0 byte = 0x59
)

func TestReplicationQueueImpl_Start(t *testing.T) {
tests := []struct {
name string
initialStatus int32
expectedStatus int32
shouldStart bool
}{
{
name: "Should start when initialized",
initialStatus: common.DaemonStatusInitialized,
expectedStatus: common.DaemonStatusStarted,
shouldStart: true,
},
{
name: "Should not start when already started",
initialStatus: common.DaemonStatusStarted,
expectedStatus: common.DaemonStatusStarted,
shouldStart: false,
},
}

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).(*replicationQueueImpl)
atomic.StoreInt32(&rq.status, tt.initialStatus)

rq.Start()
defer rq.Stop()
assert.Equal(t, tt.expectedStatus, atomic.LoadInt32(&rq.status))

if tt.shouldStart {
select {
case <-rq.done:
t.Error("purgeProcessor should not have stopped")
case <-time.After(time.Millisecond):
// expected, as the purgeProcessor should still be running
}
}
})
}
}

func TestReplicationQueueImpl_Stop(t *testing.T) {
tests := []struct {
name string
initialStatus int32
expectedStatus int32
shouldStop bool
}{
{
name: "Should stop when started",
initialStatus: common.DaemonStatusStarted,
expectedStatus: common.DaemonStatusStopped,
shouldStop: true,
},
{
name: "Should not stop when not started",
initialStatus: common.DaemonStatusInitialized,
expectedStatus: common.DaemonStatusInitialized,
shouldStop: false,
},
}

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).(*replicationQueueImpl)
atomic.StoreInt32(&rq.status, tt.initialStatus)

rq.Stop()
assert.Equal(t, tt.expectedStatus, atomic.LoadInt32(&rq.status))

if tt.shouldStop {
select {
case <-rq.done:
// expected channel closed
default:
t.Error("done channel should be closed")
}
}
})
}
}

func TestReplicationQueueImpl_Publish(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -619,3 +709,27 @@ func TestPurgeAckedMessages(t *testing.T) {
})
}
}

func TestReplicationQueueImpl_purgeProcessor(t *testing.T) {
ctrl := gomock.NewController(t)
mockQueue := persistence.NewMockQueueManager(ctrl)
rq := NewReplicationQueue(mockQueue, "testCluster", nil, nil).(*replicationQueueImpl)
atomic.StoreInt32(&rq.status, common.DaemonStatusInitialized)

done := make(chan bool)
mockQueue.EXPECT().GetAckLevels(gomock.Any()).Return(map[string]int64{}, nil).AnyTimes()
mockQueue.EXPECT().DeleteMessagesBefore(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()

go func() {
rq.Start()
close(done)
}()

rq.Stop()
abhishekj720 marked this conversation as resolved.
Show resolved Hide resolved
select {
case <-done:
// Pass if the goroutine exits
case <-time.After(10 * time.Millisecond):
t.Error("purgeProcessor did not stop within expected time")
}
}
Loading