当前位置: 首页>>代码示例>>C#>>正文


C# ConsulClient.CreateLock方法代码示例

本文整理汇总了C#中ConsulClient.CreateLock方法的典型用法代码示例。如果您正苦于以下问题:C# ConsulClient.CreateLock方法的具体用法?C# ConsulClient.CreateLock怎么用?C# ConsulClient.CreateLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ConsulClient的用法示例。


在下文中一共展示了ConsulClient.CreateLock方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Lock_AcquireRelease

        public void Lock_AcquireRelease()
        {
            var client = new ConsulClient();
            const string keyName = "test/lock/acquirerelease";
            var lockKey = client.CreateLock(keyName);

            try
            {
                lockKey.Release();
            }
            catch (LockNotHeldException ex)
            {
                Assert.IsType<LockNotHeldException>(ex);
            }

            lockKey.Acquire(CancellationToken.None);

            try
            {
                lockKey.Acquire(CancellationToken.None);
            }
            catch (LockHeldException ex)
            {
                Assert.IsType<LockHeldException>(ex);
            }

            Assert.True(lockKey.IsHeld);

            lockKey.Release();

            try
            {
                lockKey.Release();
            }
            catch (LockNotHeldException ex)
            {
                Assert.IsType<LockNotHeldException>(ex);
            }

            Assert.False(lockKey.IsHeld);
        }
开发者ID:nbahramzad,项目名称:consuldotnet,代码行数:41,代码来源:LockTest.cs

