Skip to content

Commit

Permalink
Have TargetPoller poll without initial delay and add test for TargetP…
Browse files Browse the repository at this point in the history
…oller.
  • Loading branch information
Anuraag Agrawal committed May 16, 2020
1 parent f14a788 commit efd8447
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

public class TargetPoller {
private static final Log logger = LogFactory.getLog(TargetPoller.class);
private static final long PERIOD = 10; // Seconds
private static final long MAX_JITTER = 100; // Milliseconds
private static final long PERIOD_MILLIS = TimeUnit.SECONDS.toMillis(10);
private static final long MAX_JITTER_MILLIS = 100;

private final UnsignedXrayClient client;
private final CentralizedManifest manifest;
Expand Down Expand Up @@ -53,7 +53,7 @@ public void start() {
// The executor will die and not abrupt main thread.
if(t instanceof Error) { throw t; }
}
}, PERIOD * 1000, getIntervalWithJitter(), TimeUnit.MILLISECONDS);
}, 0, getIntervalWithJitter(), TimeUnit.MILLISECONDS);
}

public void shutdown() {
Expand Down Expand Up @@ -82,6 +82,6 @@ private void pollManifest() {

private long getIntervalWithJitter() {
Rand random = new RandImpl();
return Math.round(random.next() * MAX_JITTER) + PERIOD * 1000;
return Math.round(random.next() * MAX_JITTER_MILLIS) + PERIOD_MILLIS;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.amazonaws.xray.strategy.sampling.pollers;

import com.amazonaws.xray.internal.UnsignedXrayClient;
import com.amazonaws.xray.strategy.sampling.manifest.CentralizedManifest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.time.Clock;
import java.util.concurrent.ScheduledExecutorService;

import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import com.amazonaws.xray.internal.UnsignedXrayClient;
import com.amazonaws.xray.strategy.sampling.manifest.CentralizedManifest;

public class RulePollerTest {

Expand All @@ -29,7 +31,7 @@ public void testPollerShutdown() {
poller.shutdown();

ScheduledExecutorService executor = poller.getExecutor();
assertTrue(executor.isShutdown());
assertThat(executor.isShutdown()).isTrue();
}

@Test
Expand All @@ -39,6 +41,6 @@ public void testPollerErrorNotAbruptMainThread() throws InterruptedException {
poller.start();
//Give the mocked client time (1s) to throw the pre-defined Error
Thread.sleep(1000L);
assertTrue(Thread.currentThread().isAlive());
assertThat(Thread.currentThread().isAlive()).isTrue();
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
package com.amazonaws.xray.strategy.sampling.pollers;

import com.amazonaws.xray.internal.UnsignedXrayClient;
import com.amazonaws.xray.strategy.sampling.manifest.CentralizedManifest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.time.Clock;
import java.util.Collections;

import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import static org.junit.Assert.assertTrue;
import com.amazonaws.services.xray.model.SamplingStatisticsDocument;
import com.amazonaws.xray.internal.UnsignedXrayClient;
import com.amazonaws.xray.strategy.sampling.manifest.CentralizedManifest;

public class TargetPollerTest {

@Rule
public MockitoRule mocks = MockitoJUnit.rule();

@Mock
private CentralizedManifest manifest;

@Mock
private UnsignedXrayClient client;

@Test
public void testPollerShutdown() {
TargetPoller poller = new TargetPoller(
client, new CentralizedManifest(), Clock.systemUTC());
TargetPoller poller = new TargetPoller(client, manifest, Clock.systemUTC());
poller.start();
poller.shutdown();

assertThat(poller.getExecutor().isShutdown()).isTrue();
}

@Test
public void testPollerErrorNotAbruptMainThread() throws InterruptedException {
when(client.getSamplingTargets(any())).thenThrow(new OutOfMemoryError());
when(manifest.snapshots(any())).thenReturn(Collections.singletonList(new SamplingStatisticsDocument()));

assertTrue(poller.getExecutor().isShutdown());
TargetPoller poller = new TargetPoller(client, manifest, Clock.systemUTC());
poller.start();
//Give the mocked client time (1s) to throw the pre-defined Error
Thread.sleep(1000L);
assertThat(Thread.currentThread().isAlive()).isTrue();
}
}

0 comments on commit efd8447

Please sign in to comment.