Skip to content

Commit

Permalink
Add no onmessage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dilanSachi committed Jul 19, 2023
1 parent 747ea6e commit aeb99d3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
31 changes: 29 additions & 2 deletions ballerina/tests/publish_subscribe_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ function subscribeToMultipleSubscriptionsTest() returns error? {

@test:Config {enable: true}
function publishSubscribeWithMTLSTrustKeyStoresTest() returns error? {
Listener 'listener = check new (NO_AUTH_MTLS_ENDPOINT, uuid:createType1AsString(), "mqtt/trustkeystorestopic", {connectionConfig: mtlsConnConfig});
Listener 'listener = check new (NO_AUTH_MTLS_ENDPOINT, uuid:createType1AsString(), "mqtt/trustkeystorestopic", {connectionConfig: mtlsWithTrustKeyStoreConnConfig});
check 'listener.attach(basicService);
check 'listener.'start();

Client 'client = check new (NO_AUTH_MTLS_ENDPOINT, uuid:createType1AsString(), {connectionConfig: mtlsConnConfig});
Client 'client = check new (NO_AUTH_MTLS_ENDPOINT, uuid:createType1AsString(), {connectionConfig: mtlsWithTrustKeyStoreConnConfig});
string message = "Test message for mtls with trust and key stores";
check 'client->publish("mqtt/trustkeystorestopic", {payload: message.toBytes()});
runtime:sleep(1);
Expand Down Expand Up @@ -243,6 +243,7 @@ function clientReconnectTest() returns error? {
runtime:sleep(10);
isConnected = check 'client->isConnected();
test:assertTrue(isConnected);
check stopListenerAndClient('client = 'client);
}

@test:Config {enable: true}
Expand Down Expand Up @@ -320,6 +321,7 @@ function listenerGracefulStopTest() returns error? {
message = "Test message 2 for graceful stop";
check 'client->publish("mqtt/gracefulstoptopic", {payload: message.toBytes()});
test:assertTrue(receivedMessages.indexOf(message) == ());
check stopListenerAndClient('client = 'client);
}

@test:Config {enable: true}
Expand All @@ -337,6 +339,7 @@ function listenerImmediateStopTest() returns error? {
message = "Test message 2 for immediate stop";
check 'client->publish("mqtt/immediatestoptopic", {payload: message.toBytes()});
test:assertTrue(receivedMessages.indexOf(message) == ());
check stopListenerAndClient('client = 'client);
}

@test:Config {enable: true}
Expand All @@ -354,4 +357,28 @@ function listenerDetachTest() returns error? {
message = "Test message 2 for detach";
check 'client->publish("mqtt/detachtopic", {payload: message.toBytes()});
test:assertTrue(receivedMessages.indexOf(message) == ());
check stopListenerAndClient('client = 'client);
}

@test:Config {enable: true}
function serviceWithoutOnMessageTest() returns error? {
Listener 'listener = check new (NO_AUTH_MTLS_ENDPOINT, uuid:createType1AsString(), "mqtt/noonmessagetopic", {connectionConfig: mtlsConnConfig, manualAcks: true});
string errorMessage = "";
Service noOnMsgService = service object {
remote function onError(Error err) returns error? {
log:printError("Error occured ", err);
errorMessage = err.message();
}
};
check 'listener.attach(noOnMsgService);
check 'listener.'start();

Client 'client = check new (NO_AUTH_MTLS_ENDPOINT, uuid:createType1AsString(), {connectionConfig: mtlsConnConfig});
string message = "Test message for service without onmessage method";
check 'client->publish("mqtt/noonmessagetopic", {payload: message.toBytes()});
runtime:sleep(1);

check stopListenerAndClient('listener, 'client);

test:assertEquals(errorMessage, "method onMessage not found");
}
12 changes: 8 additions & 4 deletions ballerina/tests/test_utilities.bal
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ final ConnectionConfiguration authMtlsConnConfig = {
}
};

function stopListenerAndClient(Listener 'listener, Client 'client) returns error? {
check 'client->disconnect();
check 'client->close();
check 'listener.gracefulStop();
function stopListenerAndClient(Listener? 'listener = (), Client? 'client = ()) returns error? {
if 'client != () {
check 'client->disconnect();
check 'client->close();
}
if 'listener != () {
check 'listener.gracefulStop();
}
}

0 comments on commit aeb99d3

Please sign in to comment.