public void LeaderAbortingCreatesNewLeader()
        {
            const int ConcurrentMutexes = 5;
            var settings = new BlobSettings(CloudStorageAccount.DevelopmentStorageAccount, "leases", "LeaderAbortingCreatesNewLeader");

            var firstCts = new CancellationTokenSource();
            var firstMutexAcquired = new TaskCompletionSource<bool>();
            var firstLeader = new BlobDistributedMutex(settings, SignalAndWait(firstMutexAcquired));
            firstLeader.RunTaskWhenMutexAcquired(firstCts.Token);

            Assert.IsTrue(firstMutexAcquired.Task.Wait(TimeSpan.FromSeconds(5)));

            var mutexAcquired = Enumerable.Range(0, ConcurrentMutexes).Select(_ => new TaskCompletionSource<bool>()).ToArray();

            var mutexes = mutexAcquired.Select(completed => new BlobDistributedMutex(settings, SignalAndWait(completed))).ToArray();

            var cts = new CancellationTokenSource();

            foreach (var mutex in mutexes)
            {
                mutex.RunTaskWhenMutexAcquired(cts.Token);
            }

            firstCts.Cancel();

            Task.WaitAny(mutexAcquired.Select(x => (Task)x.Task).ToArray(), TimeSpan.FromSeconds(80));

            cts.Cancel();

            Assert.AreEqual(1, mutexAcquired.Count(x => x.Task.IsCompleted));
        }
Ejemplo n.º 2
0
        public override void Run()
        {
            var settings = new BlobSettings(CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("Storage")), "leases", "MyLeaderCoordinatorTask");

            var mutex = new BlobDistributedMutex(settings, MyLeaderCoordinatorTask);

            mutex.RunTaskWhenMutexAcquired(this.cts.Token).Wait();

            Trace.TraceInformation("Returning from WorkerRole Run and signaling runCompletedEvent");
            this.runCompletedEvent.Set();
        }