This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 870
/
Copy pathRedisManagerPoolTests.cs
246 lines (212 loc) · 8.17 KB
/
RedisManagerPoolTests.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;
using ServiceStack.Text;
#if NETCORE
using System.Threading.Tasks;
#endif
namespace ServiceStack.Redis.Tests
{
[TestFixture, Category("Integration")]
public class RedisManagerPoolTests
{
readonly string[] hosts = new[] {
"readwrite1", "readwrite2:6000", "192.168.0.1", "localhost"
};
readonly string[] testReadOnlyHosts = new[] {
"read1", "read2:7000", "127.0.0.1"
};
private string firstReadWriteHost;
private string firstReadOnlyHost;
[OneTimeSetUp]
public void OneTimeSetUp()
{
RedisConfig.VerifyMasterConnections = false;
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
RedisConfig.VerifyMasterConnections = true;
}
[SetUp]
public void OnBeforeEachTest()
{
firstReadWriteHost = hosts[0];
firstReadOnlyHost = testReadOnlyHosts[0];
}
public RedisManagerPool CreateManager()
{
return new RedisManagerPool(hosts);
}
[Test]
public void Can_change_db_for_client()
{
using (var db1 = new RedisManagerPool(TestConfig.SingleHost + "?db=1"))
using (var db2 = new RedisManagerPool(TestConfig.SingleHost + "?db=2"))
{
var val = Environment.TickCount;
var key = "test" + val;
var db1c = db1.GetClient();
var db2c = db2.GetClient();
try
{
db1c.Set(key, val);
Assert.That(db2c.Get<int>(key), Is.EqualTo(0));
Assert.That(db1c.Get<int>(key), Is.EqualTo(val));
}
finally
{
db1c.Remove(key);
}
}
}
[Test]
public void Can_get_ReadWrite_client()
{
using (var manager = CreateManager())
{
var client = manager.GetClient();
AssertClientHasHost(client, firstReadWriteHost);
}
}
private static void AssertClientHasHost(IRedisClient client, string hostWithOptionalPort)
{
var parts = hostWithOptionalPort.Split(':');
var port = parts.Length > 1 ? int.Parse(parts[1]) : RedisConfig.DefaultPort;
Assert.That(client.Host, Is.EqualTo(parts[0]));
Assert.That(client.Port, Is.EqualTo(port));
}
[Test]
public void Does_loop_through_ReadWrite_hosts()
{
using (var manager = CreateManager())
{
var client1 = manager.GetClient();
client1.Dispose();
var client2 = manager.GetClient();
var client3 = manager.GetClient();
var client4 = manager.GetClient();
var client5 = manager.GetClient();
AssertClientHasHost(client1, hosts[0]);
AssertClientHasHost(client2, hosts[1]);
AssertClientHasHost(client3, hosts[2]);
AssertClientHasHost(client4, hosts[3]);
AssertClientHasHost(client5, hosts[0]);
}
}
[Test]
public void Can_have_different_pool_size_and_host_configurations()
{
var writeHosts = new[] { "readwrite1" };
using (var manager = new RedisManagerPool(
writeHosts,
new RedisPoolConfig { MaxPoolSize = 4 }))
{
//A poolsize of 4 will not block getting 4 clients
using (var client1 = manager.GetClient())
using (var client2 = manager.GetClient())
using (var client3 = manager.GetClient())
using (var client4 = manager.GetClient())
{
AssertClientHasHost(client1, writeHosts[0]);
AssertClientHasHost(client2, writeHosts[0]);
AssertClientHasHost(client3, writeHosts[0]);
AssertClientHasHost(client4, writeHosts[0]);
}
}
}
[Test]
public void Does_not_block_ReadWrite_clients_pool()
{
using (var manager = new RedisManagerPool(
hosts,
new RedisPoolConfig { MaxPoolSize = 4 }))
{
var delay = TimeSpan.FromSeconds(1);
var client1 = manager.GetClient();
var client2 = manager.GetClient();
var client3 = manager.GetClient();
var client4 = manager.GetClient();
Assert.That(((RedisClient)client1).IsManagedClient, Is.True);
Assert.That(((RedisClient)client2).IsManagedClient, Is.True);
Assert.That(((RedisClient)client3).IsManagedClient, Is.True);
Assert.That(((RedisClient)client4).IsManagedClient, Is.True);
Action func = delegate
{
Thread.Sleep(delay + TimeSpan.FromSeconds(0.5));
client4.Dispose();
};
#if NETCORE
Task.Run(func);
#else
func.BeginInvoke(null, null);
#endif
var start = DateTime.Now;
var client5 = manager.GetClient();
Assert.That(((RedisClient)client5).IsManagedClient, Is.False); //outside of pool
Assert.That(DateTime.Now - start, Is.LessThan(delay));
AssertClientHasHost(client1, hosts[0]);
AssertClientHasHost(client2, hosts[1]);
AssertClientHasHost(client3, hosts[2]);
AssertClientHasHost(client4, hosts[3]);
AssertClientHasHost(client5, hosts[0]);
}
}
[Test]
public void Can_support_64_threads_using_the_client_simultaneously()
{
const int noOfConcurrentClients = 64; //WaitHandle.WaitAll limit is <= 64
var clientUsageMap = new Dictionary<string, int>();
#if NETCORE
List<Task> tasks = new List<Task>();
#else
var clientAsyncResults = new List<IAsyncResult>();
#endif
using (var manager = CreateManager())
{
for (var i = 0; i < noOfConcurrentClients; i++)
{
var clientNo = i;
var action = (Action)(() => UseClient(manager, clientNo, clientUsageMap));
#if NETCORE
tasks.Add(Task.Run(action));
#else
clientAsyncResults.Add(action.BeginInvoke(null, null));
#endif
}
}
#if NETCORE
Task.WaitAll(tasks.ToArray());
#else
WaitHandle.WaitAll(clientAsyncResults.ConvertAll(x => x.AsyncWaitHandle).ToArray());
#endif
Debug.WriteLine(TypeSerializer.SerializeToString(clientUsageMap));
var hostCount = 0;
foreach (var entry in clientUsageMap)
{
Assert.That(entry.Value, Is.GreaterThanOrEqualTo(5), "Host has unproportionate distribution: " + entry.Value);
Assert.That(entry.Value, Is.LessThanOrEqualTo(30), "Host has unproportionate distribution: " + entry.Value);
hostCount += entry.Value;
}
Assert.That(hostCount, Is.EqualTo(noOfConcurrentClients), "Invalid no of clients used");
}
private static void UseClient(IRedisClientsManager manager, int clientNo, Dictionary<string, int> hostCountMap)
{
using (var client = manager.GetClient())
{
lock (hostCountMap)
{
int hostCount;
if (!hostCountMap.TryGetValue(client.Host, out hostCount))
{
hostCount = 0;
}
hostCountMap[client.Host] = ++hostCount;
}
Debug.WriteLine(String.Format("Client '{0}' is using '{1}'", clientNo, client.Host));
}
}
}
}