示例2: Semaphore_Conflict

        public void Semaphore_Conflict()
        {
            var client = new ConsulClient();

            const string keyName = "test/semaphore/conflict";

            var semaphoreLock = client.CreateLock(keyName + "/.lock");

            semaphoreLock.Acquire(CancellationToken.None);

            Assert.True(semaphoreLock.IsHeld);

            var semaphore = client.Semaphore(keyName, 2);

            try
            {
                semaphore.Acquire(CancellationToken.None);
            }
            catch (SemaphoreConflictException ex)
            {
                Assert.IsType<SemaphoreConflictException>(ex);
            }

            try
            {
                semaphore.Destroy();
            }
            catch (SemaphoreConflictException ex)
            {
                Assert.IsType<SemaphoreConflictException>(ex);
            }

            semaphoreLock.Release();

            Assert.False(semaphoreLock.IsHeld);

            semaphoreLock.Destroy();
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:38,代码来源:SemaphoreTest.cs

示例3: Lock_OneShot

        public void Lock_OneShot()
        {
            var client = new ConsulClient();
            const string keyName = "test/lock/oneshot";
            var lockOptions = new LockOptions(keyName)
            {
                LockTryOnce = true
            };

            Assert.Equal(Lock.DefaultLockWaitTime, lockOptions.LockWaitTime);

            lockOptions.LockWaitTime = TimeSpan.FromMilliseconds(250);

            var lockKey = client.CreateLock(lockOptions);

            lockKey.Acquire(CancellationToken.None);

            var contender = client.CreateLock(new LockOptions(keyName)
            {
                LockTryOnce = true,
                LockWaitTime = TimeSpan.FromMilliseconds(250)
            });

            Task.WaitAny(Task.Run(() =>
            {
                Assert.Throws<LockMaxAttemptsReachedException>(() => 
                contender.Acquire()
                );
            }),
            Task.Delay(2 * lockOptions.LockWaitTime.Milliseconds).ContinueWith((t) => Assert.True(false, "Took too long"))
            );

            lockKey.Release();

            contender.Acquire();
            contender.Release();
            contender.Destroy();
        }
开发者ID:nbahramzad,项目名称:consuldotnet,代码行数:38,代码来源:LockTest.cs

示例4: Lock_DeleteKey

        public void Lock_DeleteKey()
        {
            var client = new ConsulClient();

            const string keyName = "test/lock/deletekey";

            var lockKey = (Lock)client.CreateLock(keyName);
            try
            {
                lockKey.Acquire(CancellationToken.None);

                Assert.True(lockKey.IsHeld);

                var checker = Task.Run(() =>
                {
                    while (lockKey.IsHeld)
                    {
                        Thread.Sleep(10);
                    }
                    Assert.False(lockKey.IsHeld);
                });

                Task.WaitAny(new[] { checker }, 1000);

                client.KV.Delete(lockKey.Opts.Key);
            }
            finally
            {
                try
                {
                    lockKey.Release();
                    lockKey.Destroy();
                }
                catch (LockNotHeldException ex)
                {
                    Assert.IsType<LockNotHeldException>(ex);
                }
            }
        }
开发者ID:nbahramzad,项目名称:consuldotnet,代码行数:39,代码来源:LockTest.cs

示例5: Lock_ForceInvalidate

        public void Lock_ForceInvalidate()
        {
            var client = new ConsulClient();

            const string keyName = "test/lock/forceinvalidate";

            var lockKey = (Lock)client.CreateLock(keyName);
            try
            {
                lockKey.Acquire(CancellationToken.None);

                Assert.True(lockKey.IsHeld);

                var checker = Task.Run(() =>
                {
                    while (lockKey.IsHeld)
                    {
                        Task.Delay(10).Wait();
                    }
                    Assert.False(lockKey.IsHeld);
                });

                Task.Run(() => { client.Session.Destroy(lockKey.LockSession); });

                Task.WaitAny(new[] { checker }, 1000);
            }
            finally
            {
                try
                {
                    lockKey.Release();
                    lockKey.Destroy();
                }
                catch (LockNotHeldException ex)
                {
                    Assert.IsType<LockNotHeldException>(ex);
                }
            }
        }
开发者ID:nbahramzad,项目名称:consuldotnet,代码行数:39,代码来源:LockTest.cs

示例6: Lock_SemaphoreConflict

        public void Lock_SemaphoreConflict()
        {
            var client = new ConsulClient();

            const string keyName = "test/lock/semaphoreconflict";

            var semaphore = client.Semaphore(keyName, 2);

            semaphore.Acquire(CancellationToken.None);

            Assert.True(semaphore.IsHeld);

            var lockKey = client.CreateLock(keyName + "/.lock");

            try
            {
                lockKey.Acquire(CancellationToken.None);
            }
            catch (LockConflictException ex)
            {
                Assert.IsType<LockConflictException>(ex);
            }

            try
            {
                lockKey.Destroy();
            }
            catch (LockConflictException ex)
            {
                Assert.IsType<LockConflictException>(ex);
            }

            semaphore.Release();
            semaphore.Destroy();
        }
开发者ID:nbahramzad,项目名称:consuldotnet,代码行数:35,代码来源:LockTest.cs

示例7: Lock_ReclaimLock

        public async Task Lock_ReclaimLock()
        {
            var client = new ConsulClient();

            const string keyName = "test/lock/reclaim";

            var sessionRequest = await client.Session.Create();
            var sessionId = sessionRequest.Response;
            try
            {
                var lock1 = client.CreateLock(new LockOptions(keyName)
                {
                    Session = sessionId
                });

                var lock2 = client.CreateLock(new LockOptions(keyName)
                {
                    Session = sessionId
                });

                try
                {
                    lock1.Acquire(CancellationToken.None);

                    Assert.True(lock1.IsHeld);
                    if (lock1.IsHeld)
                    {
                        Task.WaitAny(new[] { Task.Run(() =>
                    {
                        lock2.Acquire(CancellationToken.None);
                        Assert.True(lock2.IsHeld);
                    }) }, 1000);
                    }
                }
                finally
                {
                    lock1.Release();
                }

                var lockCheck = new[]
            {
                Task.Run(() =>
                {
                    while (lock1.IsHeld)
                    {
                        Thread.Sleep(10);
                    }
                }),
                Task.Run(() =>
                {
                    while (lock2.IsHeld)
                    {
                        Thread.Sleep(10);
                    }
                })
            };

                Task.WaitAll(lockCheck, 1000);

                Assert.False(lock1.IsHeld);
                Assert.False(lock2.IsHeld);
            }
            finally
            {
                Assert.True((await client.Session.Destroy(sessionId)).Response);
            }
        }
开发者ID:nbahramzad,项目名称:consuldotnet,代码行数:67,代码来源:LockTest.cs

示例8: Lock_Destroy

        public void Lock_Destroy()
        {
            var client = new ConsulClient();

            const string keyName = "test/lock/contendlockdelay";

            var lockKey = client.CreateLock(keyName);

            try
            {
                lockKey.Acquire(CancellationToken.None);

                Assert.True(lockKey.IsHeld);

                try
                {
                    lockKey.Destroy();
                    Assert.True(false);
                }
                catch (LockHeldException ex)
                {
                    Assert.IsType<LockHeldException>(ex);
                }

                lockKey.Release();

                Assert.False(lockKey.IsHeld);

                var lockKey2 = client.CreateLock(keyName);

                lockKey2.Acquire(CancellationToken.None);

                Assert.True(lockKey2.IsHeld);

                try
                {
                    lockKey.Destroy();
                    Assert.True(false);
                }
                catch (LockInUseException ex)
                {
                    Assert.IsType<LockInUseException>(ex);
                }

                lockKey2.Release();

                Assert.False(lockKey2.IsHeld);

                lockKey.Destroy();
                lockKey2.Destroy();
            }
            finally
            {
                try
                {
                    lockKey.Release();
                }
                catch (LockNotHeldException ex)
                {
                    Assert.IsType<LockNotHeldException>(ex);
                }
            }
        }
开发者ID:nbahramzad,项目名称:consuldotnet,代码行数:63,代码来源:LockTest.cs

示例9: Lock_Contend_LockDelay

        public void Lock_Contend_LockDelay()
        {
            var client = new ConsulClient();

            const string keyName = "test/lock/contendlockdelay";

            const int contenderPool = 3;

            var acquired = new System.Collections.Concurrent.ConcurrentDictionary<int, bool>();
            using (var cts = new CancellationTokenSource())
            {
                cts.CancelAfter((contenderPool + 1) * (int)Lock.DefaultLockWaitTime.TotalMilliseconds);

                Parallel.For(0, contenderPool, new ParallelOptions { MaxDegreeOfParallelism = contenderPool, CancellationToken = cts.Token }, (v) =>
                {
                    var lockKey = (Lock)client.CreateLock(keyName);
                    lockKey.Acquire(CancellationToken.None);
                    if (lockKey.IsHeld)
                    {
                        Assert.True(acquired.TryAdd(v, lockKey.IsHeld));
                        client.Session.Destroy(lockKey.LockSession);
                    }
                });
            }
            for (var i = 0; i < contenderPool; i++)
            {
                bool didContend = false;
                if (acquired.TryGetValue(i, out didContend))
                {
                    Assert.True(didContend);
                }
                else
                {
                    Assert.True(false, "Contender " + i.ToString() + " did not acquire the lock");
                }
            }
        }
开发者ID:nbahramzad,项目名称:consuldotnet,代码行数:37,代码来源:LockTest.cs

示例10: Lock_ContendFast

        public void Lock_ContendFast()
        {
            var client = new ConsulClient();

            const string keyName = "test/lock/contend";
            const int contenderPool = 10;

            var acquired = new System.Collections.Concurrent.ConcurrentDictionary<int, bool>();
            using (var cts = new CancellationTokenSource())
            {
                cts.CancelAfter(contenderPool * (int)Lock.DefaultLockWaitTime.TotalMilliseconds);

                Parallel.For(0, contenderPool, new ParallelOptions { MaxDegreeOfParallelism = contenderPool, CancellationToken = cts.Token }, (v) =>
                {
                    var lockKey = client.CreateLock(keyName);
                    lockKey.Acquire(CancellationToken.None);
                    Assert.True(acquired.TryAdd(v, lockKey.IsHeld));
                    if (lockKey.IsHeld)
                    {
                        lockKey.Release();
                    }
                });
            }

            for (var i = 0; i < contenderPool; i++)
            {
                if (acquired[i])
                {
                    Assert.True(acquired[i]);
                }
                else
                {
                    Assert.True(false, "Contender " + i.ToString() + " did not acquire the lock");
                }
            }
        }
开发者ID:nbahramzad,项目名称:consuldotnet,代码行数:36,代码来源:LockTest.cs

示例11: Lock_AcquireWaitRelease

        public void Lock_AcquireWaitRelease()
        {
            var client = new ConsulClient();

            const string keyName = "test/lock/acquirewaitrelease";

            var lockOptions = new LockOptions(keyName)
            {
                SessionName = "test_locksession",
                SessionTTL = TimeSpan.FromSeconds(10)
            };

            var l = client.CreateLock(lockOptions);

            l.Acquire(CancellationToken.None);

            Assert.True(l.IsHeld);

            // Wait for multiple renewal cycles to ensure the lock session stays renewed.
            Task.Delay(TimeSpan.FromSeconds(60)).Wait();
            Assert.True(l.IsHeld);

            l.Release();

            Assert.False(l.IsHeld);

            l.Destroy();
        }
开发者ID:nbahramzad,项目名称:consuldotnet,代码行数:28,代码来源:LockTest.cs

示例12: Lock_OneShot

        public void Lock_OneShot()
        {
            var client = new ConsulClient();
            const string keyName = "test/lock/oneshot";
            var lockOptions = new LockOptions(keyName)
            {
                LockTryOnce = true
            };

            Assert.Equal(Lock.DefaultLockWaitTime, lockOptions.LockWaitTime);

            lockOptions.LockWaitTime = TimeSpan.FromMilliseconds(1000);

            var lockKey = client.CreateLock(lockOptions);

            lockKey.Acquire(CancellationToken.None);

            var contender = client.CreateLock(new LockOptions(keyName)
            {
                LockTryOnce = true,
                LockWaitTime = TimeSpan.FromMilliseconds(1000)
            });

            var stopwatch = Stopwatch.StartNew();

            Exception didExcept = null;

            Task.WaitAny(
                Task.Run(() =>
                {
                    // Needed because async asserts don't work in sync methods!
                    try
                    {
                        contender.Acquire();
                    }
                    catch (Exception e)
                    {
                        didExcept = e;
                    }
                }),
                Task.Delay((int)(2 * lockOptions.LockWaitTime.TotalMilliseconds)).ContinueWith((t) => Assert.True(false, "Took too long"))
                );


            Assert.False(stopwatch.ElapsedMilliseconds < lockOptions.LockWaitTime.TotalMilliseconds);
            Assert.False(contender.IsHeld, "Contender should have failed to acquire");
            Assert.IsType<LockMaxAttemptsReachedException>(didExcept);

            lockKey.Release();

            contender.Acquire();
            Assert.True(contender.IsHeld);

            contender.Release();
            contender.Destroy();
        }
开发者ID:geffzhang,项目名称:consuldotnet,代码行数:56,代码来源:LockTest.cs


注:本文中的ConsulClient.CreateLock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。