Skip to content

Commit 2390ad3

Browse files
krizsangaryrussell
authored andcommitted
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.
1 parent dbccd0c commit 2390ad3

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

spring-integration-core/src/main/java/org/springframework/integration/support/management/DefaultMessageChannelMetrics.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
* @author Dave Syer
2525
* @author Helena Edelson
2626
* @author Gary Russell
27+
* @author Ivan Krizsan
2728
* @since 2.0
2829
*/
2930
public class DefaultMessageChannelMetrics extends AbstractMessageChannelMetrics {
@@ -109,10 +110,12 @@ public MetricsContext beforeSend() {
109110

110111
@Override
111112
public void afterSend(MetricsContext context, boolean result) {
112-
if (result && isFullStatsEnabled()) {
113-
long now = System.nanoTime();
114-
this.sendSuccessRatio.success(now);
115-
this.sendDuration.append(now - ((DefaultChannelMetricsContext) context).start);
113+
if (result) {
114+
if (isFullStatsEnabled()) {
115+
long now = System.nanoTime();
116+
this.sendSuccessRatio.success(now);
117+
this.sendDuration.append(now - ((DefaultChannelMetricsContext) context).start);
118+
}
116119
}
117120
else {
118121
if (isFullStatsEnabled()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.support.management;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
22+
import org.springframework.integration.channel.QueueChannel;
23+
import org.springframework.integration.support.MessageBuilder;
24+
import org.springframework.messaging.Message;
25+
26+
/**
27+
* @author Ivan Krizsan
28+
*/
29+
public class DefaultMessageChannelMetricsTests {
30+
31+
protected final static int MESSAGE_COUNT = 10;
32+
33+
protected final static long SEND_TIMEOUT = 1;
34+
35+
@Test
36+
public void errorCountWithCountsEnabledOnlySuccessTest() {
37+
final QueueChannel theMessageChannel = new QueueChannel();
38+
theMessageChannel.setCountsEnabled(true);
39+
40+
41+
for (int i = 0; i < MESSAGE_COUNT; i++) {
42+
Message<String> theInputMessage =
43+
MessageBuilder.withPayload(Integer.toString(i)).build();
44+
theMessageChannel.send(theInputMessage, SEND_TIMEOUT);
45+
}
46+
47+
Assert.assertEquals(
48+
"Message count should match number of sent messages",
49+
MESSAGE_COUNT,
50+
theMessageChannel.getSendCount());
51+
Assert.assertEquals(
52+
"Error count should indicate no errors",
53+
0,
54+
theMessageChannel.getSendErrorCount());
55+
}
56+
57+
@Test
58+
public void errorCountWithCountsEnabledHalfErrorsTest() {
59+
Message<String> theInputMessage;
60+
final QueueChannel theMessageChannel = new QueueChannel(MESSAGE_COUNT / 2);
61+
theMessageChannel.setCountsEnabled(true);
62+
63+
for (int i = 0; i < MESSAGE_COUNT; i++) {
64+
theInputMessage = MessageBuilder.withPayload(Integer.toString(i)).build();
65+
theMessageChannel.send(theInputMessage, SEND_TIMEOUT);
66+
}
67+
68+
Assert.assertEquals(
69+
"Message count should match number of sent messages",
70+
MESSAGE_COUNT,
71+
theMessageChannel.getSendCount());
72+
Assert.assertEquals(
73+
"Error count should indicate errors half the messages",
74+
MESSAGE_COUNT / 2,
75+
theMessageChannel.getSendErrorCount());
76+
}
77+
78+
}

0 commit comments

Comments
 (0)