public void GivenActorAExecutesTheTaskWithOptions()
        {
            var leasableFactory = new LeasableFactory(new AzureLeaseProviderFactory(new ConnectionStringProvider()), new AzureLeasePolicyValidator());
            var leasable = leasableFactory.Create();

            string leaseName;
            TimeSpan leaseDuration;
            IRetryPolicy retryPolicy;
            IRetryStrategy retryStrategy;

            ScenarioContext.Current.TryGetValue(LeaseName, out leaseName);
            ScenarioContext.Current.TryGetValue(LeaseDuration, out leaseDuration);
            ScenarioContext.Current.TryGetValue(RetryPolicy, out retryPolicy);
            ScenarioContext.Current.TryGetValue(RetryStrategy, out retryStrategy);

            leasable.LeasePolicy = new LeasePolicy
            {
                ActorName = "Actor A",
                Duration = leaseDuration,
                Name = leaseName
            };

            leasable.RetryPolicy = retryPolicy;
            leasable.RetryStrategy = retryStrategy;

            var task = leasable.MutexWithOptionsAsync(this.DoSomething);
            SetContinuations(task);
            AddToTasks(task);
        }
        public void GivenActorAExecutesTheTaskWithATryOnceMutex()
        {
            var leasableFactory = new LeasableFactory(new AzureLeaseProviderFactory(new ConnectionStringProvider()), new AzureLeasePolicyValidator());
            var leasable = leasableFactory.Create();
            var leaseName = ScenarioContext.Current.Get<string>(LeaseName);

            var task = leasable.MutexTryOnceAsync(this.DoSomething, leaseName, "Actor A");
            SetContinuations(task);
            AddToTasks(task);
        }
        public void WhenIExecuteTheTaskWithOptions()
        {
            var leasableFactory = new LeasableFactory(new AzureLeaseProviderFactory(new ConnectionStringProvider()), new AzureLeasePolicyValidator());
            var leasable = leasableFactory.Create();

            string leaseName;
            TimeSpan leaseDuration;
            IRetryPolicy retryPolicy;
            IRetryStrategy retryStrategy;

            ScenarioContext.Current.TryGetValue(LeaseName, out leaseName);
            ScenarioContext.Current.TryGetValue(LeaseDuration, out leaseDuration);
            ScenarioContext.Current.TryGetValue(RetryPolicy, out retryPolicy);
            ScenarioContext.Current.TryGetValue(RetryStrategy, out retryStrategy);

            if (leaseName != null || leaseDuration != default(TimeSpan))
            {
                leasable.LeasePolicy = new LeasePolicy
                {
                    ActorName = "Actor A",
                    Duration = leaseDuration,
                    Name = leaseName
                };
            }

            leasable.RetryPolicy = retryPolicy;
            leasable.RetryStrategy = retryStrategy;

            bool result = false;

            try
            {
                result = leasable.MutexWithOptionsAsync(this.DoSomething).Result;
            }
            catch (AggregateException ex)
            {
                ScenarioContext.Current.Add("AggregateException", ex);
            }
            catch (Exception ex)
            {
                ScenarioContext.Current.Add("Exception", ex);
            }

            ScenarioContext.Current.Set(result, Result);
        }
        public void WhenIExecuteTheTaskWithAResult()
        {
            var leasableFactory = new LeasableFactory(new AzureLeaseProviderFactory(new ConnectionStringProvider()), new AzureLeasePolicyValidator());
            var leasable = leasableFactory.Create();
            var leaseName = ScenarioContext.Current.Get<string>(LeaseName);

            var result = new Tuple<bool, bool>(false, false);

            try
            {
                result = leasable.MutexAsync(this.DoSomethingWithResult, leaseName, "Actor A").Result;
            }
            catch (AggregateException ex)
            {
                ScenarioContext.Current.Add("AggregateException", ex);
            }
            catch (Exception ex)
            {
                ScenarioContext.Current.Add("Exception", ex);
            }

            ScenarioContext.Current.Set(result.Item1, Result);
            ScenarioContext.Current.Set(result.Item2, TaskResult);
        }
        public void GivenActorBIsCurrentlyRunningTheTask()
        {
            var leasableFactory = new LeasableFactory(new AzureLeaseProviderFactory(new ConnectionStringProvider()), new AzureLeasePolicyValidator());
            var leasable = leasableFactory.Create();
            var leaseName = ScenarioContext.Current.Get<string>(LeaseName);

            var renewalTask = Task.Factory.StartNew(async () =>
            {
                var task = leasable.MutexAsync(this.DoSomething, leaseName, "Actor B");

                AddToTasks(task);

                await task;
            }, CancellationToken.None);

            renewalTask.ContinueWith(t => { }, TaskScheduler.Current);

            Thread.Sleep(TimeSpan.FromSeconds(1));
        }
        public void WhenIRunActionsUsingLeasable(int numberOfActions)
        {
            var factory = new LeasableFactory(new AzureLeaseProviderFactory(new ConnectionStringProvider()), new AzureLeasePolicyValidator());

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

            for (int i = 0; i < numberOfActions; i++)
            {
                var leasable = factory.Create();
                leasable.MutexAsync(this.DoSomething, string.Format("testlease{0}", i), "TEST").Wait();
            }

            stopwatch.Stop();

            ScenarioContext.Current.Set(stopwatch, "stopwatch");
        }
        public void WhenIRunMutexActionsSimultaneously(int numberOfActions)
        {
            var factory = new LeasableFactory(new AzureLeaseProviderFactory(new ConnectionStringProvider()), new AzureLeasePolicyValidator());

            var duration = ScenarioContext.Current.Get<TimeSpan>("duration");

            var stopwatch = new Stopwatch();

            var tasks = new List<Task>();

            for (int i = 0; i < numberOfActions; i++)
            {
                var leasable = factory.Create();

                leasable.RetryPolicy = new RetryUntilLeaseAcquiredPolicy();
                leasable.RetryStrategy = new Linear(TimeSpan.FromSeconds(duration.TotalSeconds / 2), int.MaxValue);
                leasable.LeasePolicy = new LeasePolicy
                {
                    ActorName = "TEST " + i,
                    Duration = TimeSpan.FromSeconds(15),
                    Name = "leasetest"
                };

                var task = leasable.MutexWithOptionsAsync(this.DoSomething);

                tasks.Add(task);
            }

            stopwatch.Start();
            Task.WaitAll(tasks.ToArray());
            stopwatch.Stop();

            ScenarioContext.Current.Set(stopwatch, "stopwatch");
        }