This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 876
/
SimplePubSub.cs
186 lines (167 loc) · 7.05 KB
/
SimplePubSub.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;
using ServiceStack.Common;
namespace ServiceStack.Redis.Tests.Examples
{
[TestFixture, Ignore("Integration"), Category("Integration")]
public class SimplePubSub
{
const string ChannelName = "SimplePubSubCHANNEL";
const string MessagePrefix = "MESSAGE ";
const int PublishMessageCount = 5;
[OneTimeSetUp]
public void OneTimeSetUp()
{
using (var redis = new RedisClient(TestConfig.SingleHost))
{
redis.FlushAll();
}
}
[Test]
public void Publish_and_receive_5_messages()
{
var messagesReceived = 0;
using (var redisConsumer = new RedisClient(TestConfig.SingleHost))
using (var subscription = redisConsumer.CreateSubscription())
{
subscription.OnSubscribe = channel =>
{
Debug.WriteLine(String.Format("Subscribed to '{0}'", channel));
};
subscription.OnUnSubscribe = channel =>
{
Debug.WriteLine(String.Format("UnSubscribed from '{0}'", channel));
};
subscription.OnMessage = (channel, msg) =>
{
Debug.WriteLine(String.Format("Received '{0}' from channel '{1}'", msg, channel));
//As soon as we've received all 5 messages, disconnect by unsubscribing to all channels
if (++messagesReceived == PublishMessageCount)
{
subscription.UnSubscribeFromAllChannels();
}
};
ThreadPool.QueueUserWorkItem(x =>
{
Thread.Sleep(200);
Debug.WriteLine("Begin publishing messages...");
using (var redisPublisher = new RedisClient(TestConfig.SingleHost))
{
for (var i = 1; i <= PublishMessageCount; i++)
{
var message = MessagePrefix + i;
Debug.WriteLine(String.Format("Publishing '{0}' to '{1}'", message, ChannelName));
redisPublisher.PublishMessage(ChannelName, message);
}
}
});
Debug.WriteLine(String.Format("Started Listening On '{0}'", ChannelName));
subscription.SubscribeToChannels(ChannelName); //blocking
}
Debug.WriteLine("EOF");
/*Output:
Started Listening On 'CHANNEL'
Subscribed to 'CHANNEL'
Begin publishing messages...
Publishing 'MESSAGE 1' to 'CHANNEL'
Received 'MESSAGE 1' from channel 'CHANNEL'
Publishing 'MESSAGE 2' to 'CHANNEL'
Received 'MESSAGE 2' from channel 'CHANNEL'
Publishing 'MESSAGE 3' to 'CHANNEL'
Received 'MESSAGE 3' from channel 'CHANNEL'
Publishing 'MESSAGE 4' to 'CHANNEL'
Received 'MESSAGE 4' from channel 'CHANNEL'
Publishing 'MESSAGE 5' to 'CHANNEL'
Received 'MESSAGE 5' from channel 'CHANNEL'
UnSubscribed from 'CHANNEL'
EOF
*/
}
[Test]
public void Publish_5_messages_to_3_clients()
{
const int noOfClients = 3;
for (var i = 1; i <= noOfClients; i++)
{
var clientNo = i;
ThreadPool.QueueUserWorkItem(x =>
{
using (var redisConsumer = new RedisClient(TestConfig.SingleHost))
using (var subscription = redisConsumer.CreateSubscription())
{
var messagesReceived = 0;
subscription.OnSubscribe = channel =>
{
Debug.WriteLine(String.Format("Client #{0} Subscribed to '{1}'", clientNo, channel));
};
subscription.OnUnSubscribe = channel =>
{
Debug.WriteLine(String.Format("Client #{0} UnSubscribed from '{1}'", clientNo, channel));
};
subscription.OnMessage = (channel, msg) =>
{
Debug.WriteLine(String.Format("Client #{0} Received '{1}' from channel '{2}'",
clientNo, msg, channel));
if (++messagesReceived == PublishMessageCount)
{
subscription.UnSubscribeFromAllChannels();
}
};
Debug.WriteLine(String.Format("Client #{0} started Listening On '{1}'", clientNo, ChannelName));
subscription.SubscribeToChannels(ChannelName); //blocking
}
Debug.WriteLine(String.Format("Client #{0} EOF", clientNo));
});
}
using (var redisClient = new RedisClient(TestConfig.SingleHost))
{
Thread.Sleep(500);
Debug.WriteLine("Begin publishing messages...");
for (var i = 1; i <= PublishMessageCount; i++)
{
var message = MessagePrefix + i;
Debug.WriteLine(String.Format("Publishing '{0}' to '{1}'", message, ChannelName));
redisClient.PublishMessage(ChannelName, message);
}
}
Thread.Sleep(500);
/*Output:
Client #1 started Listening On 'CHANNEL'
Client #2 started Listening On 'CHANNEL'
Client #1 Subscribed to 'CHANNEL'
Client #2 Subscribed to 'CHANNEL'
Client #3 started Listening On 'CHANNEL'
Client #3 Subscribed to 'CHANNEL'
Begin publishing messages...
Publishing 'MESSAGE 1' to 'CHANNEL'
Client #1 Received 'MESSAGE 1' from channel 'CHANNEL'
Client #2 Received 'MESSAGE 1' from channel 'CHANNEL'
Publishing 'MESSAGE 2' to 'CHANNEL'
Client #1 Received 'MESSAGE 2' from channel 'CHANNEL'
Client #2 Received 'MESSAGE 2' from channel 'CHANNEL'
Publishing 'MESSAGE 3' to 'CHANNEL'
Client #3 Received 'MESSAGE 1' from channel 'CHANNEL'
Client #3 Received 'MESSAGE 2' from channel 'CHANNEL'
Client #3 Received 'MESSAGE 3' from channel 'CHANNEL'
Client #1 Received 'MESSAGE 3' from channel 'CHANNEL'
Client #2 Received 'MESSAGE 3' from channel 'CHANNEL'
Publishing 'MESSAGE 4' to 'CHANNEL'
Client #1 Received 'MESSAGE 4' from channel 'CHANNEL'
Client #3 Received 'MESSAGE 4' from channel 'CHANNEL'
Publishing 'MESSAGE 5' to 'CHANNEL'
Client #1 Received 'MESSAGE 5' from channel 'CHANNEL'
Client #3 Received 'MESSAGE 5' from channel 'CHANNEL'
Client #1 UnSubscribed from 'CHANNEL'
Client #1 EOF
Client #3 UnSubscribed from 'CHANNEL'
Client #3 EOF
Client #2 Received 'MESSAGE 4' from channel 'CHANNEL'
Client #2 Received 'MESSAGE 5' from channel 'CHANNEL'
Client #2 UnSubscribed from 'CHANNEL'
Client #2 EOF
*/
}
}
}