Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Dec 24, 2024
1 parent 4983063 commit 1997a9d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
35 changes: 11 additions & 24 deletions internal/consuming/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/centrifugal/centrifugo/v5/internal/configtypes"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"

"github.com/centrifugal/centrifuge"
"github.com/centrifugal/centrifugo/v5/internal/configtypes"

"github.com/centrifugal/centrifugo/v5/internal/apiproto"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -41,19 +41,6 @@ func (m *MockDispatcher) Broadcast(ctx context.Context, req *apiproto.BroadcastR
return m.onBroadcast(ctx, req)
}

// MockLogger implements the Logger interface for testing.
type MockLogger struct {
// Add necessary fields to simulate behavior or record calls
}

func (m *MockLogger) LogEnabled(_ centrifuge.LogLevel) bool {
return true // or false based on your test needs
}

func (m *MockLogger) Log(_ centrifuge.LogEntry) {
// Implement mock logic, e.g., storing log entries for assertions
}

func produceTestMessage(topic string, message []byte, headers []kgo.RecordHeader) error {
// Create a new client
client, err := kgo.NewClient(kgo.SeedBrokers(testKafkaBrokerURL))
Expand Down Expand Up @@ -167,7 +154,7 @@ func TestKafkaConsumer_GreenScenario(t *testing.T) {
eventReceived := make(chan struct{})
consumerClosed := make(chan struct{})

consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockLogger{}, &MockDispatcher{
consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockDispatcher{
onDispatch: func(ctx context.Context, method string, data []byte) error {
require.Equal(t, testMethod, method)
require.Equal(t, testPayload, data)
Expand Down Expand Up @@ -218,7 +205,7 @@ func TestKafkaConsumer_SeveralConsumers(t *testing.T) {
consumerClosed := make(chan struct{})

for i := 0; i < 3; i++ {
consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockLogger{}, &MockDispatcher{
consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockDispatcher{
onDispatch: func(ctx context.Context, method string, data []byte) error {
require.Equal(t, testMethod, method)
require.Equal(t, testPayload, data)
Expand Down Expand Up @@ -284,7 +271,7 @@ func TestKafkaConsumer_RetryAfterDispatchError(t *testing.T) {
return nil
},
}
consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockLogger{}, mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
consumer, err := NewKafkaConsumer("test", uuid.NewString(), mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
require.NoError(t, err)

go func() {
Expand Down Expand Up @@ -354,7 +341,7 @@ func TestKafkaConsumer_BlockedPartitionDoesNotBlockAnotherTopic(t *testing.T) {
return nil
},
}
consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockLogger{}, mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
consumer, err := NewKafkaConsumer("test", uuid.NewString(), mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
require.NoError(t, err)

go func() {
Expand Down Expand Up @@ -427,7 +414,7 @@ func TestKafkaConsumer_BlockedPartitionDoesNotBlockAnotherPartition(t *testing.T
return nil
},
}
consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockLogger{}, mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
consumer, err := NewKafkaConsumer("test", uuid.NewString(), mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
require.NoError(t, err)

go func() {
Expand Down Expand Up @@ -507,7 +494,7 @@ func TestKafkaConsumer_PausePartitions(t *testing.T) {
return nil
},
}
consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockLogger{}, mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
consumer, err := NewKafkaConsumer("test", uuid.NewString(), mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
require.NoError(t, err)

consumer.testOnlyConfig = testConfig
Expand Down Expand Up @@ -591,7 +578,7 @@ func TestKafkaConsumer_WorksCorrectlyInLoadedTopic(t *testing.T) {
ConsumerGroup: uuid.New().String(),
PartitionBufferSize: tc.partitionBuffer,
}
consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockLogger{}, mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
consumer, err := NewKafkaConsumer("test", uuid.NewString(), mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
require.NoError(t, err)

var records []*kgo.Record
Expand Down Expand Up @@ -690,7 +677,7 @@ func TestKafkaConsumer_TestPauseAfterResumeRace(t *testing.T) {
return nil
},
}
consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockLogger{}, mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
consumer, err := NewKafkaConsumer("test", uuid.NewString(), mockDispatcher, config, newCommonMetrics(prometheus.NewRegistry()))
require.NoError(t, err)

consumer.testOnlyConfig = testOnlyConfig{
Expand Down Expand Up @@ -770,7 +757,7 @@ func TestKafkaConsumer_GreenScenario_PublicationDataMode(t *testing.T) {
eventReceived := make(chan struct{})
consumerClosed := make(chan struct{})

consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockLogger{}, &MockDispatcher{
consumer, err := NewKafkaConsumer("test", uuid.NewString(), &MockDispatcher{
onBroadcast: func(ctx context.Context, req *apiproto.BroadcastRequest) error {
require.Equal(t, testChannels, req.Channels)
require.Equal(t, apiproto.Raw(testPayload), req.Data)
Expand Down
8 changes: 4 additions & 4 deletions internal/consuming/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestPostgresConsumer_GreenScenario(t *testing.T) {
PartitionPollInterval: configtypes.Duration(300 * time.Millisecond),
PartitionNotificationChannel: testNotificationChannel,
}
consumer, err := NewPostgresConsumer("test", &MockLogger{}, &MockDispatcher{
consumer, err := NewPostgresConsumer("test", &MockDispatcher{
onDispatch: func(ctx context.Context, method string, data []byte) error {
require.Equal(t, testMethod, method)
require.Equal(t, testPayload, data)
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestPostgresConsumer_SeveralConsumers(t *testing.T) {
numConsumers := 10

for i := 0; i < numConsumers; i++ {
consumer, err := NewPostgresConsumer("test", &MockLogger{}, &MockDispatcher{
consumer, err := NewPostgresConsumer("test", &MockDispatcher{
onDispatch: func(ctx context.Context, method string, data []byte) error {
require.Equal(t, testMethod, method)
require.Equal(t, testPayload, data)
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestPostgresConsumer_NotificationTrigger(t *testing.T) {

numEvents := 0

consumer, err := NewPostgresConsumer("test", &MockLogger{}, &MockDispatcher{
consumer, err := NewPostgresConsumer("test", &MockDispatcher{
onDispatch: func(ctx context.Context, method string, data []byte) error {
require.Equal(t, testMethod, method)
require.Equal(t, testPayload, data)
Expand Down Expand Up @@ -317,7 +317,7 @@ func TestPostgresConsumer_DifferentPartitions(t *testing.T) {

var dispatchMu sync.Mutex

consumer, err := NewPostgresConsumer("test", &MockLogger{}, &MockDispatcher{
consumer, err := NewPostgresConsumer("test", &MockDispatcher{
onDispatch: func(ctx context.Context, method string, data []byte) error {
dispatchMu.Lock()
defer dispatchMu.Unlock()
Expand Down

0 comments on commit 1997a9d

Please sign in to comment.