This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +38
-3
lines changed
tests/ServiceStack.Redis.Tests/Examples Expand file tree Collapse file tree 1 file changed +38
-3
lines changed Original file line number Diff line number Diff line change 11using System ;
22using System . Collections . Generic ;
33using System . Diagnostics ;
4- using System . Linq ;
5- using System . Text ;
64using System . Threading ;
5+ using System . Threading . Tasks ;
76using NUnit . Framework ;
8- using ServiceStack . Common ;
97
108namespace 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
You can’t perform that action at this time.
0 commit comments