Repository navigation
fix: handle failed connection attempts once #139
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
NorthArea
wants to merge
5
commits into
master
Choose a base branch
from
northarea/reconnect-loop-fixes
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.
+691
−16
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
871238e
fix(core): report greeting timeout as a timeout
38d7bc2
fix(core): skip close event before the greeting
2502fdc
fix(core): register box.shutdown callback once
e8e477f
fix(pool): stop connect timer on failed attempt
f0d30b1
fix(pool): pass reconnectAfter to pool entries
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
90 changes: 90 additions & 0 deletions
90
tarantool-core/src/test/java/io/tarantool/core/ShutdownWatcherRegistrationTest.java
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,90 @@ | ||
| /* | ||
| * Copyright (c) 2026 VK DIGITAL TECHNOLOGIES LIMITED LIABILITY COMPANY | ||
| * All Rights Reserved. | ||
| */ | ||
|
|
||
| package io.tarantool.core; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.ServerSocket; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import io.netty.bootstrap.Bootstrap; | ||
| import io.netty.channel.ChannelOption; | ||
| import io.netty.channel.EventLoopGroup; | ||
| import io.netty.channel.MultiThreadIoEventLoopGroup; | ||
| import io.netty.channel.nio.NioIoHandler; | ||
| import io.netty.channel.socket.nio.NioSocketChannel; | ||
| import io.netty.util.HashedWheelTimer; | ||
| import io.netty.util.Timer; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import io.tarantool.core.connection.ConnectionFactory; | ||
|
|
||
| /** Reconnect attempts must not add a new {@code box.shutdown} callback each time. */ | ||
| public class ShutdownWatcherRegistrationTest { | ||
|
|
||
| private static final int ATTEMPTS = 5; | ||
|
|
||
| private EventLoopGroup group; | ||
| private Timer timer; | ||
| private IProtoClientImpl client; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| group = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory()); | ||
| timer = new HashedWheelTimer(); | ||
| Bootstrap bootstrap = | ||
| new Bootstrap() | ||
| .group(group) | ||
| .channel(NioSocketChannel.class) | ||
| .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000); | ||
| client = new IProtoClientImpl(new ConnectionFactory(bootstrap, timer), timer); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() throws Exception { | ||
| client.close(); | ||
| timer.stop(); | ||
| group.shutdownGracefully().get(); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_failedConnects_shouldRegisterShutdownCallbackOnce() throws Exception { | ||
| InetSocketAddress refused = closedPortAddress(); | ||
|
|
||
| for (int i = 0; i < ATTEMPTS; i++) { | ||
| CompletableFuture<Void> attempt = client.connect(refused, 1000, true); | ||
| assertThrows(ExecutionException.class, () -> attempt.get(5, TimeUnit.SECONDS)); | ||
| } | ||
|
|
||
| assertEquals(1, shutdownCallbacksCount()); | ||
| } | ||
|
|
||
| private static InetSocketAddress closedPortAddress() throws Exception { | ||
| try (ServerSocket socket = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) { | ||
| return new InetSocketAddress(socket.getInetAddress(), socket.getLocalPort()); | ||
| } | ||
| } | ||
|
|
||
| // The callbacks are not exposed by the API, so the test reads them directly. | ||
| @SuppressWarnings("unchecked") | ||
| private int shutdownCallbacksCount() throws Exception { | ||
| Field watchersField = IProtoClientImpl.class.getDeclaredField("watchers"); | ||
| watchersField.setAccessible(true); | ||
| Map<String, Watcher> watchers = (Map<String, Watcher>) watchersField.get(client); | ||
| Field callbacksField = Watcher.class.getDeclaredField("callbacks"); | ||
| callbacksField.setAccessible(true); | ||
| return ((List<?>) callbacksField.get(watchers.get("box.shutdown"))).size(); | ||
| } | ||
| } |
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.
Мне кажется, что добавлять аргумент address только ради того, чтобы в сообщении исключения был адрес - лишнее, поскольку клиентский код, который вызывает connect(), он уже знает собственно адрес и может правильно растолковать, о чём же была ошибка ожидания.