Skip to content

Commit

Permalink
add test for trades stream (#726)
Browse files Browse the repository at this point in the history
- also fix GlobalSymbolTest() by adding more exchange exceptions
  • Loading branch information
vslee authored Feb 2, 2022
1 parent 72c5c61 commit 1e56691
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions tests/ExchangeSharpTests/ExchangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The above copyright notice and this permission notice shall be included in all c
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using ExchangeSharp;
Expand Down Expand Up @@ -100,7 +101,9 @@ public async Task GlobalSymbolTest()
if (api is ExchangeUfoDexAPI || api is ExchangeOKExAPI || api is ExchangeHitBTCAPI || api is ExchangeKuCoinAPI ||
api is ExchangeOKCoinAPI || api is ExchangeDigifinexAPI || api is ExchangeNDAXAPI || api is ExchangeBL3PAPI ||
api is ExchangeBinanceUSAPI || api is ExchangeBinanceJerseyAPI || api is ExchangeBinanceDEXAPI ||
api is ExchangeBitMEXAPI || api is ExchangeBTSEAPI || api is ExchangeBybitAPI)
api is ExchangeBitMEXAPI || api is ExchangeBTSEAPI || api is ExchangeBybitAPI ||
api is ExchangeAquanowAPI || api is ExchangeBitfinexAPI || api is ExchangeBittrexAPI ||
api is ExchangeFTXAPI || api is ExchangeFTXUSAPI || api is ExchangeGateIoAPI || api is ExchangeCoinmateAPI)
{
// WIP
continue;
Expand Down Expand Up @@ -146,5 +149,52 @@ api is ExchangeBinanceUSAPI || api is ExchangeBinanceJerseyAPI || api is Exchang
}
}
}
}

[TestMethod]
public async Task TradesWebsocketTest()
{
foreach (IExchangeAPI api in await ExchangeAPI.GetExchangeAPIsAsync())
{

if (api is ExchangeBinanceDEXAPI // volume too low
|| api is ExchangeBinanceJerseyAPI // ceased operations
|| api is ExchangeBittrexAPI // uses SignalR
|| api is ExchangeBL3PAPI // volume too low
|| api is ExchangeLivecoinAPI // defunct
|| api is ExchangeOKCoinAPI // volume appears to be too low
) { continue; }
//if (api is ExchangeKrakenAPI)
try
{
var delayCTS = new CancellationTokenSource();
var marketSymbols = await api.GetMarketSymbolsAsync();
string testSymbol = null;
if (api is ExchangeKrakenAPI) testSymbol = "XBTUSD";
if (testSymbol == null) testSymbol = marketSymbols.Where(s => // usually highest volume so we're not waiting around here
(s.ToUpper().Contains("BTC") || s.ToUpper().Contains("XBT"))
&& s.ToUpper().Contains("USD")
&& !(s.ToUpper().Contains("TBTC") || s.ToUpper().Contains("WBTC")
|| s.ToUpper().Contains("NHBTC") || s.ToUpper().Contains("BTC3L")
|| s.ToUpper().Contains("USDC") || s.ToUpper().Contains("SUSD")
|| s.ToUpper().Contains("BTC-TUSD")))
.FirstOrDefault();
if (testSymbol == null) testSymbol = marketSymbols.First();
bool thisExchangePassed = false;
using (var socket = await api.GetTradesWebSocketAsync(async kvp =>
{
thisExchangePassed = true;
delayCTS.Cancel(); // msg received. this exchange passes
}, testSymbol))
{
socket.Disconnected += async s => Assert.Fail($"disconnected by exchange {api.GetType().Name}");
await Task.Delay(100000, delayCTS.Token);
if (!thisExchangePassed) Assert.Fail($"No msgs recieved after 100 seconds for exchange {api.GetType().Name}");
}
}
catch (NotImplementedException) { } // no need to test exchanges where trades websocket is not implemented
catch (TaskCanceledException) { } // if the delay task is cancelled
catch (Exception ex) { Assert.Fail($"For exchange {api.GetType().Name}, encountered exception {ex}"); }
}
}
}
}

0 comments on commit 1e56691

Please sign in to comment.