public async Task TestLeadershipIsBlocking()
        {
            using (var manager = GetConfig("Service1"))
                using (var manager2 = GetConfig("Service2"))
                {
                    await manager.AddTtlHealthCheck(1000).RegisterServiceAsync();

                    await manager2.AddTtlHealthCheck(1000).RegisterServiceAsync();

                    await manager.TtlCheck.ReportPassingAsync();

                    await manager2.TtlCheck.ReportPassingAsync();

                    var leader1 = new LeaderRegistry(manager);
                    var leader2 = new LeaderRegistry(manager2);

                    var watcher = await leader1.GetLeaderWatcherAsync(leadershipKey);

                    var watcher2 = await leader2.GetLeaderWatcherAsync(leadershipKey);

                    var counter = 0;

                    watcher.SetLeaderCallback(info => Interlocked.Increment(ref counter));
                    watcher2.SetLeaderCallback(info => Interlocked.Increment(ref counter));

                    var ignore  = watcher.GetLeadershipAsync();
                    var ignore2 = watcher.GetLeadershipAsync();

                    await Task.Delay(1000);

                    Assert.InRange(counter, 0, 5);
                }
        }
        public async Task TestGetLeadership()
        {
            using (var manager = GetConfig("Service1"))
            {
                manager.AddTtlHealthCheck(10);
                var registerResult = await manager.RegisterServiceAsync();

                var ttlResult = await manager.TtlCheck.ReportPassingAsync();

                var leaderRegistry = new LeaderRegistry(manager);
                var watcher        = await leaderRegistry.GetLeaderWatcherAsync(leadershipKey);

                await watcher.GetLeadershipAsync();

                var result = await watcher.GetCurrentLeaderAsync();

                Assert.Equal(manager.ServiceId, result.ID);
            }
        }
 public async Task FailToGetLeadershipWhenNotRegistered()
 {
     using (var manager = GetConfig("Service1"))
     {
         var leadership = new LeaderRegistry(manager);
         await Assert.ThrowsAsync <InvalidOperationException>(async() => await leadership.GetLeaderWatcherAsync("TestKey"));
     }
 }