Search in sources :

Example 1 with WebsocketClientEndpoint

use of com.github.jnidzwetzki.bitfinex.v2.WebsocketClientEndpoint in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class HeartbeatManagerTest method testConnectWhenDisconnected.

@Test(timeout = 30000)
public void testConnectWhenDisconnected() throws Exception {
    // Events
    // * Disconnect websocket from heartbeat
    // * Disconnect from bitfinex API reconnect method
    // * Reconnect on bitfinex api
    final CountDownLatch connectLatch = new CountDownLatch(3);
    // Count down the latch on method call
    final Answer<Void> answer = new Answer<Void>() {

        public Void answer(final InvocationOnMock invocation) throws Throwable {
            connectLatch.countDown();
            return null;
        }
    };
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    final HeartbeatThread heartbeatThreadRunnable = new HeartbeatThread(bitfinexApiBroker);
    final WebsocketClientEndpoint websocketClientEndpoint = Mockito.mock(WebsocketClientEndpoint.class);
    Mockito.when(websocketClientEndpoint.isConnected()).thenReturn(connectLatch.getCount() == 0);
    Mockito.when(bitfinexApiBroker.getWebsocketEndpoint()).thenReturn(websocketClientEndpoint);
    Mockito.doAnswer(answer).when(bitfinexApiBroker).reconnect();
    Mockito.doAnswer(answer).when(websocketClientEndpoint).close();
    final Thread heartbeatThread = new Thread(heartbeatThreadRunnable);
    try {
        heartbeatThread.start();
        connectLatch.await();
    } catch (Exception e) {
        // Should not happen
        throw e;
    } finally {
        heartbeatThread.interrupt();
    }
}
Also used : Answer(org.mockito.stubbing.Answer) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HeartbeatThread(com.github.jnidzwetzki.bitfinex.v2.HeartbeatThread) WebsocketClientEndpoint(com.github.jnidzwetzki.bitfinex.v2.WebsocketClientEndpoint) CountDownLatch(java.util.concurrent.CountDownLatch) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) HeartbeatThread(com.github.jnidzwetzki.bitfinex.v2.HeartbeatThread) Test(org.junit.Test)

Example 2 with WebsocketClientEndpoint

use of com.github.jnidzwetzki.bitfinex.v2.WebsocketClientEndpoint in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class BitfinexApiBroker method connect.

/**
 * Open the connection
 * @throws APIException
 */
public void connect() throws APIException {
    try {
        final URI bitfinexURI = new URI(BITFINEX_URI);
        websocketEndpoint = new WebsocketClientEndpoint(bitfinexURI);
        websocketEndpoint.addConsumer(apiCallback);
        websocketEndpoint.connect();
        updateConnectionHeartbeat();
        executeAuthentification();
        heartbeatThread = new Thread(new HeartbeatThread(this));
        heartbeatThread.start();
    } catch (Exception e) {
        throw new APIException(e);
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) URI(java.net.URI) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) CommandException(com.github.jnidzwetzki.bitfinex.v2.commands.CommandException)

Aggregations

APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)2 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)1 HeartbeatThread (com.github.jnidzwetzki.bitfinex.v2.HeartbeatThread)1 WebsocketClientEndpoint (com.github.jnidzwetzki.bitfinex.v2.WebsocketClientEndpoint)1 CommandException (com.github.jnidzwetzki.bitfinex.v2.commands.CommandException)1 URI (java.net.URI)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1