Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
StackExchange.Redis
Commits
45d7ba62
Commit
45d7ba62
authored
Apr 01, 2014
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrated: BookSleeve locking tests
parent
6218102c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
241 additions
and
228 deletions
+241
-228
Locking.cs
MigratedBookSleeveTestSuite/Locking.cs
+241
-228
No files found.
MigratedBookSleeveTestSuite/Locking.cs
View file @
45d7ba62
//using System;
using
System
;
//using System.Collections.Generic;
using
System.Collections.Generic
;
//using System.Linq;
using
System.Threading
;
//using System.Text;
using
System.Threading.Tasks
;
//using BookSleeve;
using
NUnit.Framework
;
//using NUnit.Framework;
using
StackExchange.Redis
;
//using System.Threading;
//using System.Threading.Tasks;
namespace
Tests
{
//namespace Tests
[
TestFixture
]
//{
public
class
Locking
// [TestFixture]
{
// public class Locking
[
Test
]
// {
public
void
AggressiveParallel
()
// [Test]
{
// public void AggressiveParallel()
int
count
=
2
;
// {
int
errorCount
=
0
;
// int count = 2;
ManualResetEvent
evt
=
new
ManualResetEvent
(
false
);
// int errorCount = 0;
using
(
var
c1
=
Config
.
GetUnsecuredConnection
(
waitForOpen
:
true
))
// ManualResetEvent evt = new ManualResetEvent(false);
using
(
var
c2
=
Config
.
GetUnsecuredConnection
(
waitForOpen
:
true
))
// using (var c1 = Config.GetUnsecuredConnection(waitForOpen: true))
{
// using (var c2 = Config.GetUnsecuredConnection(waitForOpen: true))
WaitCallback
cb
=
obj
=>
// {
{
// WaitCallback cb = obj =>
var
conn
=
(
ConnectionMultiplexer
)
obj
;
// {
conn
.
InternalError
+=
delegate
{
Interlocked
.
Increment
(
ref
errorCount
);
};
// var conn = (RedisConnection)obj;
conn
.
ErrorMessage
+=
delegate
{
Interlocked
.
Increment
(
ref
errorCount
);
};
// conn.Error += delegate { Interlocked.Increment(ref errorCount); };
var
db
=
conn
.
GetDatabase
(
2
);
// for(int i = 0 ; i < 1000 ; i++)
for
(
int
i
=
0
;
i
<
1000
;
i
++)
// {
{
// conn.Strings.TakeLock(2, "abc", "def", 5);
db
.
LockTake
(
"abc"
,
"def"
,
TimeSpan
.
FromSeconds
(
5
));
// }
}
// conn.Wait(conn.Server.Ping());
db
.
Ping
();
// conn.Close(false);
conn
.
Close
(
false
);
// if (Interlocked.Decrement(ref count) == 0) evt.Set();
if
(
Interlocked
.
Decrement
(
ref
count
)
==
0
)
evt
.
Set
();
// };
};
// ThreadPool.QueueUserWorkItem(cb, c1);
ThreadPool
.
QueueUserWorkItem
(
cb
,
c1
);
// ThreadPool.QueueUserWorkItem(cb, c2);
ThreadPool
.
QueueUserWorkItem
(
cb
,
c2
);
// evt.WaitOne(8000);
evt
.
WaitOne
(
8000
);
// }
}
// Assert.AreEqual(0, Interlocked.CompareExchange(ref errorCount, 0, 0));
Assert
.
AreEqual
(
0
,
Interlocked
.
CompareExchange
(
ref
errorCount
,
0
,
0
));
// }
}
// [Test]
[
Test
]
// public void TestOpCountByVersionLocal_UpLevel()
public
void
TestOpCountByVersionLocal_UpLevel
()
// {
{
// using (var conn = Config.GetUnsecuredConnection(open: false))
using
(
var
conn
=
Config
.
GetUnsecuredConnection
(
open
:
false
))
// {
{
// TestLockOpCountByVersion(conn, 1, false);
TestLockOpCountByVersion
(
conn
,
1
,
false
);
// TestLockOpCountByVersion(conn, 1, true);
TestLockOpCountByVersion
(
conn
,
1
,
true
);
// //TestManualLockOpCountByVersion(conn, 5, false);
//TestManualLockOpCountByVersion(conn, 5, false);
// //TestManualLockOpCountByVersion(conn, 3, true);
//TestManualLockOpCountByVersion(conn, 3, true);
// }
}
// }
}
// [Test]
//[Test]
// public void TestOpCountByVersionLocal_DownLevel()
//public void TestOpCountByVersionLocal_DownLevel()
// {
//{
// using (var conn = Config.GetUnsecuredConnection(open: false))
// var config = new ConfigurationOptions
// {
// {
// conn.SetServerVersion(new Version(2, 6, 0), ServerType.Master);
// EndPoints = { { Config.LocalHost } },
// TestLockOpCountByVersion(conn, 5, false);
// DefaultVersion = new Version(2, 6, 0),
// TestLockOpCountByVersion(conn, 3, true);
// CommandMap = CommandMap.Create(
// //TestManualLockOpCountByVersion(conn, 5, false);
// new HashSet<string> { "info" }, false)
// //TestManualLockOpCountByVersion(conn, 3, true);
// };
// }
// using (var conn = ConnectionMultiplexer.Connect(config))
// }
// {
// TestLockOpCountByVersion(conn, 5, false);
// [Test]
// TestLockOpCountByVersion(conn, 3, true);
// public void TestOpCountByVersionRemote()
// //TestManualLockOpCountByVersion(conn, 5, false);
// {
// //TestManualLockOpCountByVersion(conn, 3, true);
// using (var conn = Config.GetRemoteConnection(open:false))
// }
// {
//}
// TestLockOpCountByVersion(conn, 1, false);
// TestLockOpCountByVersion(conn, 1, true);
//[Test]
// //TestManualLockOpCountByVersion(conn, 1, false);
//public void TestOpCountByVersionRemote()
// //TestManualLockOpCountByVersion(conn, 1, true);
//{
// }
// using (var conn = Config.GetRemoteConnection(open: false))
// }
// {
// public void TestLockOpCountByVersion(RedisConnection conn, int expected, bool existFirst)
// TestLockOpCountByVersion(conn, 1, false);
// {
// TestLockOpCountByVersion(conn, 1, true);
// const int DB = 0, LockDuration = 30;
// //TestManualLockOpCountByVersion(conn, 1, false);
// const string Key = "TestOpCountByVersion";
// //TestManualLockOpCountByVersion(conn, 1, true);
// conn.Wait(conn.Open());
// }
// conn.Keys.Remove(DB, Key);
//}
// var newVal = "us:" + Config.CreateUniqueName();
public
void
TestLockOpCountByVersion
(
ConnectionMultiplexer
conn
,
int
expected
,
bool
existFirst
)
// string expectedVal = newVal;
{
// if (existFirst)
const
int
DB
=
0
,
LockDuration
=
30
;
// {
const
string
Key
=
"TestOpCountByVersion"
;
// expectedVal = "other:" + Config.CreateUniqueName();
// conn.Strings.Set(DB, Key, expectedVal, LockDuration);
var
db
=
conn
.
GetDatabase
(
DB
);
// }
db
.
KeyDelete
(
Key
);
// int countBefore = conn.GetCounters().MessagesSent;
var
newVal
=
"us:"
+
Config
.
CreateUniqueName
();
// var taken = conn.Wait(conn.Strings.TakeLock(DB, Key, newVal, LockDuration));
string
expectedVal
=
newVal
;
// int countAfter = conn.GetCounters().MessagesSent;
if
(
existFirst
)
// var valAfter = conn.Wait(conn.Strings.GetString(DB, Key));
{
// Assert.AreEqual(!existFirst, taken, "lock taken");
expectedVal
=
"other:"
+
Config
.
CreateUniqueName
();
// Assert.AreEqual(expectedVal, valAfter, "taker");
db
.
StringSet
(
Key
,
expectedVal
,
TimeSpan
.
FromSeconds
(
LockDuration
));
// Assert.AreEqual(expected, (countAfter - countBefore) - 1, "expected ops (including ping)");
}
// // note we get a ping from GetCounters
long
countBefore
=
conn
.
GetCounters
().
Interactive
.
OperationCount
;
// }
var
taken
=
db
.
LockTake
(
Key
,
newVal
,
TimeSpan
.
FromSeconds
(
LockDuration
));
long
countAfter
=
conn
.
GetCounters
().
Interactive
.
OperationCount
;
// [Test]
string
valAfter
=
db
.
StringGet
(
Key
);
// public void TakeLockAndExtend()
Assert
.
AreEqual
(!
existFirst
,
taken
,
"lock taken"
);
// {
Assert
.
AreEqual
(
expectedVal
,
valAfter
,
"taker"
);
// using (var conn = Config.GetUnsecuredConnection())
Console
.
WriteLine
(
"{0} ops before, {1} ops after"
,
countBefore
,
countAfter
);
// {
Assert
.
AreEqual
(
expected
,
(
countAfter
-
countBefore
),
"expected ops (including ping)"
);
// string right = Guid.NewGuid().ToString(),
// note we get a ping from GetCounters
// wrong = Guid.NewGuid().ToString();
}
// const int DB = 7;
[
Test
]
// const string Key = "lock-key";
public
void
TakeLockAndExtend
()
{
// conn.SuspendFlush();
using
(
var
conn
=
Config
.
GetUnsecuredConnection
())
{
// conn.Keys.Remove(DB, Key);
string
right
=
Guid
.
NewGuid
().
ToString
(),
// var t1 = conn.Strings.TakeLock(DB, Key, right, 20);
wrong
=
Guid
.
NewGuid
().
ToString
();
// var t1b = conn.Strings.TakeLock(DB, Key, wrong, 10);
// var t2 = conn.Strings.GetString(DB, Key);
const
int
DB
=
7
;
// var t3 = conn.Strings.ReleaseLock(DB, Key, wrong);
const
string
Key
=
"lock-key"
;
// var t4 = conn.Strings.GetString(DB, Key);
// var t5 = conn.Strings.ExtendLock(DB, Key, wrong, 60);
//conn.SuspendFlush();
// var t6 = conn.Strings.GetString(DB, Key);
var
db
=
conn
.
GetDatabase
(
DB
);
// var t7 = conn.Keys.TimeToLive(DB, Key);
// var t8 = conn.Strings.ExtendLock(DB, Key, right, 60);
db
.
KeyDelete
(
Key
);
// var t9 = conn.Strings.GetString(DB, Key);
var
t1
=
db
.
LockTakeAsync
(
Key
,
right
,
TimeSpan
.
FromSeconds
(
20
));
// var t10 = conn.Keys.TimeToLive(DB, Key);
var
t1b
=
db
.
LockTakeAsync
(
Key
,
wrong
,
TimeSpan
.
FromSeconds
(
10
));
// var t11 = conn.Strings.ReleaseLock(DB, Key, right);
var
t2
=
db
.
StringGetAsync
(
Key
);
// var t12 = conn.Strings.GetString(DB, Key);
var
t3
=
db
.
LockReleaseAsync
(
Key
,
wrong
);
// var t13 = conn.Strings.TakeLock(DB, Key, wrong, 10);
var
t4
=
db
.
StringGetAsync
(
Key
);
// conn.ResumeFlush();
var
t5
=
db
.
LockExtendAsync
(
Key
,
wrong
,
TimeSpan
.
FromSeconds
(
60
));
// Assert.IsNotNull(right);
var
t6
=
db
.
StringGetAsync
(
Key
);
// Assert.IsNotNull(wrong);
var
t7
=
db
.
KeyTimeToLiveAsync
(
Key
);
// Assert.AreNotEqual(right, wrong);
var
t8
=
db
.
LockExtendAsync
(
Key
,
right
,
TimeSpan
.
FromSeconds
(
60
));
// Assert.IsTrue(conn.Wait(t1), "1");
var
t9
=
db
.
StringGetAsync
(
Key
);
// Assert.IsFalse(conn.Wait(t1b), "1b");
var
t10
=
db
.
KeyTimeToLiveAsync
(
Key
);
// Assert.AreEqual(right, conn.Wait(t2), "2");
var
t11
=
db
.
LockReleaseAsync
(
Key
,
right
);
// Assert.IsFalse(conn.Wait(t3), "3");
var
t12
=
db
.
StringGetAsync
(
Key
);
// Assert.AreEqual(right, conn.Wait(t4), "4");
var
t13
=
db
.
LockTakeAsync
(
Key
,
wrong
,
TimeSpan
.
FromSeconds
(
10
));
// Assert.IsFalse(conn.Wait(t5), "5");
// Assert.AreEqual(right, conn.Wait(t6), "6");
Assert
.
IsNotNull
(
right
);
// var ttl = conn.Wait(t7);
Assert
.
IsNotNull
(
wrong
);
// Assert.IsTrue(ttl > 0 && ttl <= 20, "7");
Assert
.
AreNotEqual
(
right
,
(
string
)
wrong
);
// Assert.IsTrue(conn.Wait(t8), "8");
Assert
.
IsTrue
(
conn
.
Wait
(
t1
),
"1"
);
// Assert.AreEqual(right, conn.Wait(t9), "9");
Assert
.
IsFalse
(
conn
.
Wait
(
t1b
),
"1b"
);
// ttl = conn.Wait(t10);
Assert
.
AreEqual
(
right
,
(
string
)
conn
.
Wait
(
t2
),
"2"
);
// Assert.IsTrue(ttl > 50 && ttl <= 60, "10");
Assert
.
IsFalse
(
conn
.
Wait
(
t3
),
"3"
);
// Assert.IsTrue(conn.Wait(t11), "11");
Assert
.
AreEqual
(
right
,
(
string
)
conn
.
Wait
(
t4
),
"4"
);
// Assert.IsNull(conn.Wait(t12), "12");
Assert
.
IsFalse
(
conn
.
Wait
(
t5
),
"5"
);
// Assert.IsTrue(conn.Wait(t13), "13");
Assert
.
AreEqual
(
right
,
(
string
)
conn
.
Wait
(
t6
),
"6"
);
// }
var
ttl
=
conn
.
Wait
(
t7
).
Value
.
TotalSeconds
;
// }
Assert
.
IsTrue
(
ttl
>
0
&&
ttl
<=
20
,
"7"
);
Assert
.
IsTrue
(
conn
.
Wait
(
t8
),
"8"
);
Assert
.
AreEqual
(
right
,
(
string
)
conn
.
Wait
(
t9
),
"9"
);
// //public void TestManualLockOpCountByVersion(RedisConnection conn, int expected, bool existFirst)
ttl
=
conn
.
Wait
(
t10
).
Value
.
TotalSeconds
;
// //{
Assert
.
IsTrue
(
ttl
>
50
&&
ttl
<=
60
,
"10"
);
// // const int DB = 0, LockDuration = 30;
Assert
.
IsTrue
(
conn
.
Wait
(
t11
),
"11"
);
// // const string Key = "TestManualLockOpCountByVersion";
Assert
.
IsNull
((
string
)
conn
.
Wait
(
t12
),
"12"
);
// // conn.Wait(conn.Open());
Assert
.
IsTrue
(
conn
.
Wait
(
t13
),
"13"
);
// // conn.Keys.Remove(DB, Key);
}
// // var newVal = "us:" + Config.CreateUniqueName();
}
// // string expectedVal = newVal;
// // if (existFirst)
// // {
////public void TestManualLockOpCountByVersion(RedisConnection conn, int expected, bool existFirst)
// // expectedVal = "other:" + Config.CreateUniqueName();
////{
// // conn.Strings.Set(DB, Key, expectedVal, LockDuration);
//// const int DB = 0, LockDuration = 30;
// // }
//// const string Key = "TestManualLockOpCountByVersion";
// // int countBefore = conn.GetCounters().MessagesSent;
//// conn.Wait(conn.Open());
//// conn.Keys.Remove(DB, Key);
// // var tran = conn.CreateTransaction();
//// var newVal = "us:" + Config.CreateUniqueName();
// // tran.AddCondition(Condition.KeyNotExists(DB, Key));
//// string expectedVal = newVal;
// // tran.Strings.Set(DB, Key, newVal, LockDuration);
//// if (existFirst)
// // var taken = conn.Wait(tran.Execute());
//// {
//// expectedVal = "other:" + Config.CreateUniqueName();
// // int countAfter = conn.GetCounters().MessagesSent;
//// conn.Strings.Set(DB, Key, expectedVal, LockDuration);
// // var valAfter = conn.Wait(conn.Strings.GetString(DB, Key));
//// }
// // Assert.AreEqual(!existFirst, taken, "lock taken (manual)");
//// int countBefore = conn.GetCounters().MessagesSent;
// // Assert.AreEqual(expectedVal, valAfter, "taker (manual)");
// // Assert.AreEqual(expected, (countAfter - countBefore) - 1, "expected ops (including ping) (manual)");
//// var tran = conn.CreateTransaction();
// // // note we get a ping from GetCounters
//// tran.AddCondition(Condition.KeyNotExists(DB, Key));
// //}
//// tran.Strings.Set(DB, Key, newVal, LockDuration);
//// var taken = conn.Wait(tran.Execute());
//// int countAfter = conn.GetCounters().MessagesSent;
// [Test]
//// var valAfter = conn.Wait(conn.Strings.GetString(DB, Key));
// public void TestBasicLockNotTaken()
//// Assert.AreEqual(!existFirst, taken, "lock taken (manual)");
// {
//// Assert.AreEqual(expectedVal, valAfter, "taker (manual)");
// using(var conn = Config.GetUnsecuredConnection())
//// Assert.AreEqual(expected, (countAfter - countBefore) - 1, "expected ops (including ping) (manual)");
// {
//// // note we get a ping from GetCounters
// int errorCount = 0;
////}
// conn.Error += delegate { Interlocked.Increment(ref errorCount); };
// Task<bool> taken = null;
// Task<string> newValue = null;
// Task<long> ttl = null;
[
Test
]
public
void
TestBasicLockNotTaken
()
// const int LOOP = 50;
{
// for (int i = 0; i < LOOP; i++)
using
(
var
conn
=
Config
.
GetUnsecuredConnection
())
// {
{
// conn.Keys.Remove(0, "lock-not-exists");
int
errorCount
=
0
;
// taken = conn.Strings.TakeLock(0, "lock-not-exists", "new-value", 10);
// newValue = conn.Strings.GetString(0, "lock-not-exists");
conn
.
InternalError
+=
delegate
{
Interlocked
.
Increment
(
ref
errorCount
);
};
// ttl = conn.Keys.TimeToLive(0, "lock-not-exists");
// }
var
db
=
conn
.
GetDatabase
(
0
);
// Assert.IsTrue(conn.Wait(taken), "taken");
Task
<
bool
>
taken
=
null
;
// Assert.AreEqual("new-value", conn.Wait(newValue));
Task
<
RedisValue
>
newValue
=
null
;
// var ttlValue = conn.Wait(ttl);
Task
<
TimeSpan
?>
ttl
=
null
;
// Assert.IsTrue(ttlValue >= 8 && ttlValue <= 10, "ttl");
const
int
LOOP
=
50
;
// Assert.AreEqual(0, errorCount);
for
(
int
i
=
0
;
i
<
LOOP
;
i
++)
// }
{
// }
db
.
KeyDeleteAsync
(
"lock-not-exists"
);
taken
=
db
.
LockTakeAsync
(
"lock-not-exists"
,
"new-value"
,
TimeSpan
.
FromSeconds
(
10
));
// [Test]
newValue
=
db
.
StringGetAsync
(
"lock-not-exists"
);
// public void TestBasicLockTaken()
ttl
=
db
.
KeyTimeToLiveAsync
(
"lock-not-exists"
);
// {
}
// using (var conn = Config.GetUnsecuredConnection())
Assert
.
IsTrue
(
conn
.
Wait
(
taken
),
"taken"
);
// {
Assert
.
AreEqual
(
"new-value"
,
(
string
)
conn
.
Wait
(
newValue
));
// conn.Keys.Remove(0, "lock-exists");
var
ttlValue
=
conn
.
Wait
(
ttl
).
Value
.
TotalSeconds
;
// conn.Strings.Set(0, "lock-exists", "old-value", expirySeconds: 20);
Assert
.
IsTrue
(
ttlValue
>=
8
&&
ttlValue
<=
10
,
"ttl"
);
// var taken = conn.Strings.TakeLock(0, "lock-exists", "new-value", 10);
// var newValue = conn.Strings.GetString(0, "lock-exists");
Assert
.
AreEqual
(
0
,
errorCount
);
// var ttl = conn.Keys.TimeToLive(0, "lock-exists");
}
}
// Assert.IsFalse(conn.Wait(taken), "taken");
// Assert.AreEqual("old-value", conn.Wait(newValue));
[
Test
]
// var ttlValue = conn.Wait(ttl);
public
void
TestBasicLockTaken
()
// Assert.IsTrue(ttlValue >= 18 && ttlValue <= 20, "ttl");
{
// }
using
(
var
conn
=
Config
.
GetUnsecuredConnection
())
// }
{
// }
var
db
=
conn
.
GetDatabase
(
0
);
//}
db
.
KeyDelete
(
"lock-exists"
);
db
.
StringSet
(
"lock-exists"
,
"old-value"
,
TimeSpan
.
FromSeconds
(
20
));
var
taken
=
db
.
LockTakeAsync
(
"lock-exists"
,
"new-value"
,
TimeSpan
.
FromSeconds
(
10
));
var
newValue
=
db
.
StringGetAsync
(
"lock-exists"
);
var
ttl
=
db
.
KeyTimeToLiveAsync
(
"lock-exists"
);
Assert
.
IsFalse
(
conn
.
Wait
(
taken
),
"taken"
);
Assert
.
AreEqual
(
"old-value"
,
(
string
)
conn
.
Wait
(
newValue
));
var
ttlValue
=
conn
.
Wait
(
ttl
).
Value
.
TotalSeconds
;
Assert
.
IsTrue
(
ttlValue
>=
18
&&
ttlValue
<=
20
,
"ttl"
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment