11using System ;
2+ using System . Collections . Generic ;
23using System . Threading ;
34using NUnit . Framework ;
45using ServiceStack . Text ;
@@ -9,16 +10,26 @@ namespace ServiceStack.Redis.Tests
910 [ TestFixture ]
1011 public class RedisPubSubServerTests
1112 {
12- private static RedisPubSubServer CreatePubSubServer (
13- int intervalSecs = 1 , int timeoutSecs = 3 )
13+ RedisManagerPool clientsManager = new RedisManagerPool ( TestConfig . MasterHosts ) ;
14+
15+ [ OneTimeTearDown ]
16+ public void OneTimeTearDown ( )
17+ {
18+ clientsManager . Dispose ( ) ;
19+ }
20+
21+ private RedisPubSubServer CreatePubSubServer (
22+ int intervalSecs = 1 , int timeoutSecs = 3 , params string [ ] channels )
1423 {
15- var clientsManager = new RedisManagerPool ( TestConfig . MasterHosts ) ;
1624 using ( var redis = clientsManager . GetClient ( ) )
1725 redis . FlushAll ( ) ;
26+
27+ if ( channels . Length == 0 )
28+ channels = new [ ] { "topic:test" } ;
1829
1930 var pubSub = new RedisPubSubServer (
2031 clientsManager ,
21- "topic:test" )
32+ channels )
2233 {
2334 HeartbeatInterval = TimeSpan . FromSeconds ( intervalSecs ) ,
2435 HeartbeatTimeout = TimeSpan . FromSeconds ( timeoutSecs )
@@ -97,5 +108,42 @@ public void Does_send_heartbeat_pulses_to_multiple_PubSubServers()
97108
98109 pubSubs . Each ( x => x . Dispose ( ) ) ;
99110 }
111+
112+ [ Test ]
113+ public void Can_restart_and_subscribe_to_more_channels ( )
114+ {
115+ var a = new List < string > ( ) ;
116+ var b = new List < string > ( ) ;
117+ var pubSub = CreatePubSubServer ( intervalSecs : 20 , timeoutSecs : 30 , "topic:a" ) ;
118+ pubSub . OnMessage = ( channel , msg ) => {
119+ if ( channel == "topic:a" )
120+ a . Add ( msg ) ;
121+ else if ( channel == "topic:b" )
122+ b . Add ( msg ) ;
123+ } ;
124+ pubSub . Start ( ) ;
125+ Thread . Sleep ( 100 ) ;
126+
127+ var client = clientsManager . GetClient ( ) ;
128+ var i = 0 ;
129+ client . PublishMessage ( "topic:a" , $ "msg: ${ ++ i } ") ;
130+ client . PublishMessage ( "topic:b" , $ "msg: ${ ++ i } ") ;
131+
132+ Thread . Sleep ( 100 ) ;
133+ Assert . That ( a . Count , Is . EqualTo ( 1 ) ) ;
134+ Assert . That ( b . Count , Is . EqualTo ( 0 ) ) ;
135+
136+ pubSub . Channels = new [ ] { "topic:a" , "topic:b" } ;
137+ pubSub . Restart ( ) ;
138+ Thread . Sleep ( 100 ) ;
139+
140+ client . PublishMessage ( "topic:a" , $ "msg: ${ ++ i } ") ;
141+ client . PublishMessage ( "topic:b" , $ "msg: ${ ++ i } ") ;
142+
143+
144+ Thread . Sleep ( 100 ) ;
145+ Assert . That ( a . Count , Is . EqualTo ( 2 ) ) ;
146+ Assert . That ( b . Count , Is . EqualTo ( 1 ) ) ;
147+ }
100148 }
101149}
0 commit comments