-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathLockingTests.cs
More file actions
212 lines (184 loc) · 7.65 KB
/
LockingTests.cs
File metadata and controls
212 lines (184 loc) · 7.65 KB
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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace StackExchange.Redis.Tests;
[Collection(NonParallelCollection.Name)]
public class LockingTests : TestBase
{
protected override string GetConfiguration() => TestConfig.Current.PrimaryServerAndPort;
public LockingTests(ITestOutputHelper output) : base(output) { }
public enum TestMode
{
MultiExec,
NoMultiExec,
Twemproxy,
}
public static IEnumerable<object[]> TestModes()
{
yield return new object[] { TestMode.MultiExec };
yield return new object[] { TestMode.NoMultiExec };
yield return new object[] { TestMode.Twemproxy };
}
[Theory, MemberData(nameof(TestModes))]
public void AggressiveParallel(TestMode testMode)
{
int count = 2;
int errorCount = 0;
int bgErrorCount = 0;
var evt = new ManualResetEvent(false);
var key = Me() + testMode;
using (var conn1 = Create(testMode))
using (var conn2 = Create(testMode))
{
void Inner(object? obj)
{
try
{
var conn = (IDatabase?)obj!;
conn.Multiplexer.ErrorMessage += (sender, e) => Interlocked.Increment(ref errorCount);
for (int i = 0; i < 1000; i++)
{
conn.LockTakeAsync(key, "def", TimeSpan.FromSeconds(5));
}
conn.Ping();
if (Interlocked.Decrement(ref count) == 0) evt.Set();
}
catch
{
Interlocked.Increment(ref bgErrorCount);
}
}
int db = testMode == TestMode.Twemproxy ? 0 : 2;
ThreadPool.QueueUserWorkItem(Inner, conn1.GetDatabase(db));
ThreadPool.QueueUserWorkItem(Inner, conn2.GetDatabase(db));
evt.WaitOne(8000);
}
Assert.Equal(0, Interlocked.CompareExchange(ref errorCount, 0, 0));
Assert.Equal(0, bgErrorCount);
}
[Fact]
public void TestOpCountByVersionLocal_UpLevel()
{
using var conn = Create(shared: false);
TestLockOpCountByVersion(conn, 1, false);
TestLockOpCountByVersion(conn, 1, true);
}
private void TestLockOpCountByVersion(IConnectionMultiplexer conn, int expectedOps, bool existFirst)
{
const int LockDuration = 30;
RedisKey key = Me();
var db = conn.GetDatabase();
db.KeyDelete(key, CommandFlags.FireAndForget);
RedisValue newVal = "us:" + Guid.NewGuid().ToString();
RedisValue expectedVal = newVal;
if (existFirst)
{
expectedVal = "other:" + Guid.NewGuid().ToString();
db.StringSet(key, expectedVal, TimeSpan.FromSeconds(LockDuration), flags: CommandFlags.FireAndForget);
}
long countBefore = GetServer(conn).GetCounters().Interactive.OperationCount;
var taken = db.LockTake(key, newVal, TimeSpan.FromSeconds(LockDuration));
long countAfter = GetServer(conn).GetCounters().Interactive.OperationCount;
var valAfter = db.StringGet(key);
Assert.Equal(!existFirst, taken);
Assert.Equal(expectedVal, valAfter);
// note we get a ping from GetCounters
Assert.True(countAfter - countBefore >= expectedOps, $"({countAfter} - {countBefore}) >= {expectedOps}");
}
private IConnectionMultiplexer Create(TestMode mode) => mode switch
{
TestMode.MultiExec => Create(),
TestMode.NoMultiExec => Create(disabledCommands: new[] { "multi", "exec" }),
TestMode.Twemproxy => Create(proxy: Proxy.Twemproxy),
_ => throw new NotSupportedException(mode.ToString()),
};
[Theory, MemberData(nameof(TestModes))]
public async Task TakeLockAndExtend(TestMode testMode)
{
using var conn = Create(testMode);
RedisValue right = Guid.NewGuid().ToString(),
wrong = Guid.NewGuid().ToString();
int dbId = testMode == TestMode.Twemproxy ? 0 : 7;
RedisKey key = Me() + testMode;
var db = conn.GetDatabase(dbId);
db.KeyDelete(key, CommandFlags.FireAndForget);
bool withTran = testMode == TestMode.MultiExec;
var t1 = db.LockTakeAsync(key, right, TimeSpan.FromSeconds(20));
var t1b = db.LockTakeAsync(key, wrong, TimeSpan.FromSeconds(10));
var t2 = db.LockQueryAsync(key);
var t3 = withTran ? db.LockReleaseAsync(key, wrong) : null;
var t4 = db.LockQueryAsync(key);
var t5 = withTran ? db.LockExtendAsync(key, wrong, TimeSpan.FromSeconds(60)) : null;
var t6 = db.LockQueryAsync(key);
var t7 = db.KeyTimeToLiveAsync(key);
var t8 = db.LockExtendAsync(key, right, TimeSpan.FromSeconds(60));
var t9 = db.LockQueryAsync(key);
var t10 = db.KeyTimeToLiveAsync(key);
var t11 = db.LockReleaseAsync(key, right);
var t12 = db.LockQueryAsync(key);
var t13 = db.LockTakeAsync(key, wrong, TimeSpan.FromSeconds(10));
Assert.NotEqual(default(RedisValue), right);
Assert.NotEqual(default(RedisValue), wrong);
Assert.NotEqual(right, wrong);
Assert.True(await t1, "1");
Assert.False(await t1b, "1b");
Assert.Equal(right, await t2);
if (withTran) Assert.False(await t3!, "3");
Assert.Equal(right, await t4);
if (withTran) Assert.False(await t5!, "5");
Assert.Equal(right, await t6);
var ttl = (await t7)!.Value.TotalSeconds;
Assert.True(ttl > 0 && ttl <= 20, "7");
Assert.True(await t8, "8");
Assert.Equal(right, await t9);
ttl = (await t10)!.Value.TotalSeconds;
Assert.True(ttl > 50 && ttl <= 60, "10");
Assert.True(await t11, "11");
Assert.Null((string?)await t12);
Assert.True(await t13, "13");
}
[Theory, MemberData(nameof(TestModes))]
public async Task TestBasicLockNotTaken(TestMode testMode)
{
using var conn = Create(testMode);
int errorCount = 0;
conn.ErrorMessage += (sender, e) => Interlocked.Increment(ref errorCount);
Task<bool>? taken = null;
Task<RedisValue>? newValue = null;
Task<TimeSpan?>? ttl = null;
const int LOOP = 50;
var db = conn.GetDatabase();
var key = Me() + testMode;
for (int i = 0; i < LOOP; i++)
{
_ = db.KeyDeleteAsync(key);
taken = db.LockTakeAsync(key, "new-value", TimeSpan.FromSeconds(10));
newValue = db.StringGetAsync(key);
ttl = db.KeyTimeToLiveAsync(key);
}
Assert.True(await taken!, "taken");
Assert.Equal("new-value", await newValue!);
var ttlValue = (await ttl!)!.Value.TotalSeconds;
Assert.True(ttlValue >= 8 && ttlValue <= 10, "ttl");
Assert.Equal(0, errorCount);
}
[Theory, MemberData(nameof(TestModes))]
public async Task TestBasicLockTaken(TestMode testMode)
{
using var conn = Create(testMode);
var db = conn.GetDatabase();
var key = Me() + testMode;
db.KeyDelete(key, CommandFlags.FireAndForget);
db.StringSet(key, "old-value", TimeSpan.FromSeconds(20), flags: CommandFlags.FireAndForget);
var taken = db.LockTakeAsync(key, "new-value", TimeSpan.FromSeconds(10));
var newValue = db.StringGetAsync(key);
var ttl = db.KeyTimeToLiveAsync(key);
Assert.False(await taken, "taken");
Assert.Equal("old-value", await newValue);
var ttlValue = (await ttl)!.Value.TotalSeconds;
Assert.True(ttlValue >= 18 && ttlValue <= 20, "ttl");
}
}