use of java.util.concurrent.TimeUnit.MILLISECONDS in project neo4j by neo4j.
the class TicketedProcessingTest method shouldReturnTicketsInOrder.
@Test
public void shouldReturnTicketsInOrder() throws Exception {
// GIVEN
int items = 1_000;
ParkStrategy park = new ParkStrategy.Park(2, MILLISECONDS);
BiFunction<Integer, Void, Integer> processor = (from, ignore) -> {
if (ThreadLocalRandom.current().nextFloat() < 0.01f) {
park.park(Thread.currentThread());
}
return from * 2;
};
int processorCount = Runtime.getRuntime().availableProcessors();
Future<Void> assertions;
try (TicketedProcessing<Integer, Void, Integer> processing = new TicketedProcessing<>("Doubler", processorCount, processor, () -> null)) {
processing.processors(processorCount - processing.processors(0));
// WHEN
assertions = t2.execute(new WorkerCommand<Void, Void>() {
@Override
public Void doWork(Void state) throws Exception {
for (int i = 0; i < items; i++) {
Integer next = processing.next();
assertNotNull(next);
assertEquals(i * 2, next.intValue());
}
assertNull(processing.next());
return null;
}
});
for (int i = 0; i < items; i++) {
processing.submit(i);
}
}
// THEN
assertions.get();
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project streamsupport by stefan-zobel.
the class SubmissionPublisherTest method testHandledDroppedTimedOffer.
/**
* Timed offer invokes drop handler if saturated
*/
public void testHandledDroppedTimedOffer() {
AtomicInteger calls = new AtomicInteger();
SubmissionPublisher<Integer> p = new SubmissionPublisher<>(basicExecutor, 4);
TestSubscriber s1 = new TestSubscriber();
s1.request = false;
TestSubscriber s2 = new TestSubscriber();
s2.request = false;
p.subscribe(s1);
p.subscribe(s2);
s2.awaitSubscribe();
s1.awaitSubscribe();
long delay = timeoutMillis();
for (int i = 1; i <= 4; ++i) assertTrue(p.offer(i, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) >= 0);
long startTime = System.nanoTime();
assertTrue(p.offer(5, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
s1.sn.request(64);
assertTrue(p.offer(6, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
assertTrue(millisElapsedSince(startTime) >= delay);
s2.sn.request(64);
p.close();
s2.awaitComplete();
s1.awaitComplete();
assertTrue(calls.get() >= 2);
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project tutorials by eugenp.
the class MetricTypeTest method givenStatsTimer_whenExecuteTask_thenStatsCalculated.
@Test
public // ==
void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Exception {
System.setProperty("netflix.servo", "1000");
StatsTimer timer = new StatsTimer(MonitorConfig.builder("test").build(), new StatsConfig.Builder().withComputeFrequencyMillis(2000).withPercentiles(new double[] { 99.0, 95.0, 90.0 }).withPublishMax(true).withPublishMin(true).withPublishCount(true).withPublishMean(true).withPublishStdDev(true).withPublishVariance(true).build(), MILLISECONDS);
Stopwatch stopwatch = timer.start();
MILLISECONDS.sleep(1);
timer.record(3, MILLISECONDS);
stopwatch.stop();
stopwatch = timer.start();
timer.record(6, MILLISECONDS);
MILLISECONDS.sleep(2);
stopwatch.stop();
assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalTime());
assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalMeasurement());
assertEquals("timer should record 4 updates", 4, timer.getCount());
assertEquals("stats timer value time-cost/update should be 2", 3, timer.getValue().intValue());
final Map<String, Number> metricMap = timer.getMonitors().stream().collect(toMap(monitor -> getMonitorTagValue(monitor, "statistic"), monitor -> (Number) monitor.getValue()));
assertThat(metricMap.keySet(), containsInAnyOrder("count", "totalTime", "max", "min", "variance", "stdDev", "avg", "percentile_99", "percentile_95", "percentile_90"));
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project mule by mulesoft.
the class DefaultExceptionStrategyTestCase method testExceptionNotifications.
// MULE-1627
@Test
public void testExceptionNotifications() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger notificationCount = new AtomicInteger(0);
((MuleContextWithRegistries) muleContext).getRegistry().lookupObject(NotificationListenerRegistry.class).registerListener((ExceptionNotificationListener) notification -> {
if (new IntegerAction(EXCEPTION_ACTION).equals(notification.getAction())) {
assertEquals("exception", notification.getActionName());
assertEquals("Wrong info type", TYPE_ERROR, notification.getType());
notificationCount.incrementAndGet();
latch.countDown();
}
});
// throwing exception
InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy(muleContext);
strategy.setAnnotations(singletonMap(LOCATION_KEY, TEST_CONNECTOR_LOCATION));
strategy.setMuleContext(muleContext);
strategy.setNotificationFirer(((MuleContextWithRegistries) muleContext).getRegistry().lookupObject(NotificationDispatcher.class));
strategy.handleException(new IllegalArgumentException("boom"));
// Wait for the notifcation event to be fired as they are queue
latch.await(2000, MILLISECONDS);
assertEquals(1, notificationCount.get());
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project mule by mulesoft.
the class ExpiryMonitorTestCase method testExpiryWithReset.
@Test
public void testExpiryWithReset() throws InterruptedException {
Expirable e = () -> expire();
monitor.addExpirable(EXPIRE_TIME, MILLISECONDS, e);
monitor.run();
assertThat(expired, is(false));
long startTime = currentTimeMillis();
monitor.resetExpirable(e);
monitor.run();
assertTrue(!expired);
new PollingProber(EXPIRE_TIMEOUT, 50).check(new JUnitLambdaProbe(() -> {
assertThat(monitor.isRegistered(e), is(false));
assertThat(expired, is(true));
return true;
}, ae -> {
ae.printStackTrace();
return "" + currentTimeMillis() + " - " + monitor.toString();
}));
assertThat(expiredTime - startTime, greaterThanOrEqualTo(EXPIRE_TIME - DELTA_TIME));
}
Aggregations