Skip to content

Commit

Permalink
Separate executors
Browse files Browse the repository at this point in the history
  • Loading branch information
kevink-sq committed Sep 27, 2024
1 parent 95463bf commit 81ca1db
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 31 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/amplitude/HttpTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class HttpTransport {

// Managed by setters
private ExecutorService retryThreadPool = Executors.newFixedThreadPool(10);

// The supplyAsyncPool is only used within the sendThreadPool so only when
// the sendThreadPool is increased will the supplyAsyncPool be more utilized.
// We are using the supplyAsyncPool rather than the default fork join common
// pool because the fork join common pool scales with cpu... and we do not
// want to perform network requests in that small pool.
private ExecutorService sendThreadPool = Executors.newFixedThreadPool(20);
private ExecutorService supplyAsyncPool = Executors.newCachedThreadPool();

Expand Down
31 changes: 0 additions & 31 deletions src/test/java/com/amplitude/HttpTransportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -30,8 +24,6 @@

import com.amplitude.exception.AmplitudeInvalidAPIKeyException;
import com.amplitude.util.EventsGenerator;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

@ExtendWith(MockitoExtension.class)
public class HttpTransportTest {
Expand All @@ -43,29 +35,6 @@ public void setUp() {
httpTransport = new HttpTransport(null, null, new AmplitudeLog(), 0);
}

/**
* This test is to make sure the same thread pool is used in both sendEventsWithRetry -> sendEvents.
* If the thread pool is not piped to sendEvents, then the default ForkJoinPool is used which can
* become a performance bottleneck.
*/
@Test
@MockitoSettings(strictness = Strictness.LENIENT)
public void testSentEventsThreadpool() throws AmplitudeInvalidAPIKeyException, InterruptedException{
CountDownLatch latch = new CountDownLatch(2);
HttpCall httpCall = mock(HttpCall.class);
when(httpCall.makeRequest(anyList())).thenReturn(ResponseUtil.getSuccessResponse());
ExecutorService sendThreadPool = spy(Executors.newFixedThreadPool(2));
doAnswer((invocation) -> {
latch.countDown();
invocation.callRealMethod();
return null;
}).when(sendThreadPool).execute(any());
httpTransport.setSendThreadPool(sendThreadPool);
httpTransport.setHttpCall(httpCall);
httpTransport.sendEventsWithRetry(new ArrayList<>());
assertTrue(latch.await(2L, TimeUnit.SECONDS));
}

@ParameterizedTest
@CsvSource({
"SUCCESS, false",
Expand Down

0 comments on commit 81ca1db

Please sign in to comment.