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


C# ConsulClient.Semaphore方法代码示例

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


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

示例1: Semaphore_BadLimit

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

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

            try
            {
                client.Semaphore(keyName, 0);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                Assert.IsType<ArgumentOutOfRangeException>(ex);
            }

            var semaphore1 = client.Semaphore(keyName, 1);
            semaphore1.Acquire(CancellationToken.None);

            try
            {
                var semaphore2 = client.Semaphore(keyName, 2);
                semaphore2.Acquire(CancellationToken.None);
            }
            catch (SemaphoreLimitConflictException ex)
            {
                Assert.IsType<SemaphoreLimitConflictException>(ex);
                Assert.Equal(1, ex.RemoteLimit);
                Assert.Equal(2, ex.LocalLimit);
            }

            try
            {
                semaphore1.Release();
                semaphore1.Destroy();
            }
            catch (SemaphoreNotHeldException ex)
            {
                Assert.IsType<SemaphoreNotHeldException>(ex);
            }

            Assert.False(semaphore1.IsHeld);
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:42,代码来源:SemaphoreTest.cs

示例2: Semaphore_AcquireRelease

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

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

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

            try
            {
                semaphore.Release();
            }
            catch (SemaphoreNotHeldException ex)
            {
                Assert.IsType<SemaphoreNotHeldException>(ex);
            }

            semaphore.Acquire(CancellationToken.None);

            Assert.True(semaphore.IsHeld);

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

            Assert.True(semaphore.IsHeld);

            semaphore.Release();

            try
            {
                semaphore.Release();
            }
            catch (SemaphoreNotHeldException ex)
            {
                Assert.IsType<SemaphoreNotHeldException>(ex);
            }

            Assert.False(semaphore.IsHeld);
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:45,代码来源:SemaphoreTest.cs

示例3: 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

示例4: Semaphore_DeleteKey

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

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

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

            try
            {
                semaphore.Acquire(CancellationToken.None);

                Assert.True(semaphore.IsHeld);

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

                    Assert.False(semaphore.IsHeld);
                });

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

                var req = await client.KV.DeleteTree(semaphore.Opts.Prefix);
                Assert.True(req.Response);
            }
            finally
            {
                try
                {
                    semaphore.Release();
                }
                catch (SemaphoreNotHeldException ex)
                {
                    Assert.IsType<SemaphoreNotHeldException>(ex);
                }
            }
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:41,代码来源:SemaphoreTest.cs

示例5: Semaphore_ForceInvalidate

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

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

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

            try
            {
                semaphore.Acquire(CancellationToken.None);

                Assert.True(semaphore.IsHeld);

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

                    Assert.False(semaphore.IsHeld);
                });

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

                client.Session.Destroy(semaphore.LockSession);
            }
            finally
            {
                try
                {
                    semaphore.Release();
                    semaphore.Destroy();
                }
                catch (SemaphoreNotHeldException ex)
                {
                    Assert.IsType<SemaphoreNotHeldException>(ex);
                }
            }
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:41,代码来源:SemaphoreTest.cs

示例6: Semaphore_Destroy

        public void Semaphore_Destroy()
        {
            var c = new ConsulClient();

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

            var semaphore1 = c.Semaphore(keyName, 2);
            var semaphore2 = c.Semaphore(keyName, 2);
            try
            {
                semaphore1.Acquire(CancellationToken.None);
                Assert.True(semaphore1.IsHeld);
                semaphore2.Acquire(CancellationToken.None);
                Assert.True(semaphore2.IsHeld);

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

                semaphore1.Release();
                Assert.False(semaphore1.IsHeld);

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

                semaphore2.Release();
                Assert.False(semaphore2.IsHeld);
                semaphore1.Destroy();
                semaphore2.Destroy();
            }
            finally
            {
                try
                {
                    semaphore1.Release();
                }
                catch (SemaphoreNotHeldException ex)
                {
                    Assert.IsType<SemaphoreNotHeldException>(ex);
                }
                try
                {
                    semaphore2.Release();
                }
                catch (SemaphoreNotHeldException ex)
                {
                    Assert.IsType<SemaphoreNotHeldException>(ex);
                }
            }
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:63,代码来源:SemaphoreTest.cs

示例7: Semaphore_ContendFast

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

            const string keyName = "test/semaphore/contend";
            const int contenderPool = 15;

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

                Parallel.For(0, contenderPool, new ParallelOptions { MaxDegreeOfParallelism = contenderPool, CancellationToken = cts.Token }, (v) =>
                {
                    var semaphore = client.Semaphore(keyName, 2);
                    semaphore.Acquire(CancellationToken.None);
                    acquired[v] = semaphore.IsHeld;
                    semaphore.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:jkabonick-ft,项目名称:consuldotnet,代码行数:33,代码来源:SemaphoreTest.cs

示例8: Semaphore_AcquireWaitRelease

        public void Semaphore_AcquireWaitRelease()
        {

            var client = new ConsulClient();

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

            var semaphoreOptions = new SemaphoreOptions(keyName, 1)
            {
                SessionName = "test_semaphoresession",
                SessionTTL = TimeSpan.FromSeconds(10)
            };

            var semaphore = client.Semaphore(semaphoreOptions);

            semaphore.Acquire(CancellationToken.None);

            Assert.True(semaphore.IsHeld);

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

            semaphore.Release();

            Assert.False(semaphore.IsHeld);

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

示例9: Semaphore_OneShot

        public void Semaphore_OneShot()
        {
            var client = new ConsulClient();
            const string keyName = "test/semaphore/oneshot";
            var semaphoreOptions = new SemaphoreOptions(keyName, 2)
            {
                SemaphoreTryOnce = true
            };

            Assert.Equal(Semaphore.DefaultSemaphoreWaitTime, semaphoreOptions.SemaphoreWaitTime);

            semaphoreOptions.SemaphoreWaitTime = TimeSpan.FromMilliseconds(1000);

            var semaphorekey = client.Semaphore(semaphoreOptions);

            semaphorekey.Acquire(CancellationToken.None);

            var another = client.Semaphore(new SemaphoreOptions(keyName, 2)
            {
                SemaphoreTryOnce = true,
                SemaphoreWaitTime = TimeSpan.FromMilliseconds(1000)
            });

            another.Acquire();

            var contender = client.Semaphore(new SemaphoreOptions(keyName, 2)
            {
                SemaphoreTryOnce = true,
                SemaphoreWaitTime = 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 * semaphoreOptions.SemaphoreWaitTime.TotalMilliseconds)).ContinueWith((t) => Assert.True(false, "Took too long"))
                );

            Assert.False(contender.IsHeld, "Contender should have failed to acquire");
            Assert.False(stopwatch.ElapsedMilliseconds < semaphoreOptions.SemaphoreWaitTime.TotalMilliseconds);
            Assert.IsType<SemaphoreMaxAttemptsReachedException>(didExcept);

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

示例10: 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

示例11: Semaphore_OneShot

        public void Semaphore_OneShot()
        {
            var client = new ConsulClient();
            const string keyName = "test/semaphore/oneshot";
            var semaphoreOptions = new SemaphoreOptions(keyName, 2)
            {
                SemaphoreTryOnce = true
            };

            Assert.Equal(Semaphore.DefaultSemaphoreWaitTime, semaphoreOptions.SemaphoreWaitTime);

            semaphoreOptions.SemaphoreWaitTime = TimeSpan.FromMilliseconds(250);

            var semaphorekey = client.Semaphore(semaphoreOptions);

            semaphorekey.Acquire(CancellationToken.None);

            var another = client.Semaphore(new SemaphoreOptions(keyName, 2)
            {
                SemaphoreTryOnce = true,
                SemaphoreWaitTime = TimeSpan.FromMilliseconds(250)
            });

            another.Acquire();

            var contender = client.Semaphore(new SemaphoreOptions(keyName, 2)
            {
                SemaphoreTryOnce = true,
                SemaphoreWaitTime = TimeSpan.FromMilliseconds(250)
            });

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

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


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