Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
e0d63ab
Add ParseLiveQuery and dependencies
theSlyest Jun 19, 2025
d7c8c71
Added ParseLiveQuerySubscription and refactored accordingly
theSlyest Jun 20, 2025
0932e35
Added EventArgs
theSlyest Jun 21, 2025
8740db2
ParseLiveQueryController initialization
theSlyest Jun 22, 2025
542e3cb
Subscription bug fixes
theSlyest Jun 23, 2025
40044a2
Updated event argument types
theSlyest Jun 23, 2025
0f737a0
Added DualParseLiveQueryEventArgs
theSlyest Jun 23, 2025
98e295a
Live query server error management
theSlyest Jun 23, 2025
c0a6bec
Renamed DualParseLiveQueryEventArgs to ParseLiveQueryDualEventArgs
theSlyest Jun 24, 2025
a267f63
Code quality
theSlyest Jun 25, 2025
4b83d23
Improve code quality
theSlyest Jun 25, 2025
65c73e4
Add null safety for the "where" clause extraction
theSlyest Jun 25, 2025
340f6fb
Improve code quality
theSlyest Jun 25, 2025
7e66bb6
Improvements
theSlyest Jun 25, 2025
84f7060
Null checks
theSlyest Jun 25, 2025
834ff89
Move TimeOut and BufferSize to new LiveQueryServerConnectionData and …
theSlyest Jun 27, 2025
bd36b7d
Minor improvements
theSlyest Jun 28, 2025
e9c6bcc
Improve message parsing
theSlyest Jun 30, 2025
8938a4d
Improve the retrieval of data objects from a message
theSlyest Jun 30, 2025
b65e230
Null safety and small changes
theSlyest Jun 30, 2025
cc5168c
Improve controller disposal
theSlyest Jun 30, 2025
e70789e
Fix race conditions
theSlyest Jun 30, 2025
2cfef04
Websocket exception handling
theSlyest Jun 30, 2025
97313bf
Small clean up
theSlyest Jun 30, 2025
f3374f6
Fix test error
theSlyest Jul 9, 2025
62e81fb
Fix RelationTests
theSlyest Jul 10, 2025
a76014f
Fix UserTests
theSlyest Jul 10, 2025
fbe273a
Fix RelationTests for net9.0
theSlyest Jul 10, 2025
c59316b
Add live query and live query event arg tests
theSlyest Jul 26, 2025
d9ec311
Live query event args test corrections
theSlyest Jul 26, 2025
e3b5df9
Code quality improvement
theSlyest Jul 26, 2025
70e587a
Fix tests
theSlyest Aug 11, 2025
6a50ce4
Tests code quality
theSlyest Aug 29, 2025
871f015
Replaced "int TimeOut" by "TimeSpan Timeout" and other improvements
theSlyest Aug 29, 2025
5444d5d
Improve code quality
theSlyest Aug 29, 2025
96b20f6
Code rabbits improvements
theSlyest Aug 29, 2025
854beaa
Refactor and add tests
theSlyest Sep 8, 2025
a13c7ab
Moved responsibilities from ParseLiveQueryController to 2 new classes
theSlyest Sep 10, 2025
b86a45a
Fine tuning some tests
theSlyest Sep 11, 2025
ea60936
Added TextWebSocketClientTests
theSlyest Sep 16, 2025
8341e12
CodeRabbit required changes
theSlyest Sep 16, 2025
a608262
CodeRabbit requested changes
theSlyest Sep 16, 2025
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
Prev Previous commit
CodeRabbit requested changes
  • Loading branch information
theSlyest committed Sep 16, 2025
commit a6082624bef27b498f49ea6a4a2530c1e27abcdc
40 changes: 20 additions & 20 deletions Parse.Tests/LiveQueryMessageBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task TestBuildConnectMessage()
Assert.IsTrue(message.ContainsKey("applicationId"));
Assert.IsTrue(message.ContainsKey("windowsKey"));
Assert.IsTrue(message.ContainsKey("sessionToken"));
Assert.HasCount(4, message);
Assert.AreEqual(4, message.Count);
Assert.AreEqual("connect", message["op"]);
Assert.AreEqual(Client.Services.LiveQueryServerConnectionData.ApplicationID, message["applicationId"]);
Assert.AreEqual(Client.Services.LiveQueryServerConnectionData.Key, message["windowsKey"]);
Expand All @@ -65,17 +65,17 @@ public void TestBuildUnsubscribeMessage()
Assert.IsNotNull(message);
Assert.IsTrue(message.ContainsKey("op"));
Assert.IsTrue(message.ContainsKey("requestId"));
Assert.HasCount(2, message);
Assert.AreEqual(2, message.Count);
Assert.AreEqual("unsubscribe", message["op"]);
Assert.AreEqual(requestId, message["requestId"]);

Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => builder.BuildUnsubscribeMessage(0));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => builder.BuildUnsubscribeMessage(0));
}

