public void testMultipleLeases() throws Exception
{
ConnectionNotifierMock notifier1 = new ConnectionNotifierMock();
ConnectionNotifierMock notifier2 = new ConnectionNotifierMock();
ConnectionNotifierMock notifier3 = new ConnectionNotifierMock();
Lease lease1 = new Lease("123", 3000, "foo", null, notifier1, null);
Lease lease2 = new Lease("456", 5000, "bar", null, notifier2, null);
Lease lease3 = new Lease("789", 10000, "foobar", null, notifier3, null);
lease1.startLease();
lease2.startLease();
lease3.startLease();
// when first started, the lease window is lease period X 2
System.out.println("waiting 5 seconds before update.");
Thread.currentThread().sleep(5000);
assertFalse(notifier1.notificationFired);
assertFalse(notifier2.notificationFired);
assertFalse(notifier3.notificationFired);
lease1.updateLease(3000);
lease2.updateLease(5000);
lease3.updateLease(10000);
// having to so 5 second sleep again, because lease window
// not activated until after the first update is made
System.out.println("waiting 5 seconds before update.");
Thread.currentThread().sleep(5000);
assertFalse(notifier1.notificationFired);
assertFalse(notifier2.notificationFired);
assertFalse(notifier3.notificationFired);
lease1.updateLease(3000);
lease2.updateLease(5000);
lease3.updateLease(10000);
// now initial delay is gone (since update with the same delay times),
// however, the lease window is bigger (should be 10 seconds) since took so long to update,
// therefore, should be able to wait 8 seconds now without firing
System.out.println("waiting 8 seconds before update.");
Thread.currentThread().sleep(8000);
assertFalse(notifier1.notificationFired);
assertFalse(notifier2.notificationFired);
assertFalse(notifier3.notificationFired);
// will let lease1 timeout, but update lease 2 & 3 to new times
// which should reset the delay window to new value of 6 seconds
lease2.updateLease(3000);
lease3.updateLease(3000);
// will be waiting 2 seconds and then updating lease 2 & 3
// several times as we don't want to change the lease window
// for them, but have to allow lease 1 to timeout (and since it's
// lease window is now 10 and requires two internal timeouts on the
// lease before the notification is fired, means need to wait a total
// of 20 seonds)
System.out.println("waiting 2 seconds before update");
Thread.currentThread().sleep(2000);
// note, should *not* change the lease window
lease2.updateLease(3000);
lease3.updateLease(3000);
System.out.println("waiting 2 seconds before update");
Thread.currentThread().sleep(2000);
// note, should *not* change the lease window
lease2.updateLease(3000);
lease3.updateLease(3000);
System.out.println("waiting 2 seconds before update");
Thread.currentThread().sleep(2000);
// note, should *not* change the lease window
lease2.updateLease(3000);
lease3.updateLease(3000);
System.out.println("waiting 2 seconds before update");
Thread.currentThread().sleep(2000);
// note, should *not* change the lease window
lease2.updateLease(3000);
lease3.updateLease(3000);
System.out.println("waiting 2 seconds before update");
Thread.currentThread().sleep(2000);
// note, should *not* change the lease window
lease2.updateLease(3000);
lease3.updateLease(3000);
System.out.println("waiting 2 seconds before update");
Thread.currentThread().sleep(2000);
// note, should *not* change the lease window
lease2.updateLease(3000);
lease3.updateLease(3000);
System.out.println("waiting 2 seconds before update");
Thread.currentThread().sleep(2000);
// should be total of 22 seconds for lease 1, so should have timeout
assertTrue(notifier1.notificationFired);
assertFalse(notifier2.notificationFired);
assertFalse(notifier3.notificationFired);
// will let lease2 timout now, but update lease 3, but use same time.
lease3.updateLease(3000);
System.out.println("waiting 4 seonds before update");
Thread.currentThread().sleep(4000);
lease3.updateLease(3000);
System.out.println("4 seconds for lease 2 timeout");
Thread.currentThread().sleep(4000);
lease3.updateLease(3000);
System.out.println("4 seconds for lease 2 timeout");
Thread.currentThread().sleep(4000);
assertTrue(notifier2.notificationFired);