Skip to content

Commit c054d59

Browse files
authored
fix: fixed data service code flow in case of configuration change (#5074)
* fix: fixed data service code flow in case of configuration change If the device is already connected, it will not try agan to reconnect. Restored the code path from Kura 5.2 Signed-off-by: MMaiero <[email protected]> * chore: Addded tests Signed-off-by: MMaiero <[email protected]> * fix: Additional test fix Signed-off-by: MMaiero <[email protected]> --------- Signed-off-by: MMaiero <[email protected]>
1 parent 4b06742 commit c054d59

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

kura/org.eclipse.kura.core/src/main/java/org/eclipse/kura/core/data/DataServiceImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,9 @@ public Map<String, String> getConnectionInfo() {
11301130

11311131
@Override
11321132
public void startConnectionTask() {
1133-
startConnectionMonitorTask();
1133+
if (!this.dataTransportService.isConnected()) {
1134+
startConnectionMonitorTask();
1135+
}
11341136
}
11351137

11361138
@Override

kura/test/org.eclipse.kura.core.test/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Import-Package: javax.comm,
3737
org.mockito;version="[4.0.0,5.0.0)",
3838
org.mockito.invocation;version="[4.0.0,5.0.0)",
3939
org.mockito.stubbing;version="[4.0.0,5.0.0)",
40+
org.mockito.verification;version="4.8.1",
4041
org.osgi.framework,
4142
org.osgi.service.cm,
4243
org.osgi.service.component,

kura/test/org.eclipse.kura.core.test/src/test/java/org/eclipse/kura/core/data/DataServiceImplTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
import static org.junit.Assert.assertFalse;
1818
import static org.junit.Assert.assertTrue;
1919
import static org.junit.Assert.fail;
20+
import static org.mockito.ArgumentMatchers.any;
2021
import static org.mockito.ArgumentMatchers.anyLong;
22+
import static org.mockito.ArgumentMatchers.eq;
2123
import static org.mockito.Mockito.doAnswer;
2224
import static org.mockito.Mockito.doThrow;
2325
import static org.mockito.Mockito.mock;
2426
import static org.mockito.Mockito.spy;
27+
import static org.mockito.Mockito.timeout;
2528
import static org.mockito.Mockito.times;
2629
import static org.mockito.Mockito.verify;
2730
import static org.mockito.Mockito.when;
@@ -45,6 +48,7 @@
4548
import org.eclipse.kura.message.store.StoredMessage;
4649
import org.eclipse.kura.message.store.provider.MessageStore;
4750
import org.eclipse.kura.message.store.provider.MessageStoreProvider;
51+
import org.eclipse.kura.status.CloudConnectionStatusComponent;
4852
import org.eclipse.kura.status.CloudConnectionStatusEnum;
4953
import org.eclipse.kura.status.CloudConnectionStatusService;
5054
import org.eclipse.kura.watchdog.WatchdogService;
@@ -53,13 +57,15 @@
5357
import org.junit.Test;
5458
import org.mockito.ArgumentMatchers;
5559
import org.mockito.Mockito;
60+
import org.mockito.verification.VerificationMode;
5661
import org.osgi.framework.BundleContext;
5762
import org.osgi.service.component.ComponentContext;
5863

5964
public class DataServiceImplTest {
6065

6166
private DataServiceImpl dataServiceImpl;
6267
private DataTransportService dataTransportServiceMock;
68+
private CloudConnectionStatusService ccssMock;
6369
private Map<String, Object> properties;
6470
private Optional<Exception> exception = Optional.empty();
6571
private final MessageStoreProvider messageStoreProvider = Mockito.mock(MessageStoreProvider.class);
@@ -151,6 +157,21 @@ public void shouldStoreMessagesWithPayloadSizeEqualThanConfiguredThreshold() thr
151157
thenNoExceptionIsTrown();
152158
thenMessageIsStored(0, "foo", new byte[4], 0, false, 9);
153159
}
160+
161+
@Test
162+
public void shouldNotDisconnectOnConfigChange() throws KuraStoreException {
163+
givenDataService();
164+
givenMessageStoreProvider();
165+
givenDataTrasportServiceDisconnected();
166+
givenConfigurationProperty("connect.auto-on-startup", true);
167+
givenIsActive();
168+
givenDataTrasportServiceConnected();
169+
170+
whenConfigurationIsChanged("enable.recovery.on.connection.failure", true);
171+
172+
thenDataTrasportStaysConnected();
173+
thenCloudConnectionStatusServiceIsNotChanged();
174+
}
154175

155176
@Test
156177
public void shouldNotStoreMessagesWithPayloadSizeGreaterThanConfiguredThreshold() throws KuraStoreException {
@@ -225,7 +246,7 @@ private void givenDataService() {
225246
DataServiceOptions dataServiceOptions = new DataServiceOptions(Collections.emptyMap());
226247
MessageStoreProvider messageStoreProviderMock = mock(MessageStoreProvider.class);
227248
MessageStore messageStoreMock = mock(MessageStore.class);
228-
CloudConnectionStatusService ccssMock = mock(CloudConnectionStatusService.class);
249+
this.ccssMock = mock(CloudConnectionStatusService.class);
229250
WatchdogService watchdogServiceMock = mock(WatchdogService.class);
230251
initMockMessageStore(messageStoreProviderMock, messageStoreMock);
231252
TestUtil.setFieldValue(this.dataServiceImpl, "dataServiceOptions", dataServiceOptions);
@@ -237,6 +258,11 @@ private void givenDataService() {
237258
}
238259

239260
}
261+
262+
private void whenConfigurationIsChanged(final String key, final Object value) {
263+
this.properties.put(key, value);
264+
this.dataServiceImpl.updated(properties);
265+
}
240266

241267
private void whenMessageIsPublished(final String topic, final byte[] payload, final int qos, final boolean retain,
242268
final int priority) {
@@ -266,7 +292,19 @@ private void thenDataTrasportIsConnected() {
266292
fail();
267293
}
268294
}
295+
296+
private void thenDataTrasportStaysConnected() {
297+
try {
298+
verify(this.dataTransportServiceMock, times(0)).connect();
299+
} catch (KuraConnectException e) {
300+
fail();
301+
}
302+
}
269303

304+
private void thenCloudConnectionStatusServiceIsNotChanged() {
305+
verify(this.ccssMock, times(1)).updateStatus(any(CloudConnectionStatusComponent.class), eq(CloudConnectionStatusEnum.SLOW_BLINKING));
306+
}
307+
270308
private void thenStartConnectionTaskIsInvoked() {
271309
verify(this.dataServiceImpl, times(2)).startConnectionTask();
272310
}

0 commit comments

Comments
 (0)