INT-3956: peekLast for timeSinceLastSend

JIRA: https://jira.spring.io/browse/INT-3956

Use reflection in tests to access private members

fix assertion imports

Polishing author rights.
Make tests `Threas.sleep()` free.
This commit is contained in:
Steve Swor
2016-02-17 19:07:02 +11:00
committed by Artem Bilan
parent 1f4dda011b
commit bfb31a1361
4 changed files with 78 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2009-2015 the original author or authors.
* Copyright 2009-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@@ -37,6 +37,7 @@ import java.util.List;
* sum.
* @author Dave Syer
* @author Gary Russell
* @author Steven Swor
*
*/
public class ExponentialMovingAverageRate {
@@ -205,7 +206,7 @@ public class ExponentialMovingAverageRate {
private synchronized double lastTime() {
if (this.times.size() > 0) {
return this.times.peekFirst() / this.factor;
return this.times.peekLast() / this.factor;
}
else {
return this.t0;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2009-2015 the original author or authors.
* Copyright 2009-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@@ -37,6 +37,7 @@ import java.util.List;
* sum.
* @author Dave Syer
* @author Gary Russell
* @author Steven Swor
* @since 2.0
*/
public class ExponentialMovingAverageRatio {
@@ -224,7 +225,7 @@ public class ExponentialMovingAverageRatio {
private synchronized double lastTime() {
if (this.times.size() > 0) {
return this.times.peekFirst();
return this.times.peekLast();
}
else {
return this.t0 * this.factor;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@@ -10,25 +10,30 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.support.management;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.Deque;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.util.StopWatch;
/**
* @author Dave Syer
* @author Gary Russell
*
* @author Steven Swor
*/
public class ExponentialMovingAverageRateTests {
@@ -44,10 +49,33 @@ public class ExponentialMovingAverageRateTests {
}
@Test
@SuppressWarnings("unchecked")
public void testGetTimeSinceLastMeasurement() throws Exception {
history.increment();
Thread.sleep(20L);
assertTrue(history.getTimeSinceLastMeasurement() > 0);
long sleepTime = 20L;
// fill history with the same value.
long now = System.nanoTime() - 2 * sleepTime * 1000000;
for (int i = 0; i < TestUtils.getPropertyValue(history, "retention", Integer.class); i++) {
history.increment(now);
}
final Deque<Long> times = TestUtils.getPropertyValue(history, "times", Deque.class);
assertEquals(Long.valueOf(now), times.peekFirst());
assertEquals(Long.valueOf(now), times.peekLast());
//increment just so we'll have a different value between first and last
history.increment(System.nanoTime() - sleepTime * 1000000);
assertNotEquals(times.peekFirst(), times.peekLast());
/*
* We've called Thread.sleep twice with the same value in quick
* succession. If timeSinceLastSend is pulling off the correct end of
* the queue, then we should be closer to the sleep time than we are to
* 2 x sleepTime, but we should definitely be greater than the sleep
* time.
*/
double timeSinceLastMeasurement = history.getTimeSinceLastMeasurement();
assertTrue(timeSinceLastMeasurement > sleepTime);
assertTrue(timeSinceLastMeasurement <= (1.5 * sleepTime));
}
@Test
@@ -111,7 +139,7 @@ public class ExponentialMovingAverageRateTests {
history.increment();
Thread.sleep(30L);
history.increment();
assertFalse(0==history.getStandardDeviation());
assertFalse(0 == history.getStandardDeviation());
history.reset();
assertEquals(0, history.getStandardDeviation(), 0.01);
assertEquals(0, history.getCount());
@@ -121,7 +149,8 @@ public class ExponentialMovingAverageRateTests {
assertEquals(0, history.getMax(), 0.01);
}
@Test @Ignore // tolerance needed is too dependent on hardware
@Test
@Ignore // tolerance needed is too dependent on hardware
public void testRate() {
ExponentialMovingAverageRate rate = new ExponentialMovingAverageRate(1, 60, 10);
int count = 1000000;
@@ -135,7 +164,8 @@ public class ExponentialMovingAverageRateTests {
assertEquals(calculatedRate, rate.getMean(), 4000000);
}
@Test @Ignore
@Test
@Ignore
public void testPerf() {
ExponentialMovingAverageRate rate = new ExponentialMovingAverageRate(1, 60, 10);
for (int i = 0; i < 1000000; i++) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,17 +18,22 @@ package org.springframework.integration.support.management;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
import java.util.Deque;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.integration.test.util.TestUtils;
/**
* @author Dave Syer
* @author Gary Russell
* @author Artem Bilan
*
* @author Steven Swor
*/
public class ExponentialMovingAverageRatioTests {
@@ -43,9 +48,32 @@ public class ExponentialMovingAverageRatioTests {
}
@Test
@SuppressWarnings("unchecked")
public void testGetTimeSinceLastMeasurement() throws Exception {
history.success(System.nanoTime() - 20000000);
assertThat(history.getTimeSinceLastMeasurement(), Matchers.greaterThan(0.));
long sleepTime = 20L;
// fill history with the same value.
long now = System.nanoTime() - 2 * sleepTime * 1000000;
for (int i = 0; i < TestUtils.getPropertyValue(history, "retention", Integer.class); i++) {
history.success(now);
}
final Deque<Long> times = TestUtils.getPropertyValue(history, "times", Deque.class);
assertEquals(Long.valueOf(now), times.peekFirst());
assertEquals(Long.valueOf(now), times.peekLast());
//increment just so we'll have a different value between first and last
history.success(System.nanoTime() - sleepTime * 1000000);
assertNotEquals(times.peekFirst(), times.peekLast());
/*
* We've called Thread.sleep twice with the same value in quick
* succession. If timeSinceLastSend is pulling off the correct end of
* the queue, then we should be closer to the sleep time than we are to
* 2 x sleepTime, but we should definitely be greater than the sleep
* time.
*/
double timeSinceLastMeasurement = history.getTimeSinceLastMeasurement();
assertThat(timeSinceLastMeasurement, Matchers.greaterThan((double) (sleepTime / 100)));
assertThat(timeSinceLastMeasurement, Matchers.lessThanOrEqualTo(1.5 * sleepTime / 100));
}
@Test
@@ -152,7 +180,8 @@ public class ExponentialMovingAverageRatioTests {
assertEquals(0.9, ratio.getMean(), 0.03);
}
@Test @Ignore
@Test
@Ignore
public void testPerf() {
ExponentialMovingAverageRatio ratio = new ExponentialMovingAverageRatio(60, 10);
for (int i = 0; i < 100000; i++) {