Example #1
0
        public void TestAcquireLock_SameId()
        {
            using (MongoDbPessimisticLocker locker = new MongoDbPessimisticLocker(connectionString))
            {
                string id        = "id-to-lock";
                string keeperId1 = "job1";
                string keeperId2 = "job2";

                Assert.True(locker.AcquireLock(id, keeperId1));
                Assert.False(locker.AcquireLock(id, keeperId2));
            }
        }
Example #2
0
        public void TestAcquireLock_SameId_Timeout_LockReleased_By_MongoDB()
        {
            int ttl = 1;

            using (MongoDbPessimisticLocker locker = new MongoDbPessimisticLocker(connectionString, ttl, true))
            {
                string id        = "id-to-lock";
                string keeperId1 = "job1";
                string keeperId2 = "job2";

                Assert.True(locker.AcquireLock(id, keeperId1));
                Thread.Sleep(TimeSpan.FromSeconds(ttl + 60)); // give mongodb a little bit time to remove lock
                Assert.True(locker.AcquireLock(id, keeperId2));
                Assert.False(locker.AcquireLock(id, keeperId1));
            }
        }
Example #3
0
        public void TestAcquireLock_SameId_Timeout_KeepAllive()
        {
            int ttl = 1;

            using (MongoDbPessimisticLocker locker = new MongoDbPessimisticLocker(connectionString, ttl, true))
            {
                string id        = "id-to-lock";
                string keeperId1 = "job1";
                string keeperId2 = "job2";

                Assert.True(locker.AcquireLock(id, keeperId1));
                for (int i = 0; i < 59; i++)
                {
                    Thread.Sleep(1000);
                    Assert.True(locker.AcquireLock(id, keeperId1));
                }
                Assert.False(locker.AcquireLock(id, keeperId2));
                Assert.True(locker.AcquireLock(id, keeperId1));
            }
        }
Example #4
0
        static void Main(string[] args)
        {
            string lockId           = "task1";
            string connectionString = "mongodb://localhost/LeaderElectionTest";

            using (var locker = new MongoDbPessimisticLocker(connectionString))
            {
                var leaderAwareTaskRunner = new LeaderAwareTaskRunner(locker, lockId);

                var taskRunner = new TaskRunner(500, () =>
                {
                    leaderAwareTaskRunner.RunTaskOnLeader(() =>
                    {
                        Console.WriteLine("task executed on a leader");
                    });
                });

                taskRunner.Start();
                Console.ReadLine();
                taskRunner.Stop();
            }
        }