Repository navigation
fix(sender): add connect and read timeouts to SyncSender #387
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
base: master
Are you sure you want to change the base?
Changes from all commits
423bf81
15fbaa9
b107848
7ebf1d2
710a31b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,11 @@ | |
| import static org.hamcrest.Matchers.nullValue; | ||
| import static org.hamcrest.core.Is.is; | ||
| import static org.hamcrest.core.IsInstanceOf.instanceOf; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| import com.rollbar.api.payload.Payload; | ||
| import com.rollbar.api.payload.data.Client; | ||
| import com.rollbar.api.payload.data.Level; | ||
| import com.rollbar.api.payload.data.Notifier; | ||
|
|
@@ -19,12 +23,19 @@ | |
| import com.rollbar.notifier.provider.notifier.NotifierProvider; | ||
| import com.rollbar.notifier.sender.BufferedSender; | ||
| import com.rollbar.notifier.sender.Sender; | ||
| import com.rollbar.notifier.sender.SyncSender; | ||
| import com.rollbar.notifier.sender.exception.SenderException; | ||
| import com.rollbar.notifier.sender.listener.SenderListener; | ||
| import com.rollbar.notifier.transformer.Transformer; | ||
| import com.rollbar.notifier.uuid.UuidGenerator; | ||
| import java.net.InetAddress; | ||
| import java.net.Proxy; | ||
| import java.net.ServerSocket; | ||
| import java.net.SocketTimeoutException; | ||
| import java.util.Map; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.MockitoJUnit; | ||
| import org.mockito.junit.MockitoRule; | ||
|
|
@@ -97,6 +108,8 @@ public void shouldBuildConfigurationWithDefaults() { | |
| assertThat(config.handleUncaughtErrors(), is(true)); | ||
| assertThat(config.isEnabled(), is(true)); | ||
| assertThat(config.proxy(), is(nullValue())); | ||
| assertThat(config.connectTimeoutMillis(), is(SyncSender.DEFAULT_CONNECT_TIMEOUT_MILLIS)); | ||
| assertThat(config.readTimeoutMillis(), is(SyncSender.DEFAULT_READ_TIMEOUT_MILLIS)); | ||
|
|
||
| assertThat(config.defaultMessageLevel(), equalTo(Level.WARNING)); | ||
| assertThat(config.defaultThrowableLevel(), equalTo(Level.ERROR)); | ||
|
|
@@ -204,4 +217,57 @@ public void shouldBuildWithConfig() { | |
| assertThat(config.isEnabled(), is(copy.isEnabled())); | ||
| assertThat(config.maximumTelemetryData(), is(maximumTelemetryData)); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldBuildWithTimeoutsAndCopyThem() { | ||
| Config config = ConfigBuilder.withAccessToken(ACCESS_TOKEN) | ||
| .connectTimeoutMillis(1234) | ||
| .readTimeoutMillis(5678) | ||
| .build(); | ||
|
|
||
| Config copy = withConfig(config).build(); | ||
|
|
||
| assertThat(config.connectTimeoutMillis(), is(1234)); | ||
| assertThat(config.readTimeoutMillis(), is(5678)); | ||
| assertThat(copy.connectTimeoutMillis(), is(1234)); | ||
| assertThat(copy.readTimeoutMillis(), is(5678)); | ||
| } | ||
|
Comment on lines
+221
to
+234
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low: Most users will set the new timeouts on the configuration builder, but the new test only checks that the values are stored on the configuration and survive a copy. Nothing checks that the sender the builder creates actually uses them. If the two lines that pass the values to the sender ( |
||
|
|
||
| @Test(timeout = 10_000) | ||
| public void shouldApplyReadTimeoutToTheDefaultSender() throws Exception { | ||
| // Accepts the connection (via the backlog) but never responds. If the configured timeout did | ||
| // not reach the sender, it would wait for the 30 s default and this test would time out. | ||
| String host = "127.0.0.1"; | ||
| try (ServerSocket silentServer = new ServerSocket(0, 1, InetAddress.getByName(host))) { | ||
| Config config = ConfigBuilder.withAccessToken(ACCESS_TOKEN) | ||
| .endpoint("http://" + host + ":" + silentServer.getLocalPort() + "/api/1/item/") | ||
| .readTimeoutMillis(200) | ||
| .build(); | ||
| try { | ||
| Sender defaultSender = ((BufferedSender) config.sender()).sender(); | ||
| assertThat(defaultSender, is(instanceOf(SyncSender.class))); | ||
| SenderListener listener = mock(SenderListener.class); | ||
| defaultSender.addListener(listener); | ||
| Payload payload = new Payload("{}"); | ||
|
|
||
| defaultSender.send(payload); | ||
|
|
||
| ArgumentCaptor<SenderException> error = ArgumentCaptor.forClass(SenderException.class); | ||
| verify(listener).onError(eq(payload), error.capture()); | ||
| assertThat(error.getValue().getCause(), is(instanceOf(SocketTimeoutException.class))); | ||
| } finally { | ||
| config.sender().close(false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test(expected = IllegalArgumentException.class) | ||
| public void shouldRejectNegativeConnectTimeout() { | ||
| ConfigBuilder.withAccessToken(ACCESS_TOKEN).connectTimeoutMillis(-1); | ||
| } | ||
|
|
||
| @Test(expected = IllegalArgumentException.class) | ||
| public void shouldRejectNegativeReadTimeout() { | ||
| ConfigBuilder.withAccessToken(ACCESS_TOKEN).readTimeoutMillis(-1); | ||
| } | ||
| } | ||
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.
When either timeout setter is used through
Rollbar.configure, which starts withConfigBuilder.withConfig, the constructor has already copied the current sender intothis.sender. Consequently,build()skips itssender == nullbranch, so the returnedConfigreports the new timeout while the reusedSyncSendercontinues using the old value. This makes runtime timeout reconfiguration ineffective; the builder needs to rebuild or otherwise update an inherited default sender when these values change.Useful? React with 👍 / 👎.