Skip to content

Commit

Permalink
fix: fixed data service code flow in case of configuration change (#5074
Browse files Browse the repository at this point in the history
)

* 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]>
  • Loading branch information
MMaiero authored Dec 22, 2023
1 parent 4b06742 commit c054d59
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,9 @@ public Map<String, String> getConnectionInfo() {

@Override
public void startConnectionTask() {
startConnectionMonitorTask();
if (!this.dataTransportService.isConnected()) {
startConnectionMonitorTask();
}
}

@Override
Expand Down
1 change: 1 addition & 0 deletions kura/test/org.eclipse.kura.core.test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Import-Package: javax.comm,
org.mockito;version="[4.0.0,5.0.0)",
org.mockito.invocation;version="[4.0.0,5.0.0)",
org.mockito.stubbing;version="[4.0.0,5.0.0)",
org.mockito.verification;version="4.8.1",
org.osgi.framework,
org.osgi.service.cm,
org.osgi.service.component,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -45,6 +48,7 @@
import org.eclipse.kura.message.store.StoredMessage;
import org.eclipse.kura.message.store.provider.MessageStore;
import org.eclipse.kura.message.store.provider.MessageStoreProvider;
import org.eclipse.kura.status.CloudConnectionStatusComponent;
import org.eclipse.kura.status.CloudConnectionStatusEnum;
import org.eclipse.kura.status.CloudConnectionStatusService;
import org.eclipse.kura.watchdog.WatchdogService;
Expand All @@ -53,13 +57,15 @@
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.mockito.verification.VerificationMode;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;

public class DataServiceImplTest {

private DataServiceImpl dataServiceImpl;
private DataTransportService dataTransportServiceMock;
private CloudConnectionStatusService ccssMock;
private Map<String, Object> properties;
private Optional<Exception> exception = Optional.empty();
private final MessageStoreProvider messageStoreProvider = Mockito.mock(MessageStoreProvider.class);
Expand Down Expand Up @@ -151,6 +157,21 @@ public void shouldStoreMessagesWithPayloadSizeEqualThanConfiguredThreshold() thr
thenNoExceptionIsTrown();
thenMessageIsStored(0, "foo", new byte[4], 0, false, 9);
}

@Test
public void shouldNotDisconnectOnConfigChange() throws KuraStoreException {
givenDataService();
givenMessageStoreProvider();
givenDataTrasportServiceDisconnected();
givenConfigurationProperty("connect.auto-on-startup", true);
givenIsActive();
givenDataTrasportServiceConnected();

whenConfigurationIsChanged("enable.recovery.on.connection.failure", true);

thenDataTrasportStaysConnected();
thenCloudConnectionStatusServiceIsNotChanged();
}

@Test
public void shouldNotStoreMessagesWithPayloadSizeGreaterThanConfiguredThreshold() throws KuraStoreException {
Expand Down Expand Up @@ -225,7 +246,7 @@ private void givenDataService() {
DataServiceOptions dataServiceOptions = new DataServiceOptions(Collections.emptyMap());
MessageStoreProvider messageStoreProviderMock = mock(MessageStoreProvider.class);
MessageStore messageStoreMock = mock(MessageStore.class);
CloudConnectionStatusService ccssMock = mock(CloudConnectionStatusService.class);
this.ccssMock = mock(CloudConnectionStatusService.class);
WatchdogService watchdogServiceMock = mock(WatchdogService.class);
initMockMessageStore(messageStoreProviderMock, messageStoreMock);
TestUtil.setFieldValue(this.dataServiceImpl, "dataServiceOptions", dataServiceOptions);
Expand All @@ -237,6 +258,11 @@ private void givenDataService() {
}

}

private void whenConfigurationIsChanged(final String key, final Object value) {
this.properties.put(key, value);
this.dataServiceImpl.updated(properties);
}

private void whenMessageIsPublished(final String topic, final byte[] payload, final int qos, final boolean retain,
final int priority) {
Expand Down Expand Up @@ -266,7 +292,19 @@ private void thenDataTrasportIsConnected() {
fail();
}
}

private void thenDataTrasportStaysConnected() {
try {
verify(this.dataTransportServiceMock, times(0)).connect();
} catch (KuraConnectException e) {
fail();
}
}

private void thenCloudConnectionStatusServiceIsNotChanged() {
verify(this.ccssMock, times(1)).updateStatus(any(CloudConnectionStatusComponent.class), eq(CloudConnectionStatusEnum.SLOW_BLINKING));
}

private void thenStartConnectionTaskIsInvoked() {
verify(this.dataServiceImpl, times(2)).startConnectionTask();
}
Expand Down

0 comments on commit c054d59

Please sign in to comment.