use of org.apache.curator.framework.state.ConnectionStateListener in project elastic-job by dangdangdotcom.
the class JobNodeStorageTest method assertAddConnectionStateListener.
@Test
public void assertAddConnectionStateListener() {
CuratorFramework client = mock(CuratorFramework.class);
@SuppressWarnings("unchecked") Listenable<ConnectionStateListener> listeners = mock(Listenable.class);
ConnectionStateListener listener = mock(ConnectionStateListener.class);
when(client.getConnectionStateListenable()).thenReturn(listeners);
when(regCenter.getRawClient()).thenReturn(client);
jobNodeStorage.addConnectionStateListener(listener);
verify(listeners).addListener(listener);
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestFrameworkEdges method testReconnectAfterLoss.
@Test
public void testReconnectAfterLoss() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
final CountDownLatch lostLatch = new CountDownLatch(1);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
lostLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
client.checkExists().forPath("/");
server.stop();
Assert.assertTrue(timing.awaitLatch(lostLatch));
try {
client.checkExists().forPath("/");
Assert.fail();
} catch (KeeperException.ConnectionLossException e) {
// correct
}
server.restart();
client.checkExists().forPath("/");
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestNeverConnected method testNeverConnected.
@Test
public void testNeverConnected() throws Exception {
Timing timing = new Timing();
// use a connection string to a non-existent server
CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:1111", 100, 100, new RetryOneTime(1));
try {
final BlockingQueue<ConnectionState> queue = Queues.newLinkedBlockingQueue();
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState state) {
queue.add(state);
}
};
client.getConnectionStateListenable().addListener(listener);
client.start();
client.create().inBackground().forPath("/");
ConnectionState polled = queue.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
Assert.assertEquals(polled, ConnectionState.SUSPENDED);
polled = queue.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
Assert.assertEquals(polled, ConnectionState.LOST);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestBackgroundStates method testConnectionStateListener.
@Test
public void testConnectionStateListener() throws Exception {
server.close();
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(timing.milliseconds()));
try {
client.start();
final BlockingQueue<ConnectionState> stateVector = Queues.newLinkedBlockingQueue(1);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
stateVector.offer(newState);
}
};
Timing waitingTiming = timing.forWaiting();
client.getConnectionStateListenable().addListener(listener);
server = new TestingServer(server.getPort());
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED);
server.stop();
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
server.restart();
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.RECONNECTED);
server.close();
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestBackgroundStates method testListenersReconnectedIsOK.
@Test
public void testListenersReconnectedIsOK() throws Exception {
server.close();
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
PersistentEphemeralNode node = null;
try {
client.start();
node = new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL, "/abc/node", "hello".getBytes());
node.start();
final CountDownLatch connectedLatch = new CountDownLatch(1);
final CountDownLatch reconnectedLatch = new CountDownLatch(1);
final AtomicReference<ConnectionState> lastState = new AtomicReference<ConnectionState>();
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
lastState.set(newState);
if (newState == ConnectionState.CONNECTED) {
connectedLatch.countDown();
}
if (newState == ConnectionState.RECONNECTED) {
reconnectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
timing.sleepABit();
server = new TestingServer(server.getPort());
Assert.assertTrue(timing.awaitLatch(connectedLatch));
timing.sleepABit();
Assert.assertTrue(node.waitForInitialCreate(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS));
server.restart();
timing.sleepABit();
Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
timing.sleepABit();
Assert.assertEquals(lastState.get(), ConnectionState.RECONNECTED);
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(node);
}
}
Aggregations