Commit 45d7ba62 authored by Marc Gravell's avatar Marc Gravell

Migrated: BookSleeve locking tests

parent 6218102c
//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())
{
string right = Guid.NewGuid().ToString(),
wrong = Guid.NewGuid().ToString();
const int DB = 7;
const string Key = "lock-key";
//conn.SuspendFlush();
var db = conn.GetDatabase(DB);
db.KeyDelete(Key);
var t1 = db.LockTakeAsync(Key, right, TimeSpan.FromSeconds(20));
var t1b = db.LockTakeAsync(Key, wrong, TimeSpan.FromSeconds(10));
var t2 = db.StringGetAsync(Key);
var t3 = db.LockReleaseAsync(Key, wrong);
var t4 = db.StringGetAsync(Key);
var t5 = db.LockExtendAsync(Key, wrong, TimeSpan.FromSeconds(60));
var t6 = db.StringGetAsync(Key);
var t7 = db.KeyTimeToLiveAsync(Key);
var t8 = db.LockExtendAsync(Key, right, TimeSpan.FromSeconds(60));
var t9 = db.StringGetAsync(Key);
var t10 = db.KeyTimeToLiveAsync(Key);
var t11 = db.LockReleaseAsync(Key, right);
var t12 = db.StringGetAsync(Key);
var t13 = db.LockTakeAsync(Key, wrong, TimeSpan.FromSeconds(10));
// conn.Keys.Remove(DB, Key); Assert.IsNotNull(right);
// var t1 = conn.Strings.TakeLock(DB, Key, right, 20); Assert.IsNotNull(wrong);
// var t1b = conn.Strings.TakeLock(DB, Key, wrong, 10); Assert.AreNotEqual(right, (string)wrong);
// var t2 = conn.Strings.GetString(DB, Key); Assert.IsTrue(conn.Wait(t1), "1");
// var t3 = conn.Strings.ReleaseLock(DB, Key, wrong); Assert.IsFalse(conn.Wait(t1b), "1b");
// var t4 = conn.Strings.GetString(DB, Key); Assert.AreEqual(right, (string)conn.Wait(t2), "2");
// var t5 = conn.Strings.ExtendLock(DB, Key, wrong, 60); Assert.IsFalse(conn.Wait(t3), "3");
// var t6 = conn.Strings.GetString(DB, Key); Assert.AreEqual(right, (string)conn.Wait(t4), "4");
// var t7 = conn.Keys.TimeToLive(DB, Key); Assert.IsFalse(conn.Wait(t5), "5");
// var t8 = conn.Strings.ExtendLock(DB, Key, right, 60); Assert.AreEqual(right, (string)conn.Wait(t6), "6");
// var t9 = conn.Strings.GetString(DB, Key); var ttl = conn.Wait(t7).Value.TotalSeconds;
// var t10 = conn.Keys.TimeToLive(DB, Key); Assert.IsTrue(ttl > 0 && ttl <= 20, "7");
// var t11 = conn.Strings.ReleaseLock(DB, Key, right); Assert.IsTrue(conn.Wait(t8), "8");
// var t12 = conn.Strings.GetString(DB, Key); Assert.AreEqual(right, (string)conn.Wait(t9), "9");
// var t13 = conn.Strings.TakeLock(DB, Key, wrong, 10); ttl = conn.Wait(t10).Value.TotalSeconds;
// conn.ResumeFlush(); Assert.IsTrue(ttl > 50 && ttl <= 60, "10");
// Assert.IsNotNull(right); Assert.IsTrue(conn.Wait(t11), "11");
// Assert.IsNotNull(wrong); Assert.IsNull((string)conn.Wait(t12), "12");
// Assert.AreNotEqual(right, wrong); Assert.IsTrue(conn.Wait(t13), "13");
// Assert.IsTrue(conn.Wait(t1), "1"); }
// Assert.IsFalse(conn.Wait(t1b), "1b"); }
// Assert.AreEqual(right, conn.Wait(t2), "2");
// Assert.IsFalse(conn.Wait(t3), "3");
// Assert.AreEqual(right, conn.Wait(t4), "4"); ////public void TestManualLockOpCountByVersion(RedisConnection conn, int expected, bool existFirst)
// Assert.IsFalse(conn.Wait(t5), "5"); ////{
// Assert.AreEqual(right, conn.Wait(t6), "6"); //// const int DB = 0, LockDuration = 30;
// var ttl = conn.Wait(t7); //// const string Key = "TestManualLockOpCountByVersion";
// Assert.IsTrue(ttl > 0 && ttl <= 20, "7"); //// conn.Wait(conn.Open());
// Assert.IsTrue(conn.Wait(t8), "8"); //// conn.Keys.Remove(DB, Key);
// Assert.AreEqual(right, conn.Wait(t9), "9"); //// var newVal = "us:" + Config.CreateUniqueName();
// ttl = conn.Wait(t10); //// string expectedVal = newVal;
// Assert.IsTrue(ttl > 50 && ttl <= 60, "10"); //// if (existFirst)
// Assert.IsTrue(conn.Wait(t11), "11"); //// {
// Assert.IsNull(conn.Wait(t12), "12"); //// expectedVal = "other:" + Config.CreateUniqueName();
// Assert.IsTrue(conn.Wait(t13), "13"); //// conn.Strings.Set(DB, Key, expectedVal, LockDuration);
// } //// }
// } //// int countBefore = conn.GetCounters().MessagesSent;
//// var tran = conn.CreateTransaction();
// //public void TestManualLockOpCountByVersion(RedisConnection conn, int expected, bool existFirst) //// tran.AddCondition(Condition.KeyNotExists(DB, Key));
// //{ //// tran.Strings.Set(DB, Key, newVal, LockDuration);
// // const int DB = 0, LockDuration = 30; //// var taken = conn.Wait(tran.Execute());
// // const string Key = "TestManualLockOpCountByVersion";
// // conn.Wait(conn.Open()); //// int countAfter = conn.GetCounters().MessagesSent;
// // conn.Keys.Remove(DB, Key); //// var valAfter = conn.Wait(conn.Strings.GetString(DB, Key));
// // var newVal = "us:" + Config.CreateUniqueName(); //// Assert.AreEqual(!existFirst, taken, "lock taken (manual)");
// // string expectedVal = newVal; //// Assert.AreEqual(expectedVal, valAfter, "taker (manual)");
// // if (existFirst) //// Assert.AreEqual(expected, (countAfter - countBefore) - 1, "expected ops (including ping) (manual)");
// // { //// // note we get a ping from GetCounters
// // expectedVal = "other:" + Config.CreateUniqueName(); ////}
// // conn.Strings.Set(DB, Key, expectedVal, LockDuration);
// // }
// // int countBefore = conn.GetCounters().MessagesSent;
[Test]
// // var tran = conn.CreateTransaction(); public void TestBasicLockNotTaken()
// // tran.AddCondition(Condition.KeyNotExists(DB, Key)); {
// // tran.Strings.Set(DB, Key, newVal, LockDuration); using (var conn = Config.GetUnsecuredConnection())
// // var taken = conn.Wait(tran.Execute()); {
int errorCount = 0;
// // int countAfter = conn.GetCounters().MessagesSent;
// // var valAfter = conn.Wait(conn.Strings.GetString(DB, Key)); conn.InternalError += delegate { Interlocked.Increment(ref errorCount); };
// // Assert.AreEqual(!existFirst, taken, "lock taken (manual)");
// // Assert.AreEqual(expectedVal, valAfter, "taker (manual)"); var db = conn.GetDatabase(0);
// // Assert.AreEqual(expected, (countAfter - countBefore) - 1, "expected ops (including ping) (manual)"); Task<bool> taken = null;
// // // note we get a ping from GetCounters Task<RedisValue> newValue = null;
// //} Task<TimeSpan?> ttl = null;
const int LOOP = 50;
for (int i = 0; i < LOOP; i++)
// [Test] {
// public void TestBasicLockNotTaken() db.KeyDeleteAsync("lock-not-exists");
// { taken = db.LockTakeAsync("lock-not-exists", "new-value", TimeSpan.FromSeconds(10));
// using(var conn = Config.GetUnsecuredConnection()) newValue = db.StringGetAsync("lock-not-exists");
// { ttl = db.KeyTimeToLiveAsync("lock-not-exists");
// int errorCount = 0; }
// conn.Error += delegate { Interlocked.Increment(ref errorCount); }; Assert.IsTrue(conn.Wait(taken), "taken");
// Task<bool> taken = null; Assert.AreEqual("new-value", (string)conn.Wait(newValue));
// Task<string> newValue = null; var ttlValue = conn.Wait(ttl).Value.TotalSeconds;
// Task<long> ttl = null; Assert.IsTrue(ttlValue >= 8 && ttlValue <= 10, "ttl");
// const int LOOP = 50; Assert.AreEqual(0, errorCount);
// for (int i = 0; i < LOOP; i++) }
// { }
// conn.Keys.Remove(0, "lock-not-exists");
// taken = conn.Strings.TakeLock(0, "lock-not-exists", "new-value", 10); [Test]
// newValue = conn.Strings.GetString(0, "lock-not-exists"); public void TestBasicLockTaken()
// ttl = conn.Keys.TimeToLive(0, "lock-not-exists"); {
// } using (var conn = Config.GetUnsecuredConnection())
// Assert.IsTrue(conn.Wait(taken), "taken"); {
// Assert.AreEqual("new-value", conn.Wait(newValue)); var db = conn.GetDatabase(0);
// var ttlValue = conn.Wait(ttl); db.KeyDelete("lock-exists");
// Assert.IsTrue(ttlValue >= 8 && ttlValue <= 10, "ttl"); db.StringSet("lock-exists", "old-value", TimeSpan.FromSeconds(20));
var taken = db.LockTakeAsync("lock-exists", "new-value", TimeSpan.FromSeconds(10));
// Assert.AreEqual(0, errorCount); var newValue = db.StringGetAsync("lock-exists");
// } var ttl = db.KeyTimeToLiveAsync("lock-exists");
// }
Assert.IsFalse(conn.Wait(taken), "taken");
// [Test] Assert.AreEqual("old-value", (string)conn.Wait(newValue));
// public void TestBasicLockTaken() var ttlValue = conn.Wait(ttl).Value.TotalSeconds;
// { Assert.IsTrue(ttlValue >= 18 && ttlValue <= 20, "ttl");
// using (var conn = Config.GetUnsecuredConnection()) }
// { }
// conn.Keys.Remove(0, "lock-exists"); }
// conn.Strings.Set(0, "lock-exists", "old-value", expirySeconds: 20); }
// var taken = conn.Strings.TakeLock(0, "lock-exists", "new-value", 10);
// var newValue = conn.Strings.GetString(0, "lock-exists");
// var ttl = conn.Keys.TimeToLive(0, "lock-exists");
// Assert.IsFalse(conn.Wait(taken), "taken");
// Assert.AreEqual("old-value", conn.Wait(newValue));
// var ttlValue = conn.Wait(ttl);
// Assert.IsTrue(ttlValue >= 18 && ttlValue <= 20, "ttl");
// }
// }
// }
//}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment