Skip to content

Commit

Permalink
Merge pull request #742 from qiangdavidliu/master
Browse files Browse the repository at this point in the history
1.x Fix async resolver to use daemon threads
  • Loading branch information
qiangdavidliu committed Feb 7, 2016
2 parents 21e3439 + 605ea9a commit b837ce3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ public AsyncResolver(String name,

this.threadPoolExecutor = new ThreadPoolExecutor(
1, executorThreadPoolSize, 0, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>()); // use direct handoff
new SynchronousQueue<Runnable>(), // use direct handoff
new ThreadFactoryBuilder()
.setNameFormat("AsyncResolver-executor-%d")
.setDaemon(true)
.build()
);

this.backgroundTask = new TimedSupervisorTask(
this.getClass().getSimpleName(),
Expand Down Expand Up @@ -178,7 +183,7 @@ public List<T> getClusterEndpoints() {
return false;
}

private void scheduleTask(long delay) {
/* visible for testing */ void scheduleTask(long delay) {
executorService.schedule(
backgroundTask, delay, TimeUnit.MILLISECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
Expand Down Expand Up @@ -82,6 +84,10 @@ public void testDelegateFailureAtWarmUp() {
when(delegateResolver.getClusterEndpoints())
.thenReturn(null);

// override the scheduling which will be triggered immediately if warmUp fails (as is intended).
// do this to avoid thread race conditions for a more predictable test
doNothing().when(resolver).scheduleTask(anyLong());

List endpoints = resolver.getClusterEndpoints();
assertThat(endpoints.isEmpty(), is(true));
verify(delegateResolver, times(1)).getClusterEndpoints();
Expand Down

0 comments on commit b837ce3

Please sign in to comment.