ENT-2204: fixed zookeeper client flaky test (#1198)

* ENT-2204: fixed zookeeper client flaky test

* ENT-2204: use size of list as count for the countdown latch in zk test

* ENT-2204: store timeout value into a variable

* ENT-2204: tidy up, use lambda outside of parenthesis
This commit is contained in:
bpaunescu 2018-07-05 12:59:46 +01:00 committed by GitHub
parent 9fa9521ae8
commit 06ca40c168
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 13 deletions

View File

@ -83,13 +83,13 @@ internal class PrioritizedLeaderLatch(client: CuratorFramework,
fun start() { fun start() {
Preconditions.checkState(state.compareAndSet(State.CLOSED, State.STARTED), Preconditions.checkState(state.compareAndSet(State.CLOSED, State.STARTED),
"Cannot be started more than once.") "Cannot be started more than once.")
startTask.set(AfterConnectionEstablished.execute(watchedClient, { startTask.set(AfterConnectionEstablished.execute(watchedClient) {
try { try {
internalStart() internalStart()
} finally { } finally {
startTask.set(null) startTask.set(null)
} }
})) })
} }
/** /**

View File

@ -278,20 +278,25 @@ class ZkClientTests {
@Test @Test
fun `clients randomly do things`() { fun `clients randomly do things`() {
val alice = ZkClient(zkServer.connectString, ZKPaths.makePath(ELECTION_PATH, "test9"), "ALICE", 0) val CLIENTS_NUMBER = 4
val bob = ZkClient(zkServer.connectString, ZKPaths.makePath(ELECTION_PATH, "test9"), "BOB", 1) val ACTIONS_NUMBER = 100
val chip = ZkClient(zkServer.connectString, ZKPaths.makePath(ELECTION_PATH, "test9"), "CHIP", 2) val CLIENT_TIMEOUT = 60L
val dave = ZkClient(zkServer.connectString, ZKPaths.makePath(ELECTION_PATH, "test9"), "DAVE", 3)
val countDownLatch = CountDownLatch(3) val clientList = mutableListOf<ZkClient>()
(1..CLIENTS_NUMBER).forEach {
clientList.add(ZkClient(zkServer.connectString, ZKPaths.makePath(ELECTION_PATH, "test9"), "CLI_${it}", it))
}
val countDownLatch = CountDownLatch(clientList.size)
val leaderBuffer = mutableListOf<String>() val leaderBuffer = mutableListOf<String>()
listOf(alice, bob, chip, dave).forEach { client -> clientList.forEach { client ->
thread{ thread{
client.addLeadershipListener(HelperListener(client.nodeId, leaderBuffer)) client.addLeadershipListener(HelperListener(client.nodeId, leaderBuffer))
client.start() client.start()
val randomizer = Random() val randomizer = Random()
val actions = listOf(Action.RELINQUISH, Action.REQUEST) val actions = listOf(Action.RELINQUISH, Action.REQUEST)
for (i in 1..100) { for (i in 1..ACTIONS_NUMBER) {
val action = actions[randomizer.nextInt(actions.size)] val action = actions[randomizer.nextInt(actions.size)]
when(action) { when(action) {
Action.REQUEST -> client.requestLeadership() Action.REQUEST -> client.requestLeadership()
@ -301,17 +306,16 @@ class ZkClientTests {
Thread.sleep(100) Thread.sleep(100)
} }
countDownLatch.countDown() countDownLatch.countDown()
} }
} }
countDownLatch.await(120, TimeUnit.SECONDS) countDownLatch.await(CLIENT_TIMEOUT, TimeUnit.SECONDS)
//only one leader should exist //only one leader should exist
var leaderCount = 0 var leaderCount = 0
var leaderId = "" var leaderId = ""
listOf(alice, bob, chip, dave).forEach { client -> clientList.forEach { client ->
if (client.isLeader()) { if (client.isLeader()) {
leaderCount++ leaderCount++
leaderId = client.nodeId leaderId = client.nodeId
@ -324,7 +328,8 @@ class ZkClientTests {
println(leaderBuffer) println(leaderBuffer)
assertEquals(leaderBuffer.first(), leaderId) assertEquals(leaderBuffer.first(), leaderId)
} }
listOf(alice, bob, chip, dave).forEach { client -> client.close() }
clientList.forEach { client -> client.close() }
} }
private enum class Action { private enum class Action {