Skip to content

Commit dd33b7d

Browse files
committed
add pubsub integration test for PubSub channels
Signed-off-by: jbrinkman <joe.brinkman@improving.com>
1 parent 9adf898 commit dd33b7d

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

go/api/pubsub_commands.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ package api
66
type PubSubCommands interface {
77
// Publish publishes a message to a channel. Returns the number of clients that received the message.
88
Publish(channel string, message string) (int64, error)
9+
// PubSubChannels returns a list of all channels in the database.
910
PubSubChannels() ([]string, error)
11+
// PubSubChannelsWithPattern returns a list of all channels that match the given pattern.
12+
PubSubChannelsWithPattern(pattern string) ([]string, error)
1013
}
1114

1215
// PubSubClusterCommands defines additional Pub/Sub operations available only in cluster mode.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
2+
3+
package integTest
4+
5+
import (
6+
"sort"
7+
"testing"
8+
"time"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
// TestPubSubChannels tests the PubSubChannels command for standalone client
14+
func (suite *GlideTestSuite) TestPubSubChannels() {
15+
tests := []struct {
16+
name string
17+
clientType ClientType
18+
channelNames []string
19+
pattern string
20+
expectedNames []string
21+
}{
22+
{
23+
name: "Standalone Empty Pattern",
24+
clientType: GlideClient,
25+
channelNames: []string{"news.sports", "news.weather", "events.local"},
26+
pattern: "",
27+
expectedNames: []string{"news.sports", "news.weather", "events.local"},
28+
},
29+
{
30+
name: "Standalone Exact Match",
31+
clientType: GlideClient,
32+
channelNames: []string{"news.sports", "news.weather", "events.local"},
33+
pattern: "news.sports",
34+
expectedNames: []string{"news.sports"},
35+
},
36+
{
37+
name: "Standalone Glob Pattern",
38+
clientType: GlideClient,
39+
channelNames: []string{"news.sports", "news.weather", "events.local"},
40+
pattern: "news.*",
41+
expectedNames: []string{"news.sports", "news.weather"},
42+
},
43+
{
44+
name: "Cluster Empty Pattern",
45+
clientType: GlideClusterClient,
46+
channelNames: []string{"cluster.news.sports", "cluster.news.weather", "cluster.events.local"},
47+
pattern: "",
48+
expectedNames: []string{"cluster.news.sports", "cluster.news.weather", "cluster.events.local"},
49+
},
50+
{
51+
name: "Cluster Exact Match",
52+
clientType: GlideClusterClient,
53+
channelNames: []string{"cluster.news.sports", "cluster.news.weather", "cluster.events.local"},
54+
pattern: "cluster.news.sports",
55+
expectedNames: []string{"cluster.news.sports"},
56+
},
57+
{
58+
name: "Cluster Glob Pattern",
59+
clientType: GlideClusterClient,
60+
channelNames: []string{"cluster.news.sports", "cluster.news.weather", "cluster.events.local"},
61+
pattern: "cluster.news.*",
62+
expectedNames: []string{"cluster.news.sports", "cluster.news.weather"},
63+
},
64+
}
65+
66+
for _, tt := range tests {
67+
suite.T().Run(tt.name, func(t *testing.T) {
68+
// Skip cluster tests if cluster hosts are not available
69+
if tt.clientType == 1 && len(suite.clusterHosts) == 0 {
70+
t.Skip("Cluster not available")
71+
}
72+
73+
// Create channel definitions for all channels
74+
channels := make([]ChannelDefn, len(tt.channelNames))
75+
for i, channelName := range tt.channelNames {
76+
channels[i] = ChannelDefn{Channel: channelName, Mode: 0} // ExactMode
77+
}
78+
79+
// Create a client with subscriptions
80+
receiver := suite.CreatePubSubReceiver(tt.clientType, channels, 1, false)
81+
defer receiver.Close()
82+
83+
// Allow subscription to establish
84+
time.Sleep(MESSAGE_PROCESSING_DELAY * time.Millisecond)
85+
86+
// Get active channels
87+
var activeChannels []string
88+
var err error
89+
if tt.pattern == "" {
90+
activeChannels, err = receiver.PubSubChannels()
91+
} else {
92+
activeChannels, err = receiver.PubSubChannelsWithPattern(tt.pattern)
93+
}
94+
assert.NoError(t, err)
95+
96+
// Sort both slices for consistent comparison
97+
sort.Strings(activeChannels)
98+
sort.Strings(tt.expectedNames)
99+
100+
// Verify using the verification function
101+
assert.Equal(t, tt.expectedNames, activeChannels)
102+
})
103+
}
104+
}

0 commit comments

Comments
 (0)