Skip to content

Commit

Permalink
sonarqube: remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
surendratiwari3 committed Feb 6, 2024
1 parent 456c704 commit e688b84
Showing 1 changed file with 24 additions and 43 deletions.
67 changes: 24 additions & 43 deletions workerpool/workerpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ import (
"github.com/surendratiwari3/paota/internal/factory"
"github.com/surendratiwari3/paota/internal/schema"
"github.com/surendratiwari3/paota/internal/task"
"os"
"testing"
"time"
)

func TestNewWorkerPool(t *testing.T) {
// Mock the broker for testing
mockBroker := broker.NewMockBroker(t)
mockTaskRegistrar := task.NewMockTaskRegistrarInterface(t)
// Create a mock AMQPAdapter with a valid config
func setupConfigProvider() {
mockConfigProvider := new(config.MockConfigProvider)
mockConfigProvider.On("GetConfig").Return(&config.Config{
Broker: "amqp",
Expand All @@ -29,12 +26,19 @@ func TestNewWorkerPool(t *testing.T) {
ConnectionPoolSize: 2,
},
}, nil)
config.SetConfigProvider(mockConfigProvider)
}

func TestNewWorkerPool(t *testing.T) {
setupConfigProvider()
// Mock the broker for testing
mockBroker := broker.NewMockBroker(t)
mockTaskRegistrar := task.NewMockTaskRegistrarInterface(t)

mockFactory := new(factory.MockIFactory)
mockFactory.On("CreateBroker").Return(mockBroker, nil)
mockFactory.On("CreateStore").Return(nil)
mockFactory.On("CreateTaskRegistrar", mock.Anything).Return(mockTaskRegistrar)
config.SetConfigProvider(mockConfigProvider)

globalFactory = mockFactory

Expand All @@ -48,6 +52,7 @@ func TestNewWorkerPool(t *testing.T) {
}

func TestWorkerPool_SendTaskWithContext(t *testing.T) {
setupConfigProvider()
mockBroker := &broker.MockBroker{}

mockBroker.On("Publish", mock.Anything, mock.Anything).Return(nil)
Expand All @@ -56,23 +61,10 @@ func TestWorkerPool_SendTaskWithContext(t *testing.T) {
mockTaskRegistrar := task.NewMockTaskRegistrarInterface(t)
mockTaskRegistrar.On("SendTaskWithContext", mock.Anything, mock.Anything).Return(nil)

// Create a mock AMQPAdapter with a valid config
mockConfigProvider := new(config.MockConfigProvider)
mockConfigProvider.On("GetConfig").Return(&config.Config{
Broker: "amqp",
TaskQueueName: "test",
AMQP: &config.AMQPConfig{
Url: "amqp://localhost:5672",
HeartBeatInterval: 30,
ConnectionPoolSize: 2,
},
}, nil)

mockFactory := new(factory.MockIFactory)
mockFactory.On("CreateBroker").Return(mockBroker, nil)
mockFactory.On("CreateStore").Return(nil)
mockFactory.On("CreateTaskRegistrar", mock.Anything).Return(mockTaskRegistrar)
config.SetConfigProvider(mockConfigProvider)

globalFactory = mockFactory

Expand All @@ -98,7 +90,8 @@ func TestWorkerPool_SendTaskWithContext(t *testing.T) {
assert.Equal(t, "Pending", state.Status)
}

func TestWorkerPool_StartWithBrokerInError(t *testing.T) {
func TestWorkerPool_Start(t *testing.T) {
setupConfigProvider()
mockBroker := broker.NewMockBroker(t)
mockBroker.On("StartConsumer", mock.Anything, mock.Anything, mock.Anything).Return(errors.New("start consumer failed"))
mockBroker.On("StopConsumer").Return()
Expand All @@ -111,19 +104,6 @@ func TestWorkerPool_StartWithBrokerInError(t *testing.T) {
mockFactory.On("CreateStore").Return(nil)
mockFactory.On("CreateTaskRegistrar", mock.Anything).Return(mockTaskReg)

// Create a mock AMQPAdapter with a valid config
mockConfigProvider := new(config.MockConfigProvider)
mockConfigProvider.On("GetConfig").Return(&config.Config{
Broker: "amqp",
TaskQueueName: "test",
AMQP: &config.AMQPConfig{
Url: "amqp://localhost:5672",
HeartBeatInterval: 30,
ConnectionPoolSize: 2,
},
}, nil)
config.SetConfigProvider(mockConfigProvider)

globalFactory = mockFactory

wp, err := NewWorkerPool(context.Background(), 10, "test")
Expand All @@ -140,15 +120,16 @@ func TestWorkerPool_StartWithBrokerInError(t *testing.T) {
time.Sleep(100 * time.Millisecond)
wp.Stop()
assert.Nil(t, err)
}

func TestMain(m *testing.M) {
// Perform setup tasks here

// Run tests
exitCode := m.Run()

// Perform cleanup tasks here

/*
go func() {
wp = false
err = wp.Start()
assert.Nil(t, err)
}()
time.Sleep(100 * time.Millisecond)
wp.Stop()
wp.started = false
wp.Stop()*/
// Exit with the same exit code as the tests
os.Exit(exitCode)
}

0 comments on commit e688b84

Please sign in to comment.