Skip to content
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

W-11933460: Fix flaky test: JvmThreadSnapshotCollectorTestCase.thread… #13209

Open
wants to merge 1 commit into
base: support/4.4.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,68 @@
*/
package org.mule.runtime.core.internal.profiling.threading;

import static org.mule.test.allure.AllureConstants.Profiling.PROFILING;
import static org.mule.test.allure.AllureConstants.Profiling.ProfilingServiceStory.DEFAULT_PROFILING_SERVICE;

import static java.lang.Thread.sleep;
import static java.util.concurrent.Executors.newSingleThreadExecutor;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mule.test.allure.AllureConstants.Profiling.PROFILING;
import static org.mule.test.allure.AllureConstants.Profiling.ProfilingServiceStory.DEFAULT_PROFILING_SERVICE;

import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import org.mule.runtime.api.profiling.threading.ThreadSnapshot;
import org.mule.runtime.api.profiling.threading.ThreadSnapshotCollector;
import org.mule.runtime.api.util.concurrent.Latch;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

@Feature(PROFILING)
@Story(DEFAULT_PROFILING_SERVICE)
public class JvmThreadSnapshotCollectorTestCase {

private static final ExecutorService executor = newSingleThreadExecutor();
private ExecutorService executor;
private static final long TEST_TIME = 10L;

private final Object theSharedLock = new Object();
private final ThreadSnapshotCollector threadSnapshotCollector = new JvmThreadSnapshotCollector();

@Before
public void setUp() {
executor = newSingleThreadExecutor();
}

@After
public void tearDown() {
executor.shutdown();
}

@Test
public void threadIsWaitingInSleep() throws InterruptedException {
Long previousWaitedTime = threadSnapshotCollector.getCurrentThreadSnapshot().getWaitedTime();
sleep(TEST_TIME);
ThreadSnapshot snapshot = threadSnapshotCollector.getCurrentThreadSnapshot();
assertThat(snapshot.getWaitedTime(), is(greaterThanOrEqualTo(TEST_TIME)));
assertThat(threadSnapshotCollector.getCurrentThreadSnapshot().getWaitedTime(),
is(greaterThanOrEqualTo(TEST_TIME + previousWaitedTime)));
}

@Test
public void threadWaitedButItDidNotWasteThatTimeTryingToLock() throws InterruptedException {
Long previousBlockedTime = threadSnapshotCollector.getCurrentThreadSnapshot().getBlockedTime();
synchronized (theSharedLock) {
sleep(TEST_TIME);
}
ThreadSnapshot snapshot = threadSnapshotCollector.getCurrentThreadSnapshot();
assertThat(snapshot.getBlockedTime(), is(lessThan(TEST_TIME)));
assertThat(threadSnapshotCollector.getCurrentThreadSnapshot().getBlockedTime(),
is(lessThan(TEST_TIME + previousBlockedTime)));
}

@Test
Expand Down Expand Up @@ -88,10 +101,10 @@ public void threadIsBlockedWhileTryingToAcquireALockThatIsOwnedByAnotherThread()

@Test
public void cpuTime() {
Long previousCpuTime = threadSnapshotCollector.getCurrentThreadSnapshot().getCpuTime();
for (long l = 0L; l < 100L; ++l) {
// Just the loop to make the cpu work for some time...
}
ThreadSnapshot snapshot = threadSnapshotCollector.getCurrentThreadSnapshot();
assertThat(snapshot.getCpuTime(), is(not(0L)));
assertThat(threadSnapshotCollector.getCurrentThreadSnapshot().getCpuTime(), is(greaterThan(previousCpuTime)));
}
}