forked from Transia-RnD/XrplCSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
Fix client connection handling and update ledger object flags #76
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
Platonenkov
wants to merge
7
commits into
release
Choose a base branch
from
dev
base: release
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
7 commits
Select commit
Hold shift + click to select a range
5593062
ci: bump standalone stand to rippled 3.2.1 (#73)
xrpl-release-watch[bot] e63e9ef
fix(client): исключение из OnConnected больше не убивает клиент навсе…
Platonenkov 1d9fa48
feat(models): флаги ledger-объектов, гвард по LedgerFormats.h и покры…
Platonenkov 7639041
feat(protocol): sfLEVersion — версия схемы Vault (rippled #7817) (#74)
Platonenkov 713da1a
feat(models)!: гвард полей ledger-объектов и чистка моделей по ledger…
Platonenkov 3d35665
fix(client): бэкофф при повторных сбоях OnConnected + замечания CodeR…
Platonenkov f3d5d61
fix(client): синхронизировать жизненный цикл сессии переподключения (…
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,190 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using Xrpl.Client; | ||
|
|
||
| namespace Xrpl.Tests | ||
| { | ||
| /// <summary> | ||
| /// Regression tests for <c>ChangeServer</c> pointed at a server that is not up yet. | ||
| /// <para> | ||
| /// <c>ChangeServer</c> used to set the global <c>_isIntentionalDisconnect</c> flag, which was only ever | ||
| /// reset in <c>OnceOpen</c>. When the new server never came up, the flag stayed set, the failure of the | ||
| /// new connection was read as a user disconnect ("Connection closed permanently."), no reconnect loop | ||
| /// was started, and every later call failed with "No connection attempt in progress. Call Connect() | ||
| /// first." - the client was dead even after the server came up. | ||
| /// </para> | ||
| /// </summary> | ||
| [TestClass] | ||
| public class TestUChangeServerFailure | ||
| { | ||
| private CreateMockRippled _mockedRippled; | ||
| private CreateMockRippled _secondRippled; | ||
| private XrplClient _client; | ||
| private int _port; | ||
|
|
||
| private static Dictionary<string, object> ServerInfoResponse() => new Dictionary<string, object> | ||
| { | ||
| { "type", "response" }, | ||
| { "status", "success" }, | ||
| { "result", new Dictionary<string, object> | ||
| { | ||
| { "info", new Dictionary<string, object> | ||
| { | ||
| { "build_version", "test-mock" }, | ||
| { "complete_ledgers", "1-1" }, | ||
| { "server_state", "full" }, | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| private static CreateMockRippled StartMock(int port) | ||
| { | ||
| CreateMockRippled mock = new CreateMockRippled(port) { suppressOutput = true }; | ||
| mock.AddResponse("server_info", ServerInfoResponse()); | ||
|
|
||
| Thread listenerThread = new Thread(() => mock.Start()) { IsBackground = true }; | ||
| listenerThread.Start(); | ||
| return mock; | ||
| } | ||
|
|
||
| [TestInitialize] | ||
| public void MyTestInitialize() | ||
| { | ||
| _port = TestUtils.GetFreePort(); | ||
| _mockedRippled = StartMock(_port); | ||
| } | ||
|
|
||
| [TestCleanup] | ||
| public async Task MyTestCleanup() | ||
| { | ||
| if (_client != null) | ||
| { | ||
| await _client.Disconnect(); | ||
| _client = null; | ||
| } | ||
|
|
||
| _mockedRippled?.Stop(); | ||
| _secondRippled?.Stop(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Switching to a server that is not listening yet must leave the client reconnecting, so it comes | ||
| /// up on its own once that server appears - not stranded in a permanent disconnect. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public async Task TestChangeServerToUnreachableServerRecoversWhenItComesUp() | ||
| { | ||
| _client = new XrplClient($"ws://127.0.0.1:{_port}", new XrplClient.ClientOptions | ||
| { | ||
| RequestPolicy = RequestFailurePolicy.ImmediateFail, | ||
| ReconnectBaseDelay = TimeSpan.FromMilliseconds(200), | ||
| ReconnectMaxDelay = TimeSpan.FromSeconds(1), | ||
| MaxReconnectAttempts = 50, | ||
| StopAfterMaxAttempts = false, | ||
| // Short on purpose: ChangeServer gives up waiting quickly, but the reconnect loop it left | ||
| // behind is what this test is about. | ||
| ConnectionAcquisitionTimeout = TimeSpan.FromSeconds(3), | ||
| ConnectionAttemptTimeout = TimeSpan.FromSeconds(3), | ||
| UseCustomPing = false, | ||
| }); | ||
|
|
||
| await _client.Connect(); | ||
| Assert.IsTrue(_client.connection.IsConnected(), "Precondition: client must be connected to the first server."); | ||
|
|
||
| int secondPort = TestUtils.GetFreePort(); // nothing is listening there yet | ||
|
|
||
| try | ||
| { | ||
| await _client.connection.ChangeServer($"ws://127.0.0.1:{secondPort}"); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| // Expected - the target is not up yet. What matters is the state it leaves behind. | ||
| } | ||
|
|
||
| Assert.AreNotEqual( | ||
| XrpConnectionState.Disconnected, | ||
| _client.connection.CurrentConnectionState, | ||
| "A ChangeServer target that is down is a connection failure, not a permanent disconnect."); | ||
|
|
||
| // The server appears afterwards - exactly the "start the node later" case. | ||
| // The mock binds on a background thread, so a port taken in the meantime would | ||
| // surface as a 30s timeout below rather than as a bind error; check first. | ||
| Assert.IsTrue( | ||
| TestUtils.IsPortStillFree(secondPort), | ||
| $"Port {secondPort} was taken by another process while the test held it — rerun."); | ||
| _secondRippled = StartMock(secondPort); | ||
|
|
||
| DateTime deadline = DateTime.UtcNow + TimeSpan.FromSeconds(30); | ||
| while (!_client.connection.IsConnected() && DateTime.UtcNow < deadline) | ||
| { | ||
| await Task.Delay(TimeSpan.FromMilliseconds(200)); | ||
| } | ||
|
|
||
| Assert.IsTrue( | ||
| _client.connection.IsConnected(), | ||
| $"Client never reached the new server after it came up (state: {_client.connection.CurrentConnectionState})."); | ||
| Assert.AreEqual($"ws://127.0.0.1:{secondPort}", _client.connection.GetUrl()); | ||
|
|
||
| Dictionary<string, object> response = | ||
| await _client.Request(new Dictionary<string, object> { { "command", "server_info" } }); | ||
| Assert.IsNotNull(response, "Client must be usable on the new server."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The same, after an explicit user <c>Disconnect()</c>: the global intentional-disconnect flag left | ||
| /// behind by it must not suppress reconnection for the server <c>ChangeServer</c> switches to. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public async Task TestChangeServerAfterUserDisconnectStillReconnects() | ||
| { | ||
| _client = new XrplClient($"ws://127.0.0.1:{_port}", new XrplClient.ClientOptions | ||
| { | ||
| RequestPolicy = RequestFailurePolicy.ImmediateFail, | ||
| ReconnectBaseDelay = TimeSpan.FromMilliseconds(200), | ||
| ReconnectMaxDelay = TimeSpan.FromSeconds(1), | ||
| MaxReconnectAttempts = 50, | ||
| StopAfterMaxAttempts = false, | ||
| ConnectionAcquisitionTimeout = TimeSpan.FromSeconds(3), | ||
| ConnectionAttemptTimeout = TimeSpan.FromSeconds(3), | ||
| UseCustomPing = false, | ||
| }); | ||
|
|
||
| await _client.Connect(); | ||
| await _client.Disconnect(); | ||
|
|
||
| int secondPort = TestUtils.GetFreePort(); | ||
|
|
||
| try | ||
| { | ||
| await _client.connection.ChangeServer($"ws://127.0.0.1:{secondPort}"); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| // Expected - the target is not up yet. | ||
| } | ||
|
|
||
| Assert.IsTrue( | ||
| TestUtils.IsPortStillFree(secondPort), | ||
| $"Port {secondPort} was taken by another process while the test held it — rerun."); | ||
| _secondRippled = StartMock(secondPort); | ||
|
|
||
| DateTime deadline = DateTime.UtcNow + TimeSpan.FromSeconds(30); | ||
| while (!_client.connection.IsConnected() && DateTime.UtcNow < deadline) | ||
| { | ||
| await Task.Delay(TimeSpan.FromMilliseconds(200)); | ||
| } | ||
|
|
||
| Assert.IsTrue( | ||
| _client.connection.IsConnected(), | ||
| $"Client never reached the new server after a user disconnect (state: {_client.connection.CurrentConnectionState})."); | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: StaticBit-io/XrplCSharp
Length of output: 2573
🏁 Script executed:
Repository: StaticBit-io/XrplCSharp
Length of output: 40908
Prevent the free-port race.
TestUtils.GetFreePortstops its listener before returning, so another process can claimsecondPortbeforeStartMockbinds it. Preserve the listener through startup or retry allocation when binding fails. Apply this at Lines 101 and 158.🤖 Prompt for AI Agents