Skip to content

Commit

Permalink
GEODE-8634: Fix AsyncInvocationTimeoutDistributedTest flakiness (apac…
Browse files Browse the repository at this point in the history
…he#5799)

Reset latch and threadId before each test.
  • Loading branch information
kirklund authored Dec 5, 2020
1 parent d38189e commit 2e7cb33
Showing 1 changed file with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.atomic.AtomicReference;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

Expand All @@ -38,35 +39,37 @@
@SuppressWarnings("serial")
public class AsyncInvocationTimeoutDistributedTest implements Serializable {

private static final long TIMEOUT_MILLIS = getTimeout().toMillis();

private static final AtomicReference<Long> threadId = new AtomicReference<>();
private static final AtomicReference<CountDownLatch> latch = new AtomicReference<>();
private static final AtomicReference<Long> THREAD_ID =
new AtomicReference<>(0L);
private static final AtomicReference<CountDownLatch> LATCH =
new AtomicReference<>(new CountDownLatch(0));

@Rule
public DistributedRule distributedRule = new DistributedRule();
public DistributedRule distributedRule = new DistributedRule(1);

@After
public void tearDown() {
@Before
public void setUp() {
getVM(0).invoke(() -> {
CountDownLatch latchInVM0 = latch.get();
while (latchInVM0 != null && latchInVM0.getCount() > 0) {
latchInVM0.countDown();
}
LATCH.set(new CountDownLatch(1));
THREAD_ID.set(0L);
});
}

@After
public void tearDown() {
getVM(0).invoke(() -> LATCH.get().countDown());
}

@Test
public void await_runnable_timeout_includesStackTraceAsCause() {
public void awaitWithRunnableTimeoutExceptionIncludesRemoteStackTraceAsCause() {
AsyncInvocation<Void> hangInVM0 = getVM(0).invokeAsync(() -> {
latch.set(new CountDownLatch(1));
threadId.set(Thread.currentThread().getId());
latch.get().await(TIMEOUT_MILLIS, MILLISECONDS);
THREAD_ID.set(Thread.currentThread().getId());
LATCH.get().await(getTimeout().toMillis(), MILLISECONDS);
});

long remoteThreadId = getVM(0).invoke(() -> {
await().until(() -> threadId.get() > 0);
return threadId.get();
await().until(() -> THREAD_ID.get() > 0);
return THREAD_ID.get();
});

Throwable thrown = catchThrowable(() -> hangInVM0.await(1, SECONDS));
Expand All @@ -79,17 +82,16 @@ public void await_runnable_timeout_includesStackTraceAsCause() {
}

@Test
public void await_callable_timeout_includesStackTraceAsCause() {
public void awaitWithCallableTimeoutExceptionIncludesRemoteStackTraceAsCause() {
AsyncInvocation<Integer> hangInVM0 = getVM(0).invokeAsync(() -> {
latch.set(new CountDownLatch(1));
threadId.set(Thread.currentThread().getId());
latch.get().await(TIMEOUT_MILLIS, MILLISECONDS);
THREAD_ID.set(Thread.currentThread().getId());
LATCH.get().await(getTimeout().toMillis(), MILLISECONDS);
return 42;
});

long remoteThreadId = getVM(0).invoke(() -> {
await().until(() -> threadId.get() > 0);
return threadId.get();
await().until(() -> THREAD_ID.get() > 0);
return THREAD_ID.get();
});

Throwable thrown = catchThrowable(() -> hangInVM0.await(1, SECONDS));
Expand All @@ -102,17 +104,16 @@ public void await_callable_timeout_includesStackTraceAsCause() {
}

@Test
public void get_callable_timeout_includesStackTraceAsCause() {
public void getWithCallableTimeoutExceptionIncludesRemoteStackTraceAsCause() {
AsyncInvocation<Integer> hangInVM0 = getVM(0).invokeAsync(() -> {
latch.set(new CountDownLatch(1));
threadId.set(Thread.currentThread().getId());
latch.get().await(TIMEOUT_MILLIS, MILLISECONDS);
THREAD_ID.set(Thread.currentThread().getId());
LATCH.get().await(getTimeout().toMillis(), MILLISECONDS);
return 42;
});

long remoteThreadId = getVM(0).invoke(() -> {
await().until(() -> threadId.get() > 0);
return threadId.get();
await().until(() -> THREAD_ID.get() > 0);
return THREAD_ID.get();
});

Throwable thrown = catchThrowable(() -> hangInVM0.get(1, SECONDS));
Expand Down

0 comments on commit 2e7cb33

Please sign in to comment.