private void ValidateSubscriptionMessage(IDictionary<string, object> message, string expectedOp, int requestId)
{
Assert.IsNotNull(message);
Assert.HasCount(4, message);
Assert.AreEqual(4, message.Count);

Assert.IsTrue(message.ContainsKey("op"));
Assert.AreEqual(expectedOp, message["op"]);
Expand All @@ -84,28 +84,28 @@ private void ValidateSubscriptionMessage(IDictionary<string, object> message, st
Assert.AreEqual(requestId, message["requestId"]);

Assert.IsTrue(message.ContainsKey("query"));
Assert.IsInstanceOfType<IDictionary<string, object>>(message["query"], "The 'query' value should be a Dictionary<string, object>.");
Assert.HasCount(4, (IDictionary<string, object>) message["query"]);
Assert.IsInstanceOfType(message["query"], typeof(IDictionary<string, object>), "The 'query' value should be a Dictionary<string, object>.");
Assert.AreEqual(4, ((IDictionary<string, object>) message["query"]).Count);
IDictionary<string, object> query = message["query"] as IDictionary<string, object>;

Assert.IsTrue(query.ContainsKey("className"), "The 'query' dictionary should contain the 'className' key.");
Assert.AreEqual("DummyClass", query["className"], "The 'className' property should be 'DummyClass'.");

Assert.IsTrue(query.ContainsKey("where"), "The 'query' dictionary should contain the 'where' key.");
Assert.IsInstanceOfType<IDictionary<string, object>>(query["where"], "The 'where' property should be a Dictionary<string, object>.");
Assert.IsInstanceOfType(query["where"], typeof(IDictionary<string, object>), "The 'where' property should be a Dictionary<string, object>.");
IDictionary<string, object> where = (IDictionary<string, object>) query["where"];
Assert.HasCount(1, where, "The 'where' dictionary should contain exactly one key-value pair.");
Assert.AreEqual(1, where.Count, "The 'where' dictionary should contain exactly one key-value pair.");
Assert.IsTrue(where.ContainsKey("foo"), "The 'where' dictionary should contain the 'foo' key.");
Assert.AreEqual("bar", where["foo"], "The 'foo' property in 'where' should be 'bar'.");

Assert.IsTrue(query.ContainsKey("keys"), "The 'query' dictionary should contain the 'keys' key.");
Assert.IsInstanceOfType<string[]>(query["keys"], "The 'keys' property should be a string array.");
Assert.HasCount(1, (string[]) query["keys"], "The 'keys' array should contain exactly one element.");
Assert.IsInstanceOfType(query["keys"], typeof(string[]), "The 'keys' property should be a string array.");
Assert.AreEqual(1, ((string[]) query["keys"]).Length, "The 'keys' array should contain exactly one element.");
Assert.AreEqual("foo", ((string[]) query["keys"])[0], "The 'keys' parameter should contain 'foo'.");

Assert.IsTrue(query.ContainsKey("watch"), "The 'query' dictionary should contain the 'watch' key.");
Assert.IsInstanceOfType<string[]>(query["watch"], "The 'watch' property should be a string array.");
Assert.HasCount(1, (string[]) query["watch"], "The 'watch' array should contain exactly one element.");
Assert.IsInstanceOfType(query["watch"], typeof(string[]), "The 'watch' property should be a string array.");
Assert.AreEqual(1, ((string[]) query["watch"]).Length, "The 'watch' array should contain exactly one element.");
Assert.AreEqual("foo", ((string[]) query["watch"])[0], "The 'watch' parameter should contain 'foo'.");

}
Expand All @@ -118,15 +118,15 @@ public async Task TestBuildSubscribeMessage()
Client.Services,
"DummyClass",
new Dictionary<string, object> { { "foo", "bar" } },
["foo"],
["foo"]);
new[] { "foo" },
new[] { "foo" });
ParseLiveQueryMessageBuilder builder = new ParseLiveQueryMessageBuilder();
IDictionary<string, object> message = await builder.BuildSubscribeMessage<ParseObject>(requestId, liveQuery);

ValidateSubscriptionMessage(message, "subscribe", requestId);

await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () => await builder.BuildSubscribeMessage<ParseObject>(0, liveQuery));
await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => await builder.BuildSubscribeMessage<ParseObject>(requestId, null));
await Assert.ThrowsExceptionAsync<ArgumentOutOfRangeException>(async () => await builder.BuildSubscribeMessage<ParseObject>(0, liveQuery));
await Assert.ThrowsExceptionAsync<ArgumentNullException>(async () => await builder.BuildSubscribeMessage<ParseObject>(requestId, null));
}

