Skip to content

Commit

Permalink
Create/destroy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bretambrose committed Jun 25, 2024
1 parent b676e13 commit 915e595
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
161 changes: 161 additions & 0 deletions lib/iotjobs/iotjobsclientv2.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/


import {iot, mqtt5, mqtt as mqtt311, mqtt_request_response} from "aws-crt";
import {v4 as uuid} from "uuid";
import {once} from "events";
import {IotJobsClientv2} from "./iotjobsclientv2";
//import * as model from "./model";

jest.setTimeout(1000000);

function hasTestEnvironment() : boolean {
if (process.env.AWS_TEST_MQTT5_IOT_CORE_HOST === undefined) {
return false;
}

if (process.env.AWS_TEST_MQTT5_IOT_CORE_RSA_CERT === undefined) {
return false;
}

if (process.env.AWS_TEST_MQTT5_IOT_CORE_RSA_KEY === undefined) {
return false;
}

return true;
}

const conditional_test = (condition : boolean) => condition ? it : it.skip;

function build_protocol_client_mqtt5() : mqtt5.Mqtt5Client {
let builder = iot.AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromPath(
// @ts-ignore
process.env.AWS_TEST_MQTT5_IOT_CORE_HOST,
process.env.AWS_TEST_MQTT5_IOT_CORE_RSA_CERT,
process.env.AWS_TEST_MQTT5_IOT_CORE_RSA_KEY
);

builder.withConnectProperties({
clientId : uuid(),
keepAliveIntervalSeconds: 1200,
});

return new mqtt5.Mqtt5Client(builder.build());
}

function build_protocol_client_mqtt311() : mqtt311.MqttClientConnection {
// @ts-ignore
let builder = iot.AwsIotMqttConnectionConfigBuilder.new_mtls_builder_from_path(process.env.AWS_TEST_MQTT5_IOT_CORE_RSA_CERT, process.env.AWS_TEST_MQTT5_IOT_CORE_RSA_KEY);
// @ts-ignore
builder.with_endpoint(process.env.AWS_TEST_MQTT5_IOT_CORE_HOST);
builder.with_client_id(uuid());

let client = new mqtt311.MqttClient();
return client.new_connection(builder.build());
}

enum ProtocolVersion {
Mqtt311,
Mqtt5
}

interface TestingOptions {
version: ProtocolVersion,
timeoutSeconds?: number,
}

class JobsTestingContext {

mqtt311Client?: mqtt311.MqttClientConnection;
mqtt5Client?: mqtt5.Mqtt5Client;

client: IotJobsClientv2;

private protocolStarted : boolean = false;

async startProtocolClient() {
if (!this.protocolStarted) {
this.protocolStarted = true;
if (this.mqtt5Client) {
let connected = once(this.mqtt5Client, mqtt5.Mqtt5Client.CONNECTION_SUCCESS);
this.mqtt5Client.start();

await connected;
}

if (this.mqtt311Client) {
await this.mqtt311Client.connect();
}
}
}

async stopProtocolClient() {
if (this.protocolStarted) {
this.protocolStarted = false;
if (this.mqtt5Client) {
let stopped = once(this.mqtt5Client, mqtt5.Mqtt5Client.STOPPED);
this.mqtt5Client.stop();
await stopped;

this.mqtt5Client.close();
}

if (this.mqtt311Client) {
await this.mqtt311Client.disconnect();
}
}
}

constructor(options: TestingOptions) {
if (options.version == ProtocolVersion.Mqtt5) {
this.mqtt5Client = build_protocol_client_mqtt5();

let rrOptions : mqtt_request_response.RequestResponseClientOptions = {
maxRequestResponseSubscriptions : 6,
maxStreamingSubscriptions : 2,
operationTimeoutInSeconds : options.timeoutSeconds ?? 60,
}

this.client = IotJobsClientv2.newFromMqtt5(this.mqtt5Client, rrOptions);
} else {
this.mqtt311Client = build_protocol_client_mqtt311();

let rrOptions : mqtt_request_response.RequestResponseClientOptions = {
maxRequestResponseSubscriptions : 6,
maxStreamingSubscriptions : 2,
operationTimeoutInSeconds : options.timeoutSeconds ?? 60,
}

this.client = IotJobsClientv2.newFromMqtt311(this.mqtt311Client, rrOptions);
}
}

async open() {
await this.startProtocolClient();
}

async close() {
this.client.close();
await this.stopProtocolClient();
}
}

async function doCreateDestroyTest(version: ProtocolVersion) {
let context = new JobsTestingContext({
version: version
});
await context.open();

await context.close();
}

conditional_test(hasTestEnvironment())('jobsv2 - create destroy mqtt5', async () => {
await doCreateDestroyTest(ProtocolVersion.Mqtt5);
});

conditional_test(hasTestEnvironment())('jobsv2 - create destroy mqtt311', async () => {
await doCreateDestroyTest(ProtocolVersion.Mqtt311);
});
1 change: 1 addition & 0 deletions test/native/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
testMatch: [
'<rootDir>/lib/*.spec.ts',
'<rootDir>/lib/echo_rpc/*.spec.ts',
'<rootDir>/lib/iotjobs/*.spec.ts',
'<rootDir>/lib/iotshadow/*.spec.ts'
],
preset: 'ts-jest',
Expand Down

0 comments on commit 915e595

Please sign in to comment.