-
-
Notifications
You must be signed in to change notification settings - Fork 258
feat: Add livequery support #411
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
Open
theSlyest
wants to merge
55
commits into
parse-community:master
Choose a base branch
from
theSlyest:livequery
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
e0d63ab
Add ParseLiveQuery and dependencies
theSlyest d7c8c71
Added ParseLiveQuerySubscription and refactored accordingly
theSlyest 0932e35
Added EventArgs
theSlyest 8740db2
ParseLiveQueryController initialization
theSlyest 542e3cb
Subscription bug fixes
theSlyest 40044a2
Updated event argument types
theSlyest 0f737a0
Added DualParseLiveQueryEventArgs
theSlyest 98e295a
Live query server error management
theSlyest c0a6bec
Renamed DualParseLiveQueryEventArgs to ParseLiveQueryDualEventArgs
theSlyest a267f63
Code quality
theSlyest 4b83d23
Improve code quality
theSlyest 65c73e4
Add null safety for the "where" clause extraction
theSlyest 340f6fb
Improve code quality
theSlyest 7e66bb6
Improvements
theSlyest 84f7060
Null checks
theSlyest 834ff89
Move TimeOut and BufferSize to new LiveQueryServerConnectionData and …
theSlyest bd36b7d
Minor improvements
theSlyest e9c6bcc
Improve message parsing
theSlyest 8938a4d
Improve the retrieval of data objects from a message
theSlyest b65e230
Null safety and small changes
theSlyest cc5168c
Improve controller disposal
theSlyest e70789e
Fix race conditions
theSlyest 2cfef04
Websocket exception handling
theSlyest 97313bf
Small clean up
theSlyest f3374f6
Fix test error
theSlyest 62e81fb
Fix RelationTests
theSlyest a76014f
Fix UserTests
theSlyest fbe273a
Fix RelationTests for net9.0
theSlyest c59316b
Add live query and live query event arg tests
theSlyest d9ec311
Live query event args test corrections
theSlyest e3b5df9
Code quality improvement
theSlyest 70e587a
Fix tests
theSlyest 6a50ce4
Tests code quality
theSlyest 871f015
Replaced "int TimeOut" by "TimeSpan Timeout" and other improvements
theSlyest 5444d5d
Improve code quality
theSlyest 96b20f6
Code rabbits improvements
theSlyest 854beaa
Refactor and add tests
theSlyest a13c7ab
Moved responsibilities from ParseLiveQueryController to 2 new classes
theSlyest b86a45a
Fine tuning some tests
theSlyest ea60936
Added TextWebSocketClientTests
theSlyest 8341e12
CodeRabbit required changes
theSlyest a608262
CodeRabbit requested changes
theSlyest 75b3816
Added tests
theSlyest b676df1
CodeRabbitAI suggested changes
theSlyest da1aea7
Merge branch 'master' into livequery
theSlyest 5ae15c6
CodeRabbitAI suggestions
theSlyest e983359
CodeRabbitAI suggested changes
theSlyest d9669e9
Suggested changes
theSlyest ed1a751
CodeRabbitAI suggestions
theSlyest 86cdeac
Add ParseClient constructor test with LiveQuery
theSlyest 172a4fc
Add missing tests
theSlyest 1fc8184
Add ServiceHub unit tests
theSlyest 38dd690
Suggested changes
theSlyest 43c5da1
Requested changes
theSlyest e90c570
Fixed failing test
theSlyest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Parse.Infrastructure; | ||
|
|
||
| namespace Parse.Tests; | ||
|
|
||
| [TestClass] | ||
| public class ClientTests | ||
| { | ||
| [TestMethod] | ||
| public void TestParseClientConstructor() | ||
| { | ||
| ParseClient client = new("appId", "https://parse.example.com/", "dotnetKey"); | ||
| Assert.AreEqual("appId", client.ServerConnectionData.ApplicationID); | ||
| Assert.AreEqual("https://parse.example.com/", client.ServerConnectionData.ServerURI); | ||
| Assert.AreEqual("dotnetKey", client.ServerConnectionData.Key); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestParseClientLiveQueryConstructor() | ||
| { | ||
| ServerConnectionData serverConnectionData = new() | ||
| { | ||
| ApplicationID = "appId", | ||
| ServerURI = "https://parse.example.com/", | ||
| Key = "dotnetKey" | ||
| }; | ||
|
|
||
| LiveQueryServerConnectionData liveQueryServerConnectionData = new() | ||
| { | ||
| ApplicationID = "appId", | ||
| ServerURI = "https://parse.example.com/", | ||
| Key = "dotnetKey" | ||
| }; | ||
|
|
||
| ParseClient client = new(serverConnectionData, liveQueryServerConnectionData); | ||
| Assert.AreEqual("appId", client.ServerConnectionData.ApplicationID); | ||
| Assert.AreEqual("https://parse.example.com/", client.ServerConnectionData.ServerURI); | ||
| Assert.AreEqual("dotnetKey", client.ServerConnectionData.Key); | ||
|
|
||
| Assert.AreEqual("appId", client.LiveQueryServerConnectionData.ApplicationID); | ||
| Assert.AreEqual("https://parse.example.com/", client.LiveQueryServerConnectionData.ServerURI); | ||
| Assert.AreEqual("dotnetKey", client.LiveQueryServerConnectionData.Key); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestPublicize() | ||
| { | ||
| ParseClient previous = ParseClient.Instance; | ||
| ParseClient client = new("appId", "https://parse.example.com/", "dotnetKey"); | ||
| try { | ||
| client.Publicize(); | ||
| Assert.AreSame(client, ParseClient.Instance); | ||
| } finally { | ||
| previous?.Publicize(); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestConstructorWithTestTrue() | ||
| { | ||
| ServerConnectionData data = new() | ||
| { | ||
| ApplicationID = "appId", | ||
| ServerURI = "https://parse.example.com/", | ||
| Key = "key", | ||
| Test = true | ||
| }; | ||
| ParseClient client = new(data); | ||
| Assert.AreEqual("https://parse.example.com/", client.ServerConnectionData.ServerURI); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| using System.Collections.Generic; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Parse.Infrastructure.Utilities; | ||
|
|
||
| namespace Parse.Tests; | ||
|
|
||
| [TestClass] | ||
| public class InfrastructureTests | ||
| { | ||
| [TestMethod] | ||
| public void TestFlexibleDictionaryWrapper() | ||
| { | ||
| // Arrange | ||
| Dictionary<string, int> inner = new() | ||
| { | ||
| { "char", 1 }, | ||
| { "string", 594 } | ||
| }; | ||
| FlexibleDictionaryWrapper<int, int> wrapper = new(inner); | ||
|
|
||
| // Act & Assert | ||
| Assert.AreEqual<int>(1, wrapper["char"]); | ||
| Assert.AreEqual<int>(594, wrapper["string"]); | ||
|
|
||
| wrapper["new"] = 615; | ||
| Assert.AreEqual<int>(615, inner["new"]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestFlexibleListWrapper() | ||
| { | ||
| // Arrange | ||
| List<object> inner = [1, 6.7f]; | ||
| FlexibleListWrapper<int, object> wrapper = new(inner); | ||
|
|
||
| // Act & Assert | ||
| Assert.AreEqual(1, wrapper[0]); | ||
| Assert.AreEqual(7, wrapper[1]); | ||
|
|
||
| wrapper.Add(2); | ||
| Assert.AreEqual(2, inner[2]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestConversion() | ||
| { | ||
| // Test basic conversions | ||
| Assert.AreEqual(123, Conversion.ConvertTo<int>("123")); | ||
| Assert.AreEqual(123L, Conversion.ConvertTo<long>(123)); | ||
| Assert.AreEqual(123.45, (double)Conversion.ConvertTo<double>("123.45"), 1e-10); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestGetOrDefault() | ||
| { | ||
| Dictionary<string, string> dict = new Dictionary<string, string> { { "key", "value" } }; | ||
| Assert.AreEqual("value", dict.GetOrDefault("key", "default")); | ||
| Assert.AreEqual("default", dict.GetOrDefault("missing", "default")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestCollectionsEqual() | ||
| { | ||
| List<int> list1 = [1, 2, 3]; | ||
| List<int> list2 = [1, 2, 3]; | ||
| List<int> list3 = [1, 2, 4]; | ||
|
|
||
| Assert.IsTrue(list1.CollectionsEqual(list2)); | ||
| Assert.IsFalse(list1.CollectionsEqual(list3)); | ||
| Assert.IsFalse(list1.CollectionsEqual(null)); | ||
| Assert.IsTrue(((List<int>)null).CollectionsEqual(null)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Moq; | ||
| using Parse.Abstractions.Infrastructure.Execution; | ||
| using Parse.Abstractions.Platform.LiveQueries; | ||
| using Parse.Platform.LiveQueries; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System.Reflection; | ||
|
|
||
| namespace Parse.Tests; | ||
|
|
||
| [TestClass] | ||
| public class LiveQueryControllerTests | ||
| { | ||
| private readonly Mock<IWebSocketClient> _webSocketClientMock; | ||
| private readonly Mock<IParseLiveQueryMessageParser> _messageParserMock; | ||
| private readonly Mock<IParseLiveQueryMessageBuilder> _messageBuilderMock; | ||
| private readonly TimeSpan _timeout = TimeSpan.FromSeconds(1); | ||
|
|
||
| public LiveQueryControllerTests() | ||
| { | ||
| _webSocketClientMock = new Mock<IWebSocketClient>(); | ||
| _messageParserMock = new Mock<IParseLiveQueryMessageParser>(); | ||
| _messageBuilderMock = new Mock<IParseLiveQueryMessageBuilder>(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ProcessMessage_UnknownOperation_DoesNotThrow() | ||
| { | ||
| ParseLiveQueryController controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| Dictionary<string, object> message = new Dictionary<string, object> { { "op", "unknown" } }; | ||
| MethodInfo processMessage = controller.GetType().GetMethod("ProcessMessage", BindingFlags.NonPublic | BindingFlags.Instance); | ||
| processMessage.Invoke(controller, new object[] { message }); | ||
| // No exception expected | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ProcessMessage_MissingOp_DoesNotThrow() | ||
| { | ||
| ParseLiveQueryController controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| Dictionary<string, object> message = new Dictionary<string, object>(); | ||
| MethodInfo processMessage = controller.GetType().GetMethod("ProcessMessage", BindingFlags.NonPublic | BindingFlags.Instance); | ||
| processMessage.Invoke(controller, new object[] { message }); | ||
| // No exception expected | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ProcessConnectionMessage_SetsStateConnected() | ||
| { | ||
| ParseLiveQueryController controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| _messageParserMock.Setup(m => m.GetClientId(It.IsAny<IDictionary<string, object>>())).Returns("clientId"); | ||
| Dictionary<string, object> message = new Dictionary<string, object> { { "op", "connected" } }; | ||
| MethodInfo processConnectionMessage = controller.GetType().GetMethod("ProcessConnectionMessage", BindingFlags.NonPublic | BindingFlags.Instance); | ||
| processConnectionMessage.Invoke(controller, new object[] { message }); | ||
| Assert.AreEqual(ParseLiveQueryController.ParseLiveQueryState.Connected, controller.State); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ProcessErrorMessage_RaisesErrorEvent() | ||
| { | ||
| ParseLiveQueryController controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| IParseLiveQueryMessageParser.LiveQueryError error = new IParseLiveQueryMessageParser.LiveQueryError { Code = 99, Message = "err", Reconnect = false }; | ||
| _messageParserMock.Setup(m => m.GetError(It.IsAny<IDictionary<string, object>>())).Returns(error); | ||
| bool raised = false; | ||
| controller.Error += (s, e) => { raised = true; Assert.AreEqual(99, e.Code); }; | ||
| MethodInfo processErrorMessage = controller.GetType().GetMethod("ProcessErrorMessage", BindingFlags.NonPublic | BindingFlags.Instance); | ||
| processErrorMessage.Invoke(controller, new object[] { new Dictionary<string, object>() }); | ||
| Assert.IsTrue(raised); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Constructor_InitializesStateToClosed() | ||
| { | ||
| ParseLiveQueryController controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| Assert.AreEqual(ParseLiveQueryController.ParseLiveQueryState.Closed, controller.State); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task ConnectAsync_ThrowsIfDisposed() | ||
| { | ||
| ParseLiveQueryController controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| controller.Dispose(); | ||
| await Assert.ThrowsAsync<ObjectDisposedException>(() => controller.ConnectAsync()); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DisposeAsync_ClosesConnection() | ||
| { | ||
| ParseLiveQueryController controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| _webSocketClientMock.Setup(w => w.CloseAsync(It.IsAny<CancellationToken>())).Returns(Task.CompletedTask).Verifiable(); | ||
| await controller.DisposeAsync(); | ||
| _webSocketClientMock.Verify(w => w.CloseAsync(It.IsAny<CancellationToken>()), Times.Once); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Dispose_SetsDisposedFlag() | ||
| { | ||
| ParseLiveQueryController controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| controller.Dispose(); | ||
| Assert.Throws<ObjectDisposedException>(() => controller.ConnectAsync().GetAwaiter().GetResult()); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Error_Event_Is_Raised_On_Error_Message() | ||
| { | ||
| ParseLiveQueryController controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| IParseLiveQueryMessageParser.LiveQueryError error = new IParseLiveQueryMessageParser.LiveQueryError { Code = 42, Message = "Test error", Reconnect = true }; | ||
| _messageParserMock.Setup(m => m.GetError(It.IsAny<IDictionary<string, object>>())).Returns(error); | ||
| bool eventRaised = false; | ||
| controller.Error += (sender, args) => | ||
| { | ||
| eventRaised = true; | ||
| Assert.AreEqual(42, args.Code); | ||
| Assert.AreEqual("Test error", args.Error); | ||
| Assert.IsTrue(args.Reconnect); | ||
| }; | ||
| Dictionary<string, object> message = new Dictionary<string, object> { { "op", "error" } }; | ||
| MethodInfo processMessage = controller.GetType().GetMethod("ProcessMessage", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | ||
| processMessage.Invoke(controller, new object[] { message }); | ||
| Assert.IsTrue(eventRaised); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidateClientMessage_ReturnsFalse_WhenClientIdMissing() | ||
| { | ||
| var controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| MethodInfo validate = controller.GetType().GetMethod("ValidateClientMessage", BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
|
||
| _messageParserMock.Setup(m => m.GetClientId(It.IsAny<IDictionary<string, object>>())).Returns((string)null); | ||
| _messageParserMock.Setup(m => m.GetRequestId(It.IsAny<IDictionary<string, object>>())).Returns(123); | ||
|
|
||
| var message = new Dictionary<string, object>(); | ||
| object[] args = { message, 0 }; | ||
| bool result = (bool)validate.Invoke(controller, args); | ||
|
|
||
| Assert.IsFalse(result, "Expected validation to fail when client id is null"); | ||
| // requestId should remain default since client id was invalid | ||
| Assert.AreEqual(0, (int)args[1]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidateClientMessage_ReturnsFalse_WhenRequestIdZero() | ||
| { | ||
| var controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| MethodInfo validate = controller.GetType().GetMethod("ValidateClientMessage", BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
|
||
| _messageParserMock.Setup(m => m.GetClientId(It.IsAny<IDictionary<string, object>>())).Returns("cid"); | ||
| _messageParserMock.Setup(m => m.GetRequestId(It.IsAny<IDictionary<string, object>>())).Returns(0); | ||
|
|
||
| var message = new Dictionary<string, object>(); | ||
| object[] args = { message, 0 }; | ||
| bool result = (bool)validate.Invoke(controller, args); | ||
|
|
||
| Assert.IsFalse(result, "Expected validation to fail when request id is zero"); | ||
| Assert.AreEqual(0, (int)args[1]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidateClientMessage_ReturnsTrue_AndSetsFields_WhenValid() | ||
| { | ||
| var controller = new ParseLiveQueryController(_timeout, _webSocketClientMock.Object, _messageParserMock.Object, _messageBuilderMock.Object); | ||
| MethodInfo validate = controller.GetType().GetMethod("ValidateClientMessage", BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
|
||
| _messageParserMock.Setup(m => m.GetClientId(It.IsAny<IDictionary<string, object>>())).Returns("cid"); | ||
| _messageParserMock.Setup(m => m.GetRequestId(It.IsAny<IDictionary<string, object>>())).Returns(999); | ||
|
|
||
| var message = new Dictionary<string, object>(); | ||
| object[] args = { message, 0 }; | ||
| bool result = (bool)validate.Invoke(controller, args); | ||
|
|
||
| Assert.IsTrue(result, "Expected validation to succeed with valid client id and request id"); | ||
| Assert.AreEqual(999, (int)args[1]); | ||
|
|
||
| // verify controller's internal ClientId property was set appropriately | ||
| PropertyInfo clientIdProp = controller.GetType().GetProperty("ClientId", BindingFlags.NonPublic | BindingFlags.Instance); | ||
| Assert.AreEqual("cid", clientIdProp.GetValue(controller)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.