[TestMethod]
Expand All @@ -137,14 +137,14 @@ public async Task TestBuildUpdateSubscriptionMessage()
Client.Services,
"DummyClass",
new Dictionary<string, object> { { "foo", "bar" } },
["foo"],
["foo"]);
new[] { "foo" },
new[] { "foo" });
ParseLiveQueryMessageBuilder builder = new ParseLiveQueryMessageBuilder();
IDictionary<string, object> message = await builder.BuildUpdateSubscriptionMessage<ParseObject>(requestId, liveQuery);

ValidateSubscriptionMessage(message, "update", requestId);

await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () => await builder.BuildUpdateSubscriptionMessage<ParseObject>(0, liveQuery));
await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => await builder.BuildUpdateSubscriptionMessage<ParseObject>(requestId, null));
await Assert.ThrowsExceptionAsync<ArgumentOutOfRangeException>(async () => await builder.BuildUpdateSubscriptionMessage<ParseObject>(0, liveQuery));
await Assert.ThrowsExceptionAsync<ArgumentNullException>(async () => await builder.BuildUpdateSubscriptionMessage<ParseObject>(requestId, null));
}
}
2 changes: 1 addition & 1 deletion Parse.Tests/LiveQueryMessageParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void TestGetError()
};

IParseLiveQueryMessageParser.LiveQueryError error = parser.GetError(message);
Assert.HasCount(3, message);
Assert.AreEqual(3, message.Count);
Assert.AreEqual(errorCode, error.Code);
Assert.AreEqual(errorMessage, error.Message);
Assert.AreEqual(reconnect, error.Reconnect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ namespace Parse.Abstractions.Platform.LiveQueries;
public interface IParseLiveQueryMessageParser
{

struct LiveQueryError
/// <summary>
/// Structure representing a Live Query Server error
/// </summary>
readonly struct LiveQueryError
{
public int Code { get; }
public string Message { get; }
Expand Down Expand Up @@ -56,7 +59,7 @@ public LiveQueryError(int code, string message, bool reconnect)
/// </summary>
/// <param name="message">The message containing error details.</param>
/// <returns>
/// A tuple containing the error code, error message, and a boolean indicating whether to reconnect.
/// A LiveQueryError containing the error code, message, and a boolean indicating whether to reconnect.
/// </returns>
LiveQueryError GetError(IDictionary<string, object> message);
}
46 changes: 12 additions & 34 deletions Parse/Platform/LiveQueries/ParseLiveQueryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ParseLiveQueryController : IParseLiveQueryController, IDisposable,
private bool disposed;

/// <summary>
/// Gets or sets the timeout duration, in milliseconds, used by the ParseLiveQueryController
/// Gets or sets the timeout duration used by the ParseLiveQueryController (as a TimeSpan)
/// for various operations, such as establishing a connection or completing a subscription.
/// </summary>
/// <remarks>
Expand Down Expand Up @@ -106,7 +106,7 @@ public enum ParseLiveQueryState
TaskCompletionSource ConnectionSignal { get; set; }
private ConcurrentDictionary<int, TaskCompletionSource> SubscriptionSignals { get; } = new ConcurrentDictionary<int, TaskCompletionSource>();
private ConcurrentDictionary<int, TaskCompletionSource> UnsubscriptionSignals { get; } = new ConcurrentDictionary<int, TaskCompletionSource>();
private ConcurrentDictionary<int, IParseLiveQuerySubscription> Subscriptions { get; set; } = new ConcurrentDictionary<int, IParseLiveQuerySubscription>();
private ConcurrentDictionary<int, IParseLiveQuerySubscription> Subscriptions { get; } = new ConcurrentDictionary<int, IParseLiveQuerySubscription>();

/// <summary>
/// Initializes a new instance of the <see cref="ParseLiveQueryController"/> class.
Expand Down Expand Up @@ -279,20 +279,7 @@ void ProcessCreateEventMessage(IDictionary<string, object> message)

void ProcessErrorMessage(IDictionary<string, object> message)
{
if (!(message.TryGetValue("code", out object codeObj) &&
Int32.TryParse(codeObj?.ToString(), out int code)))
return;

if (!(message.TryGetValue("error", out object errorObj) &&
errorObj is string error))
return;

if (!(message.TryGetValue("reconnect", out object reconnectObj) &&
Boolean.TryParse(reconnectObj?.ToString(), out bool reconnect)))
return;

var liveQueryError = MessageParser.GetError(message);

Error?.Invoke(this, new ParseLiveQueryErrorEventArgs(liveQueryError.Code, liveQueryError.Message, liveQueryError.Reconnect));
}

Expand Down Expand Up @@ -325,27 +312,17 @@ void ProcessConnectionMessage(IDictionary<string, object> message)
ConnectionSignal?.TrySetResult();
}

private async Task<IDictionary<string, object>> AppendSessionToken(IDictionary<string, object> message)
{
string sessionToken = await ParseClient.Instance.Services.GetCurrentSessionToken();
return sessionToken is null
? message
: message.Concat(new Dictionary<string, object> {
{ "sessionToken", sessionToken }
}).ToDictionary();
}

private async Task SendMessage(IDictionary<string, object> message, CancellationToken cancellationToken) =>
await WebSocketClient.SendAsync(JsonUtilities.Encode(message), cancellationToken);

private async Task OpenAsync(CancellationToken cancellationToken = default)
{
if (ParseClient.Instance.Services == null)
if (ParseClient.Instance.Services is null)
{
throw new InvalidOperationException("ParseClient.Services must be initialized before connecting to the LiveQuery server.");
}

if (ParseClient.Instance.Services.LiveQueryServerConnectionData == null)
if (ParseClient.Instance.Services.LiveQueryServerConnectionData is null)
{
throw new InvalidOperationException("ParseClient.Services.LiveQueryServerConnectionData must be initialized before connecting to the LiveQuery server.");
}
Expand Down Expand Up @@ -384,12 +361,12 @@ private async Task EstablishConnectionAsync(CancellationToken cancellationToken)
WebSocketClient.UnknownError += WebSocketClientOnUnknownError;

IDictionary<string, object> message = await MessageBuilder.BuildConnectMessage();
ConnectionSignal = new TaskCompletionSource();

ConnectionSignal = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

try
{
await SendMessage(message, cancellationToken);
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(Timeout);
await ConnectionSignal.Task.WaitAsync(cts.Token);
}
Expand All @@ -404,6 +381,7 @@ private async Task EstablishConnectionAsync(CancellationToken cancellationToken)
}
finally
{
cts.Dispose();
ConnectionSignal = null;
}
}
Expand Down Expand Up @@ -458,16 +436,14 @@ private async Task SendAndWaitForSignalAsync(IDictionary<string, object> message
int requestId,
CancellationToken cancellationToken)
{
TaskCompletionSource tcs = new TaskCompletionSource();
CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
TaskCompletionSource tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
signalDictionary.TryAdd(requestId, tcs);

try
{
await SendMessage(message, cancellationToken);

CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(Timeout);

await tcs.Task.WaitAsync(cts.Token);
}
catch (OperationCanceledException)
Expand All @@ -477,6 +453,8 @@ private async Task SendAndWaitForSignalAsync(IDictionary<string, object> message
finally
{
signalDictionary.TryRemove(requestId, out _);
tcs = null;
cts.Dispose();
}
}

Expand Down
22 changes: 6 additions & 16 deletions Parse/Platform/LiveQueries/ParseLiveQueryMessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<IDictionary<string, object>> BuildConnectMessage() => await Ap
}
});

public async Task<IDictionary<string, object>> BuildSubscribeMessage<T>(int requestId, ParseLiveQuery<T> liveQuery) where T : ParseObject
private async Task<IDictionary<string, object>> BuildSubscriptionMessageCore<T>(string operation, int requestId, ParseLiveQuery<T> liveQuery) where T : ParseObject
{
if (requestId <= 0)
throw new ArgumentOutOfRangeException(nameof(requestId), "Request ID must be greater than zero.");
Expand All @@ -42,27 +42,17 @@ public async Task<IDictionary<string, object>> BuildSubscribeMessage<T>(int requ

return await AppendSessionToken(new Dictionary<string, object>
{
{ "op", "subscribe" },
{ "op", operation },
{ "requestId", requestId },
{ "query", liveQuery.BuildParameters() }
});
}

public async Task<IDictionary<string, object>> BuildUpdateSubscriptionMessage<T>(int requestId, ParseLiveQuery<T> liveQuery) where T : ParseObject
{
if (requestId <= 0)
throw new ArgumentOutOfRangeException(nameof(requestId), "Request ID must be greater than zero.");

if (liveQuery is null)
throw new ArgumentNullException(nameof(liveQuery));
public async Task<IDictionary<string, object>> BuildSubscribeMessage<T>(int requestId, ParseLiveQuery<T> liveQuery) where T : ParseObject
=> await BuildSubscriptionMessageCore("subscribe", requestId, liveQuery);

return await AppendSessionToken(new Dictionary<string, object>
{
{ "op", "update" },
{ "requestId", requestId },
{ "query", liveQuery.BuildParameters() }
});
}
public async Task<IDictionary<string, object>> BuildUpdateSubscriptionMessage<T>(int requestId, ParseLiveQuery<T> liveQuery) where T : ParseObject
=> await BuildSubscriptionMessageCore("update", requestId, liveQuery);

public IDictionary<string, object> BuildUnsubscribeMessage(int requestId)
{
Expand Down
Loading