Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 530588a

Browse files
committed
Add new AcquireLock example test
1 parent 39f3bcf commit 530588a

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

tests/ServiceStack.Redis.Tests/Examples/SimpleLocks.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Text;
64
using System.Threading;
5+
using System.Threading.Tasks;
76
using NUnit.Framework;
8-
using ServiceStack.Common;
97

108
namespace ServiceStack.Redis.Tests.Examples
119
{
@@ -111,6 +109,43 @@ public void SimulateLockTimeout()
111109
}
112110
}
113111

112+
[Test]
113+
public void AcquireLock_using_Tasks()
114+
{
115+
const int noOfClients = 4;
116+
var tasks = new Task[noOfClients];
117+
for (var i = 0; i < noOfClients; i++)
118+
{
119+
Thread.Sleep(2000);
120+
tasks[i] = Task.Factory.StartNew((object clientNo) =>
121+
{
122+
try
123+
{
124+
Console.WriteLine("About to process " + clientNo);
125+
//var redisClient = new RedisClient("xxxx.redis.cache.windows.net", 6379, "xxxx");
126+
var redisClient = new RedisClient("localhost", 6379);
127+
128+
using (redisClient.AcquireLock("testlock1", TimeSpan.FromMinutes(3)))
129+
{
130+
Console.WriteLine("client {0} acquired lock", (int)clientNo);
131+
var counter = redisClient.Get<int>("atomic-counter");
132+
133+
//Add an artificial delay to demonstrate locking behaviour
134+
Thread.Sleep(100);
135+
136+
redisClient.Set("atomic-counter", counter + 1);
137+
Console.WriteLine("client {0} released lock", (int)clientNo);
138+
}
139+
}
140+
catch (Exception e)
141+
{
142+
Console.WriteLine(e.Message);
143+
}
144+
145+
}, i + 1);
146+
}
147+
}
148+
114149
}
115150

116151

0 commit comments

Comments
 (0)