コード例 #1
0
ファイル: TradeCacheTest.cs プロジェクト: twistys01/Binance
        public void SubscribeThrows()
        {
            var symbol = Symbol.BTC_USDT;
            var api    = new Mock <IBinanceApi>().Object;
            var client = new Mock <ITradeWebSocketClient>().Object;

            var cache = new TradeCache(api, client);

            Assert.Throws <ArgumentNullException>("symbol", () => cache.Subscribe(null));
            Assert.Throws <ArgumentNullException>("symbol", () => cache.Subscribe(string.Empty));

            cache.Subscribe(symbol);

            Assert.Throws <InvalidOperationException>(() => cache.Subscribe(symbol));
        }
コード例 #2
0
        public void SubscribeTrades(string symbol, int limit, Action <TradeEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken)
        {
            try
            {
                var tradeCache = new TradeCache(binanceApi, new TradeWebSocketClient());
                tradeCache.Subscribe(symbol, limit, e =>
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        tradeCache.Unsubscribe();
                        return;
                    }

                    try
                    {
                        var trades = e.Trades.Select(t => NewTrade(t)).ToList();
                        callback.Invoke(new TradeEventArgs {
                            Trades = trades
                        });
                    }
                    catch (Exception ex)
                    {
                        tradeCache.Unsubscribe();
                        exception.Invoke(ex);
                    }
                });
            }
            catch (Exception ex)
            {
                exception.Invoke(ex);
            }
        }
コード例 #3
0
        public Task SubscribeTrades(string symbol, int limit, Action <TradeEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken)
        {
            var tcs = new TaskCompletionSource <object>();

            var binanceApi = new BinanceApi();
            var tradeCache = new TradeCache(binanceApi, new TradeWebSocketClient());

            tradeCache.Subscribe(symbol, limit, e =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    tradeCache.Unsubscribe();
                    return;
                }

                var trades = e.Trades.Select(t => NewTrade(t)).ToList();

                try
                {
                    callback.Invoke(new TradeEventArgs {
                        Trades = trades
                    });
                }
                catch (Exception ex)
                {
                    tradeCache.Unsubscribe();
                    exception.Invoke(ex);
                    return;
                }
            });

            tcs.SetResult(null);

            return(tcs.Task);
        }
コード例 #4
0
ファイル: TradeCacheTest.cs プロジェクト: mirror222/Binance
        public void SubscribeThrows()
        {
            var api    = new Mock <IBinanceApi>().Object;
            var client = new Mock <ITradeWebSocketClient>().Object;

            var cache = new TradeCache(api, client);

            Assert.Throws <ArgumentNullException>("symbol", () => cache.Subscribe(null));
        }
コード例 #5
0
        public void LinkToClient()
        {
            var api     = new Mock <IBinanceApi>().Object;
            var client1 = new Mock <ITradeClient>().Object;
            var client2 = new TradeClient();
            var symbol1 = Symbol.BTC_USDT;
            var symbol2 = Symbol.LTC_BTC;

            client2.Subscribe(symbol1);

            var clientSubscribeStreams = client2.SubscribedStreams.ToArray();

            Assert.Equal(TradeClient.GetStreamName(symbol1), clientSubscribeStreams.Single());

            var cache = new TradeCache(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(TradeClient.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(TradeClient.GetStreamName(symbol2), client2.SubscribedStreams);
        }