Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to ignore only some unhandled messages #194

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CryptoExchange.Net.UnitTests/SocketClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CryptoExchange.Net.Objects;
Expand Down Expand Up @@ -233,5 +234,28 @@ public async Task SuccessResponse_Should_ConfirmSubscription()
// assert
Assert.That(client.SubClient.TestSubscription.Confirmed);
}

[TestCase()]
public void ExpectedUnhandledMessaged_Should_BeIgnored()
{
// arrange
var client = new TestSocketClient(opt =>
{
opt.OutputOriginalData = true;
});
var socket = client.CreateSocket();
socket.CanConnect = true;
var _ = client.SubClient.SubscribeToSomethingAsync("BTCUSD", onUpdate => { }, default);

// act
client.SubClient.IgnoredUnhandledMessages = new HashSet<string> { "connected" };
var unhandledMessage = JsonConvert.SerializeObject(new { topic = "unhandled" });
socket.InvokeMessage(JsonConvert.SerializeObject(new { topic = "connected" }));
socket.InvokeMessage(unhandledMessage);

// assert
Assert.That(client.SubClient.UnhandledMessages.Count == 1);
Assert.That(client.SubClient.UnhandledMessages.First().Equals(unhandledMessage));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using CryptoExchange.Net.UnitTests.TestImplementations.Sockets;
using Microsoft.Extensions.Logging;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CryptoExchange.Net.UnitTests.TestImplementations
Expand Down Expand Up @@ -74,6 +75,7 @@ public class TestSocketOptions: SocketExchangeOptions<TestEnvironment>

public class TestSubSocketClient : SocketApiClient
{
public List<object> UnhandledMessages { get; } = new List<object>();
private MessagePath _channelPath = MessagePath.Get().Property("channel");
private MessagePath _topicPath = MessagePath.Get().Property("topic");

Expand All @@ -84,6 +86,18 @@ public TestSubSocketClient(TestSocketOptions options, SocketApiOptions apiOption

}

protected override void HandleUnhandledMessage(IMessageAccessor message)
{
if (message.Underlying is JObject json)
{
UnhandledMessages.Add(json.ToString(Formatting.None));
}
else
{
UnhandledMessages.Add(message.Underlying);
}
}

internal IWebsocket CreateSocketInternal(string address)
{
return CreateSocket(address);
Expand Down
15 changes: 15 additions & 0 deletions CryptoExchange.Net/Clients/SocketApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public abstract class SocketApiClient : BaseApiClient, ISocketApiClient
/// </summary>
protected internal bool UnhandledMessageExpected { get; set; }

/// <summary>
/// To be used if <see cref="UnhandledMessageExpected"/> is false and some messages should be ignored
/// </summary>
protected internal HashSet<string>? IgnoredUnhandledMessages { get; set; } = null;

/// <summary>
/// If true a subscription will accept message before the confirmation of a subscription has been received
/// </summary>
Expand Down Expand Up @@ -494,6 +499,16 @@ protected virtual void HandleUnhandledMessage(IMessageAccessor message)
{
}

/// <summary>
/// Check if an unhandled message with the given listen id should be ignored
/// </summary>
/// <param name="listenId"></param>
/// <returns></returns>
public bool IsUnhandledMessageIgnored(string listenId)
{
return IgnoredUnhandledMessages?.Contains(listenId) ?? false;
}

/// <summary>
/// Connect a socket
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion CryptoExchange.Net/Sockets/SocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ protected virtual void HandleStreamMessage(WebSocketMessageType type, ReadOnlyMe

if (processors.Count == 0)
{
if (!ApiClient.UnhandledMessageExpected)
if (!ApiClient.UnhandledMessageExpected && !ApiClient.IsUnhandledMessageIgnored(listenId))
{
List<string> listenerIds;
lock (_listenersLock)
Expand Down
Loading