1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Threading ;
3
4
using NUnit . Framework ;
4
5
using ServiceStack . Text ;
@@ -9,16 +10,26 @@ namespace ServiceStack.Redis.Tests
9
10
[ TestFixture ]
10
11
public class RedisPubSubServerTests
11
12
{
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 )
14
23
{
15
- var clientsManager = new RedisManagerPool ( TestConfig . MasterHosts ) ;
16
24
using ( var redis = clientsManager . GetClient ( ) )
17
25
redis . FlushAll ( ) ;
26
+
27
+ if ( channels . Length == 0 )
28
+ channels = new [ ] { "topic:test" } ;
18
29
19
30
var pubSub = new RedisPubSubServer (
20
31
clientsManager ,
21
- "topic:test" )
32
+ channels )
22
33
{
23
34
HeartbeatInterval = TimeSpan . FromSeconds ( intervalSecs ) ,
24
35
HeartbeatTimeout = TimeSpan . FromSeconds ( timeoutSecs )
@@ -97,5 +108,42 @@ public void Does_send_heartbeat_pulses_to_multiple_PubSubServers()
97
108
98
109
pubSubs . Each ( x => x . Dispose ( ) ) ;
99
110
}
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
+ }
100
148
}
101
149
}
0 commit comments