public async Task Semaphore_AcquireWaitRelease()
        {
            const string keyName = "test/semaphore/acquirewaitrelease";

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

            var semaphore = _client.Semaphore(semaphoreOptions);

            await semaphore.Acquire(CancellationToken.None);

            Assert.True(semaphore.IsHeld);

            // Wait for multiple renewal cycles to ensure the semaphore session stays renewed.
            for (int i = 0; i < 60; i++)
            {
                await Task.Delay(1000);

                Assert.True(semaphore.IsHeld);
            }

            Assert.True(semaphore.IsHeld);

            await semaphore.Release();

            Assert.False(semaphore.IsHeld);

            await semaphore.Destroy();
        }
Exemple #2
0
        public void Semaphore_AcquireWaitRelease()
        {
            var client = new Client();

            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.IsTrue(semaphore.IsHeld);

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

            semaphore.Release();

            Assert.IsFalse(semaphore.IsHeld);

            semaphore.Destroy();
        }
Exemple #3
0
        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();
        }
Exemple #4
0
        public async Task 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);

            await semaphorekey.Acquire(CancellationToken.None);

            Assert.True(semaphorekey.IsHeld);

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

            await another.Acquire();

            Assert.True(another.IsHeld);
            Assert.True(semaphorekey.IsHeld);

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


            var stopwatch = Stopwatch.StartNew();

            Task.WaitAny(
                Task.Run(async() => { await Assert.ThrowsAsync <SemaphoreMaxAttemptsReachedException>(async() => await contender.Acquire()); }),
                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.False(contender.IsHeld);
            Assert.True(another.IsHeld);
            Assert.True(semaphorekey.IsHeld);
            await semaphorekey.Release();

            await another.Release();

            await contender.Destroy();
        }
        public async Task Semaphore_OneShot()
        {
            const string keyName  = "test/semaphore/oneshot";
            TimeSpan     waitTime = TimeSpan.FromMilliseconds(3000);

            var semaphoreOptions = new SemaphoreOptions(keyName, 2)
            {
                SemaphoreTryOnce = true
            };

            semaphoreOptions.SemaphoreWaitTime = waitTime;

            var semaphoreKey = _client.Semaphore(semaphoreOptions);

            await semaphoreKey.Acquire(CancellationToken.None);

            Assert.True(semaphoreKey.IsHeld);

            var another = _client.Semaphore(new SemaphoreOptions(keyName, 2)
            {
                SemaphoreTryOnce  = true,
                SemaphoreWaitTime = waitTime
            });

            await another.Acquire();

            Assert.True(another.IsHeld);
            Assert.True(semaphoreKey.IsHeld);

            var contender = _client.Semaphore(new SemaphoreOptions(keyName, 2)
            {
                SemaphoreTryOnce  = true,
                SemaphoreWaitTime = waitTime
            });

            var stopwatch = Stopwatch.StartNew();

            await TimeoutUtils.WithTimeout(
                Assert.ThrowsAsync <SemaphoreMaxAttemptsReachedException>(async() => await contender.Acquire()));

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

            Assert.False(contender.IsHeld);
            Assert.True(another.IsHeld);
            Assert.True(semaphoreKey.IsHeld);
            await semaphoreKey.Release();

            await another.Release();

            await contender.Destroy();
        }
Exemple #6
0
        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 <SemaphoreMaxAttemptsReachedException>(() =>
                                                                     contender.Acquire()
                                                                     );
            }),
                         Task.Delay(2 * semaphoreOptions.SemaphoreWaitTime.Milliseconds).ContinueWith((t) => Assert.True(false, "Took too long"))
                         );

            semaphorekey.Release();
            another.Release();
            contender.Destroy();
        }
        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();
        }
        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();
        }
        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();
        }
 public Task ExecuteInSemaphore(SemaphoreOptions opts, Action a, CancellationToken ct = default)
 {
     throw new NotImplementedException();
 }
 public Task <IDistributedSemaphore> AcquireSemaphore(SemaphoreOptions opts, CancellationToken ct = default)
 {
     throw new NotImplementedException();
 }
 public IDistributedSemaphore Semaphore(SemaphoreOptions opts)
 {
     throw new NotImplementedException();
 }
        public void Semaphore_AcquireWaitRelease()
        {
            var semaphoreOptions = new SemaphoreOptions("test/semaphore", 1)
            {
                SessionName = "test_semaphoresession",
                SessionTTL = TimeSpan.FromSeconds(10)
            };
            var c = ClientTest.MakeClient();

            var s = c.Semaphore(semaphoreOptions);

            s.Acquire(CancellationToken.None);

            Assert.IsTrue(s.IsHeld);

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

            s.Release();

            Assert.IsFalse(s.IsHeld);

            s.Destroy();
        }