Skip to content

Commit

Permalink
tests: add tests for pkg/broker/redis (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishekvrshny authored Dec 16, 2024
1 parent eeff329 commit 24e1a6a
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/CRED-CLUB/propeller
go 1.23

require (
github.com/alicebob/miniredis/v2 v2.33.0
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.4.2
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
Expand All @@ -28,6 +29,7 @@ require (
github.com/DataDog/go-tuf v0.3.0--fix-localmeta-fork // indirect
github.com/DataDog/sketches-go v1.2.1 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down Expand Up @@ -64,6 +66,7 @@ require (
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
github.com/tinylib/msgp v1.1.6 // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go4.org/intern v0.0.0-20211027215823-ae77deb06f29 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ github.com/DataDog/sketches-go v1.2.1/go.mod h1:1xYmPLY1So10AwxV6MJV0J53XVH+WL9A
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY=
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk=
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis/v2 v2.33.0 h1:uvTF0EDeu9RLnUEG27Db5I68ESoIxTiXbNUiji6lZrA=
github.com/alicebob/miniredis/v2 v2.33.0/go.mod h1:MhP4a3EU7aENRi9aO+tHfTBZicLqQevyi/DJpoj6mi0=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bsm/ginkgo/v2 v2.5.0 h1:aOAnND1T40wEdAtkGSkvSICWeQ8L3UASX7YVCqQx+eQ=
Expand Down Expand Up @@ -303,6 +307,8 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand Down
182 changes: 182 additions & 0 deletions pkg/broker/redis/pubsub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package redispkg

import (
"context"
"testing"

"github.com/CRED-CLUB/propeller/pkg/broker"
"github.com/CRED-CLUB/propeller/pkg/logger"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

// MockRedisClient is a mock implementation of redis.UniversalClient
type MockRedisClient struct {
mock.Mock
}

func (m *MockRedisClient) Publish(ctx context.Context, channel string, message interface{}) *redis.IntCmd {
args := m.Called(ctx, channel, message)
return args.Get(0).(*redis.IntCmd)
}

func (m *MockRedisClient) Subscribe(ctx context.Context, channels ...string) *redis.PubSub {
args := m.Called(ctx, channels)
return args.Get(0).(*redis.PubSub)
}

func (m *MockRedisClient) TxPipeline() redis.Pipeliner {
args := m.Called()
return args.Get(0).(redis.Pipeliner)
}

func (m *MockRedisClient) AddHook(hook redis.Hook) {
m.Called(hook)
}

func (m *MockRedisClient) Append(ctx context.Context, key, value string) *redis.IntCmd {
args := m.Called(ctx, key, value)
return args.Get(0).(*redis.IntCmd)
}

// MockPipeliner is a mock implementation of redis.Pipeliner
type MockPipeliner struct {
mock.Mock
}

func (m *MockPipeliner) Exec(ctx context.Context) ([]redis.Cmder, error) {
args := m.Called(ctx)
return args.Get(0).([]redis.Cmder), args.Error(1)
}

func (m *MockPipeliner) Publish(ctx context.Context, channel string, message interface{}) *redis.IntCmd {
args := m.Called(ctx, channel, message)
return args.Get(0).(*redis.IntCmd)
}

func TestPubSub_Publish(t *testing.T) {
mr, err := miniredis.Run()
assert.NoError(t, err)
defer mr.Close()

client := redis.NewClient(&redis.Options{
Addr: mr.Addr(),
})
defer client.Close()

ps := &PubSub{
c: &Client{client: client},
}

// Test cases using real Redis commands with miniredis
err = ps.Publish(context.Background(), PublishRequest{
Channel: "test-channel",
Data: []byte("test-data"),
})
assert.NoError(t, err)
}

func TestPubSub_PublishBulk(t *testing.T) {
mr, err := miniredis.Run()
assert.NoError(t, err)
defer mr.Close()

client := redis.NewClient(&redis.Options{
Addr: mr.Addr(),
})
defer client.Close()

ps := &PubSub{
c: &Client{client: client},
}

requests := []PublishRequest{
{Channel: "channel1", Data: []byte("data1")},
{Channel: "channel2", Data: []byte("data2")},
}

err = ps.PublishBulk(context.Background(), requests)
assert.NoError(t, err)
}

func TestPubSub_Subscribe(t *testing.T) {
// Initialize logger for tests
serviceKV := map[string]interface{}{
"serviceName": "test-service",
"gitCommitHash": "test-hash",
}
_, err := logger.NewLogger("dev", serviceKV, nil)

assert.NoError(t, err)
mr, err := miniredis.Run()
assert.NoError(t, err)
defer mr.Close()

client := redis.NewClient(&redis.Options{
Addr: mr.Addr(),
})
defer client.Close()

ps := &PubSub{
c: &Client{client: client},
}
ctx, cancel := context.WithCancel(context.Background())
subscription := ps.Subscribe(ctx, "test-channel")
assert.NotNil(t, subscription)
assert.IsType(t, PubSubSubscription{}, subscription)
cancel()
}

func TestPubSub_RemoveSubscription(t *testing.T) {
mr, err := miniredis.Run()
assert.NoError(t, err)
defer mr.Close()

client := redis.NewClient(&redis.Options{
Addr: mr.Addr(),
})
defer client.Close()

ps := &PubSub{
c: &Client{client: client},
}

subscription := PubSubSubscription{
BaseSubscription: broker.BaseSubscription{
TopicEventChan: make(chan broker.TopicEvent),
Topics: []string{"test-channel"},
},
subs: client.Subscribe(context.Background()),
}

err = ps.RemoveSubscription(context.Background(), "test-channel", subscription)
assert.NoError(t, err)
}

func TestPubSub_UnSubscribe(t *testing.T) {
mr, err := miniredis.Run()
assert.NoError(t, err)
defer mr.Close()

client := redis.NewClient(&redis.Options{
Addr: mr.Addr(),
})
defer client.Close()

ps := &PubSub{
c: &Client{client: client},
}

subscription := PubSubSubscription{
BaseSubscription: broker.BaseSubscription{
TopicEventChan: make(chan broker.TopicEvent),
Topics: []string{"test-channel"},
},
subs: client.Subscribe(context.Background()),
}

err = ps.UnSubscribe(context.Background(), subscription)
assert.NoError(t, err)
}

0 comments on commit 24e1a6a

Please sign in to comment.