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());
        }
        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());
        }