-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
142 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
...s/src/main/java/io/eventuate/coordination/leadership/tests/LeaderSelectorTestingWrap.java
This file was deleted.
Oops, something went wrong.
89 changes: 89 additions & 0 deletions
89
...hip-tests/src/main/java/io/eventuate/coordination/leadership/tests/SelectorUnderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.eventuate.coordination.leadership.tests; | ||
|
||
import io.eventuate.coordination.leadership.EventuateLeaderSelector; | ||
import io.eventuate.coordination.leadership.LeaderSelectedCallback; | ||
import io.eventuate.coordination.leadership.LeadershipController; | ||
import io.eventuate.util.test.async.Eventually; | ||
import org.junit.Assert; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.function.BiFunction; | ||
|
||
public class SelectorUnderTest<SELECTOR extends EventuateLeaderSelector> { | ||
private SELECTOR selector; | ||
private final AtomicInteger invocationCounter = new AtomicInteger(0);; | ||
private final AtomicBoolean leaderFlag = new AtomicBoolean(false); | ||
private LeadershipController leadershipController; | ||
|
||
private CountDownLatch sleepLatch; | ||
|
||
public SelectorUnderTest(BiFunction<LeaderSelectedCallback , Runnable, SELECTOR> factory) { | ||
this.selector = factory.apply(this::leadershipSelectedCallback, this::leadershipRemovedCallback); | ||
} | ||
|
||
private void leadershipRemovedCallback() { | ||
leaderFlag.set(false); | ||
} | ||
|
||
private void leadershipSelectedCallback(LeadershipController leadershipController) { | ||
this.leadershipController = leadershipController; | ||
leaderFlag.set(true); | ||
invocationCounter.incrementAndGet(); | ||
sleepLatch= new CountDownLatch(1); | ||
try { | ||
sleepLatch.await(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void eventuallyAssertIsLeaderAndCallbackIsInvokedOnce() { | ||
Eventually.eventually(() -> { | ||
Assert.assertTrue("should be leader", isLeader()); | ||
Assert.assertEquals(1, getInvocationCount()); | ||
}); | ||
} | ||
|
||
public void eventuallyAssertIsNotLeaderAndCallbackIsInvokedOnce() { | ||
Eventually.eventually(() -> { | ||
Assert.assertFalse("should not be leader", isLeader()); | ||
Assert.assertEquals(1, getInvocationCount()); | ||
}); | ||
} | ||
|
||
public int getInvocationCount() { | ||
return invocationCounter.get(); | ||
} | ||
|
||
public boolean isLeader() { | ||
return leaderFlag.get(); | ||
} | ||
|
||
public SELECTOR getSelector() { | ||
return selector; | ||
} | ||
|
||
public void start() { | ||
selector.start(); | ||
} | ||
|
||
public void stop() { | ||
selector.stop(); | ||
} | ||
|
||
public void relinquish() { | ||
Assert.assertTrue("should be leader", isLeader()); | ||
sleepLatch.countDown(); | ||
leadershipController.stop(); | ||
} | ||
|
||
public void eventuallyAssertIsLeaderAndCallbackIsInvokedTwice() { | ||
Eventually.eventually(() -> { | ||
Assert.assertTrue("should be leader", isLeader()); | ||
Assert.assertEquals(2, getInvocationCount()); | ||
}); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
eventuate-common-coordination-leadership-zookeeper/src/test/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
eventuatelocal.zookeeper.connection.string=localhost:2181 | ||
logging.level.io.eventuate=INFO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters