11import { expect } from "chai" ;
22import Redis , { Cluster } from "../../lib" ;
3- import redis from "../../lib" ;
43
54const host = "127.0.0.1" ;
6- const masters = [ 30000 , 30001 , 30002 ] ;
5+ const masters = [ 3000 , 3001 , 3002 ] ;
76const port : number = masters [ 0 ]
87
98/**
@@ -22,33 +21,44 @@ describe("cluster:ClusterSubscriberGroup", () => {
2221
2322 const cluster : Cluster = new Cluster ( [ { host : host , port : port } ] , { shardedSubscribers : true } ) ;
2423
25- //Subscribe to the three channels
26- cluster . ssubscribe ( "channel:1:{1}" , "channel:2:{1}" , "channel:3:{1}" ) . then ( ( count : number ) => {
27- console . log ( "Subscribed to 3 channels." ) ;
28- expect ( count ) . to . equal ( 3 ) ;
29- } ) ;
30-
31- //Publish a message to one of the channels
32- cluster . spublish ( "channel:2:{1}" , "This is a test message to channel 2." ) . then ( ( value : number ) => {
33- console . log ( "Published a message to channel:2:{1} and expect one subscriber." ) ;
34- expect ( value ) . to . be . eql ( 1 ) ;
35- } ) ;
24+ // Subscribe to the three channels
25+ const subscribeCount = await cluster . ssubscribe (
26+ "channel:1:{1}" ,
27+ "channel:2:{1}" ,
28+ "channel:3:{1}"
29+ ) ;
30+ expect ( subscribeCount , "Should be subscribed to 3 channels." ) . to . equal ( 3 ) ;
31+
32+ // Publish a message to one of the channels
33+ const publishValue = await cluster . spublish (
34+ "channel:2:{1}" ,
35+ "This is a test message to channel 2."
36+ ) ;
37+ expect (
38+ publishValue ,
39+ "Should have published a message to channel:2:{1} and expect one subscriber."
40+ ) . to . be . eql ( 1 ) ;
3641
3742 await sleep ( 500 ) ;
3843
39- //Unsubscribe from one of the channels
40- cluster . sunsubscribe ( "channel:2:{1}" ) . then ( ( count : number ) => {
41- console . log ( "Unsubscribed from channel:2:{1} .") ;
42- expect ( count ) . to . equal ( 2 ) ;
43- } ) ;
44+ // Unsubscribe from one of the channels
45+ const unsubscribeCount = await cluster . sunsubscribe ( "channel:2:{1}" ) ;
46+ expect ( unsubscribeCount , "Should be subscribed from 2 channels .") . to . equal (
47+ 2
48+ ) ;
4449
4550 await sleep ( 500 ) ;
4651
47- //Publish a message to the channel from which we unsubscribed
48- cluster . spublish ( "channel:2:{1}" , "This is a test message to channel 2." ) . then ( ( value : number ) => {
49- console . log ( "Published a second message to channel:2:{1} and expect to have nobody listening." ) ;
50- expect ( value ) . to . be . eql ( 0 ) ;
51- } ) ;
52+ // Publish a message to the channel from which we unsubscribed
53+ const value = await cluster . spublish (
54+ "channel:2:{1}" ,
55+ "This is a test message to channel 2."
56+ ) ;
57+
58+ expect (
59+ value ,
60+ "Published a second message to channel:2:{1} and expect to have nobody listening."
61+ ) . to . be . eql ( 0 ) ;
5262
5363 await sleep ( 1000 ) ;
5464 await cluster . disconnect ( ) ;
@@ -167,22 +177,22 @@ describe("cluster:ClusterSubscriberGroup", () => {
167177 const channel = "channel:test:3" ;
168178
169179 //Used as control connections for orchestrating the slot migration
170- const source : Redis = new Redis ( { host : host , port : 30000 } ) ;
171- const target : Redis = new Redis ( { host : host , port : 30001 } ) ;
180+ const source : Redis = new Redis ( { host : host , port : 3000 } ) ;
181+ const target : Redis = new Redis ( { host : host , port : 3001 } ) ;
172182
173183 //Initialize the publisher cluster connections and verify that the slot is on node 1
174184 const publisher : Cluster = new Cluster ( [ { host : host , port : port } ] ) ;
175185
176186 publisher . on ( "ready" , ( ) => {
177- expect ( publisher . slots [ slot ] [ 0 ] ) . eql ( "127.0.0.1:30000 " ) ;
187+ expect ( publisher . slots [ slot ] [ 0 ] ) . eql ( "127.0.0.1:3000 " ) ;
178188 } ) ;
179189
180190
181191 //Initialize the subscriber cluster connections and verify that the slot is on node 1
182192 const subscriber : Cluster = new Cluster ( [ { host : host , port : port } ] , { shardedSubscribers : true } ) ;
183193
184194 subscriber . on ( "ready" , ( ) => {
185- expect ( subscriber . slots [ slot ] [ 0 ] ) . eql ( "127.0.0.1:30000 " )
195+ expect ( subscriber . slots [ slot ] [ 0 ] ) . eql ( "127.0.0.1:3000 " )
186196 } ) ;
187197
188198 //The subscription callback. We should receive both messages
@@ -227,14 +237,20 @@ describe("cluster:ClusterSubscriberGroup", () => {
227237 //TODO: What if there is no traffic on the cluster connection?
228238 status = await subscriber . set ( "match_slot{" + channel + "}" , "channel 3" ) ;
229239 expect ( status ) . to . eql ( "OK" ) ;
230- expect ( subscriber . slots [ slot ] [ 0 ] ) . eql ( "127.0.0.1:30001 " ) ;
240+ expect ( subscriber . slots [ slot ] [ 0 ] ) . eql ( "127.0.0.1:3001 " ) ;
231241
232242 //Wait a bit to let the subscriber resubscribe to previous channels
233243 await sleep ( 1000 ) ;
234244
235245 const numSubscribers = await publisher . spublish ( channel , "This is a test message #2 to slot "
236246 + slot + " on channel " + channel + "." ) ;
237- expect ( publisher . slots [ slot ] [ 0 ] ) . eql ( "127.0.0.1:30001 " ) ;
247+ expect ( publisher . slots [ slot ] [ 0 ] ) . eql ( "127.0.0.1:3001 " ) ;
238248 expect ( numSubscribers ) . to . eql ( 1 ) ;
249+
250+ await sleep ( 1000 ) ;
251+ subscriber . disconnect ( ) ;
252+ publisher . disconnect ( ) ;
253+ source . disconnect ( ) ;
254+ target . disconnect ( ) ;
239255 } ) ;
240- } ) ;
256+ } ) ;
0 commit comments