/
AmqpPublisherActorTest.java
219 lines (187 loc) · 10.6 KB
/
AmqpPublisherActorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.connectivity.messaging.amqp;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.after;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.time.Duration;
import java.util.Collections;
import javax.jms.CompletionListener;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import org.apache.qpid.jms.JmsSession;
import org.apache.qpid.jms.message.JmsMessage;
import org.apache.qpid.jms.message.JmsTextMessage;
import org.apache.qpid.jms.provider.amqp.AmqpConnection;
import org.apache.qpid.jms.provider.amqp.message.AmqpJmsTextMessageFacade;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.connectivity.ConnectionId;
import org.eclipse.ditto.model.connectivity.Target;
import org.eclipse.ditto.services.connectivity.messaging.AbstractPublisherActorTest;
import org.eclipse.ditto.services.connectivity.messaging.TestConstants;
import org.eclipse.ditto.services.connectivity.messaging.amqp.status.ProducerClosedStatusReport;
import org.eclipse.ditto.services.connectivity.messaging.config.ConnectionConfig;
import org.eclipse.ditto.services.connectivity.messaging.config.DittoConnectivityConfig;
import org.eclipse.ditto.services.models.connectivity.ExternalMessage;
import org.eclipse.ditto.services.models.connectivity.ExternalMessageFactory;
import org.eclipse.ditto.services.models.connectivity.OutboundSignal;
import org.eclipse.ditto.services.models.connectivity.OutboundSignalFactory;
import org.eclipse.ditto.services.utils.config.DefaultScopedConfig;
import org.eclipse.ditto.signals.base.Signal;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
public class AmqpPublisherActorTest extends AbstractPublisherActorTest {
private static final Logger LOGGER = LoggerFactory.getLogger(AmqpPublisherActorTest.class);
private JmsSession session;
private MessageProducer messageProducer;
@Override
protected void setupMocks(final TestProbe probe) throws JMSException {
session = mock(JmsSession.class);
messageProducer = mock(MessageProducer.class);
when(session.createProducer(any(Destination.class))).thenReturn(messageProducer);
when(session.createTextMessage(anyString())).thenAnswer((Answer<JmsMessage>) invocation -> {
final String argument = invocation.getArgument(0);
final AmqpJmsTextMessageFacade facade = new AmqpJmsTextMessageFacade() {{
connection = mock(AmqpConnection.class);
}};
final JmsTextMessage jmsTextMessage = new JmsTextMessage(facade);
jmsTextMessage.setText(argument);
return jmsTextMessage;
});
}
@Test
public void testRecoverPublisher() throws Exception {
new TestKit(actorSystem) {{
final TestProbe probe = new TestProbe(actorSystem);
setupMocks(probe);
final OutboundSignal outboundSignal = mock(OutboundSignal.class);
final Signal source = mock(Signal.class);
when(source.getEntityId()).thenReturn(TestConstants.Things.THING_ID);
when(source.getDittoHeaders()).thenReturn(DittoHeaders.empty());
when(outboundSignal.getSource()).thenReturn(source);
final Target target = createTestTarget();
when(outboundSignal.getTargets()).thenReturn(Collections.singletonList(decorateTarget(target)));
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().putHeader("device_id", "ditto:thing").build();
final ExternalMessage externalMessage =
ExternalMessageFactory.newExternalMessageBuilder(dittoHeaders).withText("payload").build();
final OutboundSignal.WithExternalMessage mappedOutboundSignal =
OutboundSignalFactory.newMappedOutboundSignal(outboundSignal, externalMessage);
final Props props = AmqpPublisherActor.props(ConnectionId.generateRandom(),
Collections.singletonList(TestConstants.Targets.TWIN_TARGET.withAddress(getOutboundAddress())),
session,
loadConnectionConfig());
final ActorRef publisherActor = actorSystem.actorOf(props);
publisherActor.tell(mappedOutboundSignal, getRef());
publisherActor.tell(mappedOutboundSignal, getRef());
// producer is cached so created only once
verify(session, timeout(1_000).times(1)).createProducer(any(Destination.class));
verify(messageProducer, timeout(1000).times(2)).send(any(Message.class), any(CompletionListener.class));
// check that backoff works properly, we expect 4 invocations after 10 seconds
// - first invocation is the initial creation of the producer from above
// - by default backoff starts with 1 second and doubles with each subsequent backoff
// --> we expect backoff to trigger after 1 second, 3 seconds, 7 seconds (+ some delay)
// --> 4 calls to createProducer in total after 10 seconds
for (int i = 0; i < 3; i++) {
// trigger closing of producer
publisherActor.tell(ProducerClosedStatusReport.get(messageProducer), getRef());
final int wantedNumberOfInvocations = i + 2;
final long millis =
1_000 // initial backoff
* (long) (Math.pow(2, i)) // backoff doubles with each retry
+ 500; // give the producer some time to recover
LOGGER.info("Want {} invocations after {}ms.", wantedNumberOfInvocations, millis);
verify(session, after(millis)
.times(wantedNumberOfInvocations)).createProducer(any(Destination.class));
}
}};
}
@Test
public void producerClosedDuringBackOffIsIgnored() throws Exception {
new TestKit(actorSystem) {{
final TestProbe probe = new TestProbe(actorSystem);
setupMocks(probe);
final OutboundSignal outboundSignal = mock(OutboundSignal.class);
final Signal source = mock(Signal.class);
when(source.getEntityId()).thenReturn(TestConstants.Things.THING_ID);
when(source.getDittoHeaders()).thenReturn(DittoHeaders.empty());
when(outboundSignal.getSource()).thenReturn(source);
final Target target = createTestTarget();
when(outboundSignal.getTargets()).thenReturn(Collections.singletonList(decorateTarget(target)));
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().putHeader("device_id", "ditto:thing").build();
final ExternalMessage externalMessage =
ExternalMessageFactory.newExternalMessageBuilder(dittoHeaders).withText("payload").build();
final OutboundSignal.WithExternalMessage mappedOutboundSignal =
OutboundSignalFactory.newMappedOutboundSignal(outboundSignal, externalMessage);
final Props props = AmqpPublisherActor.props(ConnectionId.generateRandom(),
Collections.singletonList(TestConstants.Targets.TWIN_TARGET.withAddress(getOutboundAddress())),
session,
loadConnectionConfig());
final ActorRef publisherActor = actorSystem.actorOf(props);
publisherActor.tell(mappedOutboundSignal, getRef());
publisherActor.tell(mappedOutboundSignal, getRef());
// producer is cached so created only once
verify(session, timeout(1_000)).createProducer(any(Destination.class));
// and trigger closing of producer multiple times
publisherActor.tell(ProducerClosedStatusReport.get(messageProducer), getRef());
publisherActor.tell(ProducerClosedStatusReport.get(messageProducer), getRef());
publisherActor.tell(ProducerClosedStatusReport.get(messageProducer), getRef());
// check that createProducer is called only twice in the next 10 seconds (once for the initial create, once for the backoff)
verify(session, after(10_000).times(2)).createProducer(any(Destination.class));
}};
}
@Override
protected Props getPublisherActorProps() {
return AmqpPublisherActor.props(ConnectionId.of("theConnection"), Collections.emptyList(), session,
loadConnectionConfig());
}
@Override
protected void verifyPublishedMessage() throws Exception {
final ArgumentCaptor<JmsMessage> messageCaptor = ArgumentCaptor.forClass(JmsMessage.class);
verify(messageProducer, timeout(1000)).send(messageCaptor.capture(), any(CompletionListener.class));
final Message message = messageCaptor.getValue();
assertThat(message).isNotNull();
assertThat(message.getStringProperty("thing_id")).isEqualTo(TestConstants.Things.THING_ID.toString());
assertThat(message.getStringProperty("suffixed_thing_id")).isEqualTo(
TestConstants.Things.THING_ID + ".some.suffix");
assertThat(message.getStringProperty("prefixed_thing_id")).isEqualTo(
"some.prefix." + TestConstants.Things.THING_ID);
assertThat(message.getStringProperty("eclipse")).isEqualTo("ditto");
assertThat(message.getStringProperty("device_id"))
.isEqualTo(TestConstants.Things.THING_ID.toString());
}
@Override
protected Target decorateTarget(final Target target) {
return target;
}
protected String getOutboundAddress() {
return "outbound";
}
private static ConnectionConfig loadConnectionConfig() {
return DittoConnectivityConfig.of(DefaultScopedConfig.dittoScoped(CONFIG)).getConnectionConfig();
}
}