public void AquireReturnsFalseWhenResponseIsValid()
        {
            var esClient = Substitute.For<IElasticClient>();
            esClient.Update<object>(u => u.Script("")).ReturnsForAnyArgs(call =>
            {
                var response = Substitute.For<IUpdateResponse>();
                response.IsValid.Returns(false);
                return response;

            });

            var dLock = new ElasticSearchDistributedLock("Test", esClient);
            Assert.IsFalse(dLock.Aquire());
        }
        public void ThrowArgumentErrorOnEmptyString()
        {
            var esClient = Substitute.For<IElasticClient>();

            bool error = false;
            try
            {
                var dLock = new ElasticSearchDistributedLock("", esClient);
            }
            catch (Exception)
            {
                error = true;
            }

            Assert.IsTrue(error);
        }
        private async void buttonAquire_Click(object sender, EventArgs e)
        {
            var time = TimeSpan.FromMinutes(1);

            if (!string.IsNullOrEmpty(textBoxLockTimeout.Text))
            {
                TimeSpan.TryParse(textBoxLockTimeout.Text, out time);
            }

            var retryCount = 0;

            if (!string.IsNullOrEmpty(textBoxLockRetry.Text))
            {
                int.TryParse(textBoxLockRetry.Text, out retryCount);
            }

            mLock = new ElasticSearchDistributedLock(textBoxName.Text);

            var result = await mLock.AquireAsync(numberOfRetries: retryCount, Ttlms: (int)time.TotalMilliseconds);

            ShowMessage("Aquire: " + result.ToString());
        }
        private Task<ChaosResult> RunTest(TimeSpan duration, int numberTasks)
        {
            return Task.Factory.StartNew<ChaosResult>(() =>
            {

                var noop = new Action(() => { });
                var success = 0;
                var failure = 0;

                var sw = new Stopwatch();
                sw.Start();

                var client = new Nest.ElasticClient();
                
                while (sw.Elapsed < duration)
                {
                    Parallel.For(1, numberTasks, (a) =>
                    {
                        using (var dLock = new ElasticSearchDistributedLock("lock_" + a.ToString(), client))
                        {
                            if (dLock.Aquire())
                            {
                                noop();
                                success++;
                            }
                            else
                            {
                                failure++;
                            }
                        }
                    });
                }

                sw.Stop();

                var result = new ChaosResult();
                result.Elapsed = sw.Elapsed;
                result.Success = success;
                result.Failure = failure;

                return result;

            });
        }
        public void CanReleaseAfterAquire()
        {
            var esClient = Substitute.For<IElasticClient>();
            esClient.Delete("", "", "").ReturnsForAnyArgs(call =>
            {
                var response = Substitute.For<IDeleteResponse>();
                response.IsValid.Returns(true);
                return response;
            });

            esClient.Update<object>(u => u.Script("")).ReturnsForAnyArgs(call =>
            {
                var response = Substitute.For<IUpdateResponse>();
                response.IsValid.Returns(true);
                return response;

            });

            var dLock = new ElasticSearchDistributedLock("Test", esClient);
            Assert.IsTrue(dLock.Aquire());
            Assert.IsTrue(dLock.Release());
        }
 public void CanReleaseWithoutAquire()
 {
     var esClient = Substitute.For<IElasticClient>();
     var dLock = new ElasticSearchDistributedLock("Test", esClient);
     Assert.IsTrue(dLock.Release());
 }