use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class IntegrationTest method testCandleStream.
/**
* Test the candle stream
*/
@Test(timeout = 10000)
public void testCandleStream() {
final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
// Await at least 10 callbacks
final CountDownLatch latch = new CountDownLatch(10);
try {
bitfinexClient.connect();
final BitfinexCandlestickSymbol symbol = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1);
final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
final BiConsumer<BitfinexCandlestickSymbol, BitfinexTick> callback = (c, o) -> {
latch.countDown();
};
orderbookManager.registerCandlestickCallback(symbol, callback);
orderbookManager.subscribeCandles(symbol);
latch.await();
orderbookManager.unsubscribeCandles(symbol);
Assert.assertTrue(orderbookManager.removeCandlestickCallback(symbol, callback));
Assert.assertFalse(orderbookManager.removeCandlestickCallback(symbol, callback));
} catch (Exception e) {
// Should not happen
e.printStackTrace();
Assert.assertTrue(false);
} finally {
bitfinexClient.close();
}
}
use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class TickHandlerTest method testTickUpdateAndNotify.
/**
* Test the parsing of one tick
* @throws APIException
* @throws InterruptedException
*/
@Test(timeout = 20000)
public void testTickUpdateAndNotify() throws APIException, InterruptedException {
final String callbackValue = "[26123,41.4645776,26129,33.68138507,2931,0.2231,26129,144327.10936387,26149,13139]";
final JSONArray jsonArray = new JSONArray(callbackValue);
final BitfinexCurrencyPair currencyPair = BitfinexCurrencyPair.BTC_USD;
final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currencyPair);
final ExecutorService executorService = Executors.newFixedThreadPool(10);
final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
final QuoteManager tickerManager = new QuoteManager(bitfinexApiBroker);
Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(tickerManager);
final CountDownLatch latch = new CountDownLatch(1);
tickerManager.registerTickCallback(symbol, (s, c) -> {
try {
Assert.assertEquals(symbol, s);
Assert.assertEquals(26129, c.getOpen().doubleValue(), DELTA);
Assert.assertEquals(26129, c.getClose().doubleValue(), DELTA);
Assert.assertEquals(26129, c.getHigh().doubleValue(), DELTA);
Assert.assertEquals(26129, c.getLow().doubleValue(), DELTA);
Assert.assertEquals(BitfinexTick.INVALID_VOLUME.doubleValue(), c.getVolume().doubleValue(), DELTA);
} catch (Throwable e) {
System.out.println(e);
throw e;
}
latch.countDown();
});
Assert.assertEquals(-1, tickerManager.getHeartbeatForSymbol(symbol));
Assert.assertEquals(null, tickerManager.getLastTick(symbol));
final TickHandler tickHandler = new TickHandler();
final long now = System.currentTimeMillis();
tickHandler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
// Tick callbacks are handled async
latch.await();
Assert.assertTrue(now <= tickerManager.getHeartbeatForSymbol(symbol));
Assert.assertTrue(tickerManager.getLastTick(symbol) != null);
Assert.assertTrue(tickerManager.getLastTick(symbol) != null);
}
use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class HeartbeatThread method checkTickerFreshness.
/**
* Are all ticker uptodate
* @return
*/
private boolean checkTickerFreshness() {
final long currentTime = System.currentTimeMillis();
final QuoteManager tickerManager = bitfinexApiBroker.getQuoteManager();
final Set<BitfinexStreamSymbol> activeSymbols = tickerManager.getActiveSymbols();
for (final BitfinexStreamSymbol symbol : activeSymbols) {
final long lastHeatbeat = tickerManager.getHeartbeatForSymbol(symbol);
if (lastHeatbeat == -1) {
continue;
}
if (lastHeatbeat + TICKER_TIMEOUT < currentTime) {
logger.error("Last update for symbol {} is {} current time is {}, the data is outdated", symbol, lastHeatbeat, currentTime);
return false;
}
}
return true;
}
use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class IntegrationTest method testReconnect.
/**
* Test the session reconnect
* @throws APIException
* @throws InterruptedException
*/
@Test
public void testReconnect() throws APIException, InterruptedException {
final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
bitfinexClient.connect();
final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(BitfinexCurrencyPair.BTC_USD);
final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
orderbookManager.subscribeTicker(symbol);
Thread.sleep(1000);
bitfinexClient.reconnect();
// Await at least 2 callbacks
final CountDownLatch latch = new CountDownLatch(2);
final BiConsumer<BitfinexTickerSymbol, BitfinexTick> callback = (c, o) -> {
latch.countDown();
};
orderbookManager.registerTickCallback(symbol, callback);
latch.await();
Assert.assertTrue(bitfinexClient.isTickerActive(symbol));
orderbookManager.unsubscribeTicker(symbol);
Assert.assertFalse(bitfinexClient.isTickerActive(symbol));
bitfinexClient.close();
}
use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class CandlestickHandlerTest method testCandlestickUpdateAndNotify.
/**
* Test the parsing of one candlestick
* @throws APIException
*/
@Test
public void testCandlestickUpdateAndNotify() throws APIException {
final String callbackValue = "[15134900000,15996,15997,16000,15980,318.5139342]";
final JSONArray jsonArray = new JSONArray(callbackValue);
final BitfinexCandlestickSymbol symbol = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1);
final ExecutorService executorService = Executors.newFixedThreadPool(10);
final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
final QuoteManager tickerManager = new QuoteManager(bitfinexApiBroker);
Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(tickerManager);
final AtomicInteger counter = new AtomicInteger(0);
tickerManager.registerCandlestickCallback(symbol, (s, c) -> {
counter.incrementAndGet();
Assert.assertEquals(symbol, s);
Assert.assertEquals(15996, c.getOpen().doubleValue(), DELTA);
Assert.assertEquals(15997, c.getClose().doubleValue(), DELTA);
Assert.assertEquals(16000, c.getHigh().doubleValue(), DELTA);
Assert.assertEquals(15980, c.getLow().doubleValue(), DELTA);
Assert.assertEquals(318.5139342, c.getVolume().doubleValue(), DELTA);
});
final CandlestickHandler candlestickHandler = new CandlestickHandler();
candlestickHandler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
Assert.assertEquals(1, counter.get());
}
Aggregations