public void SubscribeThrows() { var api = new Mock <IBinanceApi>().Object; var client = new Mock <IDepthWebSocketClient>().Object; var cache = new OrderBookCache(api, client); Assert.Throws <ArgumentNullException>("symbol", () => cache.Subscribe(null)); }
public Task SubscribeOrderBook(string symbol, int limit, Action <OrderBookEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken) { var tcs = new TaskCompletionSource <object>(); try { var orderBookCache = new OrderBookCache(binanceApi, new DepthWebSocketClient()); orderBookCache.Subscribe(symbol, limit, e => { if (cancellationToken.IsCancellationRequested) { orderBookCache.Unsubscribe(); return; } try { var orderBook = NewOrderBook(e.OrderBook); callback.Invoke(new OrderBookEventArgs { OrderBook = orderBook }); } catch (Exception ex) { orderBookCache.Unsubscribe(); exception.Invoke(ex); return; } }); tcs.SetResult(null); } catch (Exception ex) { tcs.SetException(ex); } return(tcs.Task); }
public void LinkToClient() { var api = new Mock <IBinanceApi>().Object; var client1 = new Mock <IDepthClient>().Object; var client2 = new DepthClient(); var symbol1 = Symbol.BTC_USDT; var symbol2 = Symbol.LTC_BTC; client2.Subscribe(symbol1); var clientSubscribeStreams = client2.SubscribedStreams.ToArray(); Assert.Equal(DepthClient.GetStreamName(symbol1), clientSubscribeStreams.Single()); var cache = new OrderBookCache(api, client1) { Client = client2 // link client. }; // Client subscribed streams are unchanged after link to unsubscribed cache. Assert.Equal(clientSubscribeStreams, client2.SubscribedStreams); cache.Client = client1; // unlink client. // Subscribe cache to symbol. cache.Subscribe(symbol2); // Cache is subscribed to symbol. Assert.Equal(DepthClient.GetStreamName(symbol2), cache.SubscribedStreams.Single()); cache.Client = client2; // link to client. // Client has second subscribed stream from cache. Assert.True(client2.SubscribedStreams.Count() == 2); Assert.Contains(DepthClient.GetStreamName(symbol2), client2.SubscribedStreams); }