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 pathRetryCommandTests.Async.cs
143 lines (112 loc) · 5.11 KB
/
RetryCommandTests.Async.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
using NUnit.Framework;
using System;
using System.Linq;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace ServiceStack.Redis.Tests
{
[TestFixture, Category("Async")]
public class RetryCommandTestsAsync
{
[Test, Ignore("3 vs 2 needs investigation; does same in non-async")]
public async Task Does_retry_failed_commands()
{
// warning: this test looks brittle; is often failing "Expected: 3 But was: 2" (on main branch);
// LogManager.LogFactory = new ConsoleLogFactory(debugEnabled: true);
// RedisConfig.EnableVerboseLogging = true;
RedisStats.Reset();
var redisCtrl = new RedisClient(RedisConfig.DefaultHost).ForAsyncOnly();
await redisCtrl.FlushAllAsync();
await redisCtrl.SetClientAsync("redisCtrl");
var redis = new RedisClient(RedisConfig.DefaultHost).ForAsyncOnly();
await redis.SetClientAsync("redisRetry");
var clientInfo = await redisCtrl.GetClientsInfoAsync();
var redisId = clientInfo.First(m => m["name"] == "redisRetry")["id"];
Assert.That(redisId.Length, Is.GreaterThan(0));
Assert.That(await redis.IncrementValueAsync("retryCounter"), Is.EqualTo(1));
((RedisClient)redis).OnBeforeFlush = () =>
{
((IRedisClient)redisCtrl).KillClients(withId: redisId);
};
Assert.That(await redis.IncrementValueAsync("retryCounter"), Is.EqualTo(2));
Assert.That(await redis.GetAsync<int>("retryCounter"), Is.EqualTo(3));
Assert.That(RedisStats.TotalRetryCount, Is.EqualTo(1));
Assert.That(RedisStats.TotalRetrySuccess, Is.EqualTo(1));
Assert.That(RedisStats.TotalRetryTimedout, Is.EqualTo(0));
}
[Test]
public async Task Does_retry_failed_commands_with_SocketException()
{
RedisStats.Reset();
var redis = new RedisClient(RedisConfig.DefaultHost).ForAsyncOnly();
await redis.FlushAllAsync();
Assert.That(await redis.IncrementValueAsync("retryCounter"), Is.EqualTo(1));
((RedisClient)redis).OnBeforeFlush = () =>
{
((RedisClient)redis).OnBeforeFlush = null;
throw new SocketException();
};
Assert.That(await redis.IncrementValueAsync("retryCounter"), Is.EqualTo(2));
Assert.That(await redis.GetAsync<int>("retryCounter"), Is.EqualTo(3));
Assert.That(RedisStats.TotalRetryCount, Is.EqualTo(1));
Assert.That(RedisStats.TotalRetrySuccess, Is.EqualTo(1));
Assert.That(RedisStats.TotalRetryTimedout, Is.EqualTo(0));
}
[Test]
public async Task Does_Timeout_with_repeated_SocketException()
{
RedisConfig.Reset();
RedisConfig.DefaultRetryTimeout = 100;
var redis = new RedisClient(RedisConfig.DefaultHost).ForAsyncOnly();
await redis.FlushAllAsync();
Assert.That(await redis.IncrementValueAsync("retryCounter"), Is.EqualTo(1));
((RedisClient)redis).OnBeforeFlush = () =>
{
throw new SocketException();
};
try
{
await redis.IncrementValueAsync("retryCounter");
Assert.Fail("Should throw");
}
catch (RedisException ex)
{
Assert.That(ex.Message, Does.StartWith("Exceeded timeout"));
((RedisClient)redis).OnBeforeFlush = null;
Assert.That(await redis.GetAsync<int>("retryCounter"), Is.EqualTo(1));
Assert.That(RedisStats.TotalRetryCount, Is.GreaterThan(1));
Assert.That(RedisStats.TotalRetrySuccess, Is.EqualTo(0));
Assert.That(RedisStats.TotalRetryTimedout, Is.EqualTo(1));
}
RedisConfig.Reset();
}
[Test]
public async Task Does_not_retry_when_RetryTimeout_is_Zero()
{
RedisConfig.Reset();
RedisConfig.DefaultRetryTimeout = 0;
var redis = new RedisClient(RedisConfig.DefaultHost).ForAsyncOnly();
await redis.FlushAllAsync();
Assert.That(await redis.IncrementValueAsync("retryCounter"), Is.EqualTo(1));
((RedisClient)redis).OnBeforeFlush = () =>
{
throw new SocketException();
};
try
{
await redis.IncrementValueAsync("retryCounter");
Assert.Fail("Should throw");
}
catch (Exception ex)
{
Assert.That(ex.Message, Does.StartWith("Exceeded timeout"));
((RedisClient)redis).OnBeforeFlush = null;
Assert.That(await redis.GetAsync<int>("retryCounter"), Is.EqualTo(1));
Assert.That(RedisStats.TotalRetryCount, Is.EqualTo(0));
Assert.That(RedisStats.TotalRetrySuccess, Is.EqualTo(0));
Assert.That(RedisStats.TotalRetryTimedout, Is.EqualTo(1));
}
RedisConfig.Reset();
}
}
}