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 * 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 * the License. You may obtain a copy of the License at
@@ -37,6 +37,7 @@ import java.util.List;
* sum. * sum.
* @author Dave Syer * @author Dave Syer
* @author Gary Russell * @author Gary Russell
* @author Steven Swor
* *
*/ */
public class ExponentialMovingAverageRate { public class ExponentialMovingAverageRate {
@@ -205,7 +206,7 @@ public class ExponentialMovingAverageRate {
private synchronized double lastTime() { private synchronized double lastTime() {
if (this.times.size() > 0) { if (this.times.size() > 0) {
return this.times.peekFirst() / this.factor; return this.times.peekLast() / this.factor;
} }
else { else {
return this.t0; 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 * 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 * the License. You may obtain a copy of the License at
@@ -37,6 +37,7 @@ import java.util.List;
* sum. * sum.
* @author Dave Syer * @author Dave Syer
* @author Gary Russell * @author Gary Russell
* @author Steven Swor
* @since 2.0 * @since 2.0
*/ */
public class ExponentialMovingAverageRatio { public class ExponentialMovingAverageRatio {
@@ -224,7 +225,7 @@ public class ExponentialMovingAverageRatio {
private synchronized double lastTime() { private synchronized double lastTime() {
if (this.times.size() > 0) { if (this.times.size() > 0) {
return this.times.peekFirst(); return this.times.peekLast();
} }
else { else {
return this.t0 * this.factor; 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 * 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 * 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 * 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. * specific language governing permissions and limitations under the License.
*/ */
package org.springframework.integration.support.management; package org.springframework.integration.support.management;
import static org.hamcrest.Matchers.lessThan; import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.Deque;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
/** /**
* @author Dave Syer * @author Dave Syer
* @author Gary Russell * @author Gary Russell
* * @author Steven Swor
*/ */
public class ExponentialMovingAverageRateTests { public class ExponentialMovingAverageRateTests {
@@ -44,10 +49,33 @@ public class ExponentialMovingAverageRateTests {
} }
@Test @Test
@SuppressWarnings("unchecked")
public void testGetTimeSinceLastMeasurement() throws Exception { public void testGetTimeSinceLastMeasurement() throws Exception {
history.increment(); long sleepTime = 20L;
Thread.sleep(20L);
assertTrue(history.getTimeSinceLastMeasurement() > 0); // 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 @Test
@@ -121,7 +149,8 @@ public class ExponentialMovingAverageRateTests {
assertEquals(0, history.getMax(), 0.01); 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() { public void testRate() {
ExponentialMovingAverageRate rate = new ExponentialMovingAverageRate(1, 60, 10); ExponentialMovingAverageRate rate = new ExponentialMovingAverageRate(1, 60, 10);
int count = 1000000; int count = 1000000;
@@ -135,7 +164,8 @@ public class ExponentialMovingAverageRateTests {
assertEquals(calculatedRate, rate.getMean(), 4000000); assertEquals(calculatedRate, rate.getMean(), 4000000);
} }
@Test @Ignore @Test
@Ignore
public void testPerf() { public void testPerf() {
ExponentialMovingAverageRate rate = new ExponentialMovingAverageRate(1, 60, 10); ExponentialMovingAverageRate rate = new ExponentialMovingAverageRate(1, 60, 10);
for (int i = 0; i < 1000000; i++) { 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.equalTo;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.util.Deque;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.integration.test.util.TestUtils;
/** /**
* @author Dave Syer * @author Dave Syer
* @author Gary Russell * @author Gary Russell
* @author Artem Bilan * @author Artem Bilan
* * @author Steven Swor
*/ */
public class ExponentialMovingAverageRatioTests { public class ExponentialMovingAverageRatioTests {
@@ -43,9 +48,32 @@ public class ExponentialMovingAverageRatioTests {
} }
@Test @Test
@SuppressWarnings("unchecked")
public void testGetTimeSinceLastMeasurement() throws Exception { public void testGetTimeSinceLastMeasurement() throws Exception {
history.success(System.nanoTime() - 20000000); long sleepTime = 20L;
assertThat(history.getTimeSinceLastMeasurement(), Matchers.greaterThan(0.)); // 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 @Test
@@ -152,7 +180,8 @@ public class ExponentialMovingAverageRatioTests {
assertEquals(0.9, ratio.getMean(), 0.03); assertEquals(0.9, ratio.getMean(), 0.03);
} }
@Test @Ignore @Test
@Ignore
public void testPerf() { public void testPerf() {
ExponentialMovingAverageRatio ratio = new ExponentialMovingAverageRatio(60, 10); ExponentialMovingAverageRatio ratio = new ExponentialMovingAverageRatio(60, 10);
for (int i = 0; i < 100000; i++) { for (int i = 0; i < 100000; i++) {