forked from Transia-RnD/XrplCSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
Очередь потока существует до того, как потребитель подпишется: пинг-таймер больше не гасит процессор #114
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
Merged
Merged
Очередь потока существует до того, как потребитель подпишется: пинг-таймер больше не гасит процессор #114
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d47dff8
fix(client): очередь потока существует до того, как потребитель подпи…
Platonenkov afbdf0c
docs(client): список мест остановки процессора был неполон
Platonenkov 5eb7c96
docs(client): комментарии описывали порядок, которого больше нет; тес…
Platonenkov 89dc148
docs(client): один remarks на член, документация помощника догоняет н…
Platonenkov 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
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
159 changes: 159 additions & 0 deletions
159
Tests/Xrpl.Tests/Client/TestUStreamProcessorStartsBeforeCallbacks.cs
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,159 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| using System; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using Xrpl.Client; | ||
| using Xrpl.Models.Subscriptions; | ||
| using Xrpl.Tests; | ||
|
|
||
| namespace XrplTests.Client; | ||
|
|
||
| /// <summary> | ||
| /// Frames answered while the <c>OnConnected</c> callback is still running go through the queue, | ||
| /// like every other frame. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Subscribing from <c>OnConnected</c> is the ordinary pattern - it is what the wallet consuming | ||
| /// this SDK does - and the node can answer that subscription before the handler returns. The | ||
| /// message processor used to start at the very end of <c>OnceOpen</c>, after the callback, so | ||
| /// those first frames found no channel and took the fallback: outside | ||
| /// <c>StreamMessageQueueCapacity</c>, uncounted by <c>DroppedStreamMessages</c>, and dispatched | ||
| /// concurrently rather than one at a time. The events most likely to arrive out of order were | ||
| /// precisely the first ones after connecting. | ||
| /// <para> | ||
| /// Moving the start earlier was blocked by an unrelated coupling: <c>StartPingTimer</c> begins with | ||
| /// <c>StopPingTimerSync</c>, which stopped the message processor as well, so an earlier start was | ||
| /// torn down again moments later. The two lifecycles are now separate. | ||
| /// </para> | ||
| /// </remarks> | ||
| [TestClass] | ||
| public class TestUStreamProcessorStartsBeforeCallbacks | ||
| { | ||
| private const string ScriptedReply = | ||
| "{\"id\":__ID__,\"status\":\"success\",\"type\":\"response\",\"result\":{}}"; | ||
|
|
||
| private const string TransactionMessage = """ | ||
| { | ||
| "type": "transaction", | ||
| "status": "closed", | ||
| "validated": true, | ||
| "engine_result": "tesSUCCESS", | ||
| "tx_json": { "TransactionType": "Payment", "Account": "rP9jPyP5kyvFRb6ZiRghAGw5u8SGAmU4bd", "Sequence": 4 }, | ||
| "meta": { "AffectedNodes": [], "TransactionIndex": 0, "TransactionResult": "tesSUCCESS" } | ||
| } | ||
| """; | ||
|
|
||
| /// <summary> | ||
| /// A frame driven from inside the <c>OnConnected</c> handler reaches the queue, not the | ||
| /// fallback. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <c>FallbackDispatchedStreamMessages</c> is the whole assertion: it counts frames dispatched | ||
| /// outside the queue, so before this change it would have counted this one. | ||
| /// </remarks> | ||
| [TestMethod] | ||
| public async Task TestUFrameAnsweredDuringOnConnectedGoesThroughTheQueue() | ||
| { | ||
| using ScriptedResponseServer server = new ScriptedResponseServer(ScriptedReply); | ||
| using XrplClient client = new XrplClient(server.Url); | ||
|
|
||
| bool channelExisted = false; | ||
| long fallbackDuringCallback = -1; | ||
| TaskCompletionSource callbackDone = new TaskCompletionSource( | ||
| TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| client.connection.OnConnected += async () => | ||
| { | ||
| // Stands in for the node answering a subscription issued from this very handler. | ||
| channelExisted = client.connection.IsMessageProcessorRunning; | ||
| await client.connection.IOnMessageFastPath( | ||
| Encoding.UTF8.GetBytes(TransactionMessage), | ||
| client.connection.ActiveSessionId); | ||
| fallbackDuringCallback = client.connection.FallbackDispatchedStreamMessages; | ||
| callbackDone.TrySetResult(); | ||
| }; | ||
|
|
||
| await client.Connect(); | ||
|
|
||
| try | ||
| { | ||
| // Connect() returning is not enough: OnceOpen resolves the waiters before invoking | ||
| // this callback, so without the wait the assertions below can read their defaults. | ||
| Task finished = await Task.WhenAny(callbackDone.Task, Task.Delay(TimeSpan.FromSeconds(5))); | ||
| Assert.AreSame(callbackDone.Task, finished, "the OnConnected handler never ran"); | ||
|
|
||
| Assert.IsTrue(channelExisted, | ||
| "the queue did not exist while consumer code was subscribing - frames answered there take the fallback"); | ||
| Assert.AreEqual(0L, fallbackDuringCallback, | ||
| "a frame answered during OnConnected went round the queue"); | ||
| } | ||
| finally | ||
| { | ||
| await client.Disconnect(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The queue survives the ping timer starting, which happens after the callback. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This is the coupling that blocked the fix: <c>StartPingTimer</c> calls | ||
| /// <c>StopPingTimerSync</c>, and that used to stop the message processor too. Starting the | ||
| /// processor early is worth nothing if the ping timer tears it down a moment later, and the | ||
| /// symptom is invisible - the fallback keeps delivering frames, just without any of the queue's | ||
| /// guarantees. | ||
| /// </remarks> | ||
| [TestMethod] | ||
| public async Task TestUStartingThePingTimerLeavesTheProcessorRunning() | ||
| { | ||
| using ScriptedResponseServer server = new ScriptedResponseServer(ScriptedReply); | ||
| using XrplClient client = new XrplClient( | ||
| server.Url, | ||
| new XrplClient.ClientOptions { UseCustomPing = true }); | ||
| await client.Connect(); | ||
|
|
||
| try | ||
| { | ||
| // The ping timer starts at the very end of OnceOpen, after Connect() has returned. | ||
| // Asserting before it runs would pass whether or not it takes the processor down - | ||
| // the test has to wait for the thing that used to break it. | ||
| DateTime deadline = DateTime.UtcNow.AddSeconds(5); | ||
| while (!client.connection.IsPingTimerRunning && DateTime.UtcNow < deadline) | ||
| { | ||
| await Task.Delay(10); | ||
| } | ||
|
|
||
| Assert.IsTrue(client.connection.IsPingTimerRunning, "the ping timer never started"); | ||
|
|
||
| Assert.IsTrue(client.connection.IsMessageProcessorRunning, | ||
| "the ping timer took the message processor down with it"); | ||
|
|
||
| long fallbackBefore = client.connection.FallbackDispatchedStreamMessages; | ||
|
|
||
| TaskCompletionSource<TransactionStream> received = new TaskCompletionSource<TransactionStream>( | ||
| TaskCreationOptions.RunContinuationsAsynchronously); | ||
| client.OnTransaction += r => | ||
| { | ||
| received.TrySetResult(r); | ||
| return Task.CompletedTask; | ||
| }; | ||
|
|
||
| await client.connection.IOnMessageFastPath( | ||
| Encoding.UTF8.GetBytes(TransactionMessage), | ||
| client.connection.ActiveSessionId); | ||
|
|
||
| Task completed = await Task.WhenAny(received.Task, Task.Delay(TimeSpan.FromSeconds(5))); | ||
| Assert.AreSame(received.Task, completed, "the frame never reached the handler"); | ||
|
|
||
| Assert.AreEqual(0L, client.connection.FallbackDispatchedStreamMessages - fallbackBefore, | ||
| "the frame took the fallback, so the processor was not running after all"); | ||
| } | ||
| finally | ||
| { | ||
| await client.Disconnect(); | ||
| } | ||
| } | ||
| } | ||
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.