Skip to content

Commit

Permalink
Fix test issues
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Aug 29, 2022
1 parent f0754eb commit f1a3963
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,7 +17,6 @@
package org.glassfish.jersey.jdk.connector.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -82,6 +81,6 @@ public void randomnessStatus200() throws InterruptedException, ExecutionExceptio
}
}
executor.shutdown();
assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));
executor.awaitTermination(10, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,13 +16,17 @@

package org.glassfish.jersey.tests.integration.jersey4697;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.management.JMX;
Expand All @@ -48,11 +52,12 @@
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MonitoringEventListenerTest extends JerseyTest {

private static final long TIMEOUT = 500;
private static final long TIMEOUT = 10000;
private static final String MBEAN_EXCEPTION =
"org.glassfish.jersey:type=MonitoringEventListenerTest,subType=Global,exceptions=ExceptionMapper";

Expand Down Expand Up @@ -126,17 +131,14 @@ public void exceptionInScheduler() throws Exception {
final Long ERRORS_BEFORE_FAIL = 10L;
// Send some requests to process some statistics.
request(ERRORS_BEFORE_FAIL);
// Give some time to the scheduler to collect data.
Thread.sleep(TIMEOUT);
// All events were consumed by scheduler
queueIsEmpty();
queueIsEmpty(TIMEOUT);
// Make the scheduler to fail. No more statistics are collected.
makeFailure();
// Sending again requests
request(20);
Thread.sleep(TIMEOUT);
// No new events should be accepted because scheduler is not working.
queueIsEmpty();
queueIsEmpty(TIMEOUT);
Long monitoredErrors = mappedErrorsFromJMX(MBEAN_EXCEPTION);
assertEquals(ERRORS_BEFORE_FAIL, monitoredErrors);
}
Expand All @@ -146,10 +148,25 @@ private void makeFailure() {
assertEquals(200, response.getStatus());
}

private void queueIsEmpty() {
Response response = target("/example/queueSize").request().get();
assertEquals(200, response.getStatus());
assertEquals(Integer.valueOf(0), response.readEntity(Integer.class));
private void queueIsEmpty(long timeout) {
final long MAX_TIMEOUT = System.currentTimeMillis() + timeout;
int queueSize = Integer.MAX_VALUE;
while (queueSize > 0) {
if (System.currentTimeMillis() > MAX_TIMEOUT) {
fail("Queue size is " + queueSize + ". It is expected to be 0. Check next thread"
+ " dumps to see what is the scheduler doing:" + threadsDump());
}
Response response = target("/example/queueSize").request().get();
assertEquals(200, response.getStatus());
queueSize = response.readEntity(Integer.class);
}
}

private String threadsDump() {
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
ThreadInfo[] infos = bean.dumpAllThreads(true, true);
return Arrays.stream(infos).map(Object::toString)
.collect(Collectors.joining());
}

private Long mappedErrorsFromJMX(String name) throws Exception {
Expand Down

0 comments on commit f1a3963

Please sign in to comment.