From fb9d34ead7ba4714fc0215511e71cff7fdf496e9 Mon Sep 17 00:00:00 2001 From: krizsan Date: Thu, 28 Dec 2017 10:44:57 +0100 Subject: [PATCH] INT-4373: Fix channel error count JIRA: https://jira.spring.io/browse/INT-4373 Error count is wrong when just counts enabled. CheckStyle fix. Reduce send timeout in test. --- .../DefaultMessageChannelMetrics.java | 13 ++-- .../DefaultMessageChannelMetricsTests.java | 78 +++++++++++++++++++ 2 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/support/management/DefaultMessageChannelMetricsTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/management/DefaultMessageChannelMetrics.java b/spring-integration-core/src/main/java/org/springframework/integration/support/management/DefaultMessageChannelMetrics.java index 4c01b8e65a..8c4a75d8b2 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/support/management/DefaultMessageChannelMetrics.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/management/DefaultMessageChannelMetrics.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2017 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 @@ -21,6 +21,7 @@ import java.util.concurrent.atomic.AtomicLong; * @author Dave Syer * @author Helena Edelson * @author Gary Russell + * @author Ivan Krizsan * @since 2.0 */ public class DefaultMessageChannelMetrics extends AbstractMessageChannelMetrics { @@ -106,10 +107,12 @@ public class DefaultMessageChannelMetrics extends AbstractMessageChannelMetrics @Override public void afterSend(MetricsContext context, boolean result) { - if (result && isFullStatsEnabled()) { - long now = System.nanoTime(); - this.sendSuccessRatio.success(now); - this.sendDuration.append(now - ((DefaultChannelMetricsContext) context).start); + if (result) { + if (isFullStatsEnabled()) { + long now = System.nanoTime(); + this.sendSuccessRatio.success(now); + this.sendDuration.append(now - ((DefaultChannelMetricsContext) context).start); + } } else { if (isFullStatsEnabled()) { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/support/management/DefaultMessageChannelMetricsTests.java b/spring-integration-core/src/test/java/org/springframework/integration/support/management/DefaultMessageChannelMetricsTests.java new file mode 100644 index 0000000000..ba380c0769 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/support/management/DefaultMessageChannelMetricsTests.java @@ -0,0 +1,78 @@ +/* + * Copyright 2017 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on 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 org.junit.Assert; +import org.junit.Test; + +import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.messaging.Message; + +/** + * @author Ivan Krizsan + */ +public class DefaultMessageChannelMetricsTests { + + protected final static int MESSAGE_COUNT = 10; + + protected final static long SEND_TIMEOUT = 1; + + @Test + public void errorCountWithCountsEnabledOnlySuccessTest() { + final QueueChannel theMessageChannel = new QueueChannel(); + theMessageChannel.setCountsEnabled(true); + + + for (int i = 0; i < MESSAGE_COUNT; i++) { + Message theInputMessage = + MessageBuilder.withPayload(Integer.toString(i)).build(); + theMessageChannel.send(theInputMessage, SEND_TIMEOUT); + } + + Assert.assertEquals( + "Message count should match number of sent messages", + MESSAGE_COUNT, + theMessageChannel.getSendCount()); + Assert.assertEquals( + "Error count should indicate no errors", + 0, + theMessageChannel.getSendErrorCount()); + } + + @Test + public void errorCountWithCountsEnabledHalfErrorsTest() { + Message theInputMessage; + final QueueChannel theMessageChannel = new QueueChannel(MESSAGE_COUNT / 2); + theMessageChannel.setCountsEnabled(true); + + for (int i = 0; i < MESSAGE_COUNT; i++) { + theInputMessage = MessageBuilder.withPayload(Integer.toString(i)).build(); + theMessageChannel.send(theInputMessage, SEND_TIMEOUT); + } + + Assert.assertEquals( + "Message count should match number of sent messages", + MESSAGE_COUNT, + theMessageChannel.getSendCount()); + Assert.assertEquals( + "Error count should indicate errors half the messages", + MESSAGE_COUNT / 2, + theMessageChannel.getSendErrorCount()); + } + +}