Skip to content

Commit

Permalink
feat: Birth messages now default to Qos 1 (#4876)
Browse files Browse the repository at this point in the history
* feat: Birth messages now default to Qos 1

Signed-off-by: MMaiero <[email protected]>

* feat: qos 1 for birth, 0 for disconnect

Signed-off-by: MMaiero <[email protected]>

---------

Signed-off-by: MMaiero <[email protected]>
  • Loading branch information
MMaiero authored Oct 11, 2023
1 parent 7c19a6f commit 986c721
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,7 @@ private boolean isValidMessage(KuraApplicationTopic kuraTopic, KuraPayload kuraP
public void onMessagePublished(int messageId, String topic) {
synchronized (this.messageId) {
if (this.messageId.get() != -1 && this.messageId.get() == messageId) {
if (CloudServiceOptions.getLifeCycleMessageQos() == 0) {
this.messageId.set(-1);
}
this.messageId.set(-1);
this.messageId.notifyAll();
return;
}
Expand Down Expand Up @@ -885,7 +883,7 @@ private void publishLifeCycleMessage(LifecycleMessage message) throws KuraExcept
payload.setTimestamp(new Date());
byte[] encodedPayload = encodePayload(payload);
int id = this.dataService.publish(message.getTopic(), encodedPayload,
CloudServiceOptions.getLifeCycleMessageQos(), CloudServiceOptions.getLifeCycleMessageRetain(),
message.getQos(), CloudServiceOptions.getLifeCycleMessageRetain(),
CloudServiceOptions.getLifeCycleMessagePriority());
this.messageId.set(id);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class CloudServiceOptions {
private static final String ENABLE_DFLT_SUBSCRIPTIONS = "enable.default.subscriptions";
private static final String PAYLOAD_ENCODING = "payload.encoding";

private static final int LIFECYCLE_QOS = 0;
private static final int LIFECYCLE_PRIORITY = 0;
private static final boolean LIFECYCLE_RETAIN = false;

Expand Down Expand Up @@ -217,10 +216,6 @@ public static String getTopicWildCard() {
return TOPIC_WILD_CARD;
}

public static int getLifeCycleMessageQos() {
return LIFECYCLE_QOS;
}

public static int getLifeCycleMessagePriority() {
return LIFECYCLE_PRIORITY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class LifecycleMessage {
private KuraPayload payload;
private boolean isAppCertificateMessage = false;
private boolean isBirthCertificateMessage = false;
private int qos = 0;

public LifecycleMessage(CloudServiceOptions options, CloudServiceImpl cloudServiceImpl) {
this.topicBuilder = new StringBuilder(options.getTopicControlPrefix());
Expand All @@ -37,13 +38,15 @@ public LifecycleMessage asBirthCertificateMessage() {
this.topicBuilder.append(CloudServiceOptions.getTopicBirthSuffix());
this.payload = this.payloadBuilder.buildBirthPayload();
this.isBirthCertificateMessage = true;
this.qos = 1;
return this;
}

public LifecycleMessage asAppCertificateMessage() {
this.topicBuilder.append(CloudServiceOptions.getTopicAppsSuffix());
this.payload = this.payloadBuilder.buildBirthPayload();
this.isAppCertificateMessage = true;
this.qos = 1;
return this;
}

Expand All @@ -68,5 +71,9 @@ public boolean isAppCertificateMessage() {
public boolean isBirthCertificateMessage() {
return this.isBirthCertificateMessage;
}

public int getQos() {
return this.qos;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void shouldPublishImmediatelyWhenDeactivate() throws KuraException {
whenDeactivate();

thenNoExceptionOccurred();
thenBirthIsPublishedImmediately(BIRTH_TOPIC_PREFIX + CloudServiceOptions.getTopicDisconnectSuffix());
thenDisconnectIsPublishedImmediately(BIRTH_TOPIC_PREFIX + CloudServiceOptions.getTopicDisconnectSuffix());
}

@Test
Expand Down Expand Up @@ -180,7 +180,7 @@ public void shouldPublishImmediatelyOnDisconnecting() throws KuraException {
whenOnDisconnecting();

thenNoExceptionOccurred();
thenBirthIsPublishedImmediately(BIRTH_TOPIC_PREFIX + CloudServiceOptions.getTopicDisconnectSuffix());
thenDisconnectIsPublishedImmediately(BIRTH_TOPIC_PREFIX + CloudServiceOptions.getTopicDisconnectSuffix());
}

@Test
Expand Down Expand Up @@ -394,11 +394,16 @@ private void thenNoBirthIsPublished() throws KuraStoreException {
private void thenBirthIsPublishedAfter(long delayMillis, String expectedTopic) throws KuraException {
verify(this.dataService, after(delayMillis).never()).publish(eq(expectedTopic), any(), eq(0), eq(false),
eq(0));
verify(this.dataService, after(delayMillis + SLACK_DELAY).times(1)).publish(eq(expectedTopic), any(), eq(0),
verify(this.dataService, after(delayMillis + SLACK_DELAY).times(1)).publish(eq(expectedTopic), any(), eq(1),
eq(false), eq(0));
}

private void thenBirthIsPublishedImmediately(String expectedTopic) throws KuraException {
verify(this.dataService, timeout(SLACK_DELAY).times(1)).publish(eq(expectedTopic), any(), eq(1), eq(false),
eq(0));
}

private void thenDisconnectIsPublishedImmediately(String expectedTopic) throws KuraException {
verify(this.dataService, timeout(SLACK_DELAY).times(1)).publish(eq(expectedTopic), any(), eq(0), eq(false),
eq(0));
}
Expand Down

0 comments on commit 986c721

Please sign in to comment.