react-native-paho-mqtt
Version:
A fork of the Paho javascript client for use in React Native
237 lines (196 loc) • 6.69 kB
JavaScript
require('./hacks');
var settings = require('./client-harness');
var testServer = settings.server;
var testPort = settings.port;
var testPath = settings.path;
var testMqttVersion = settings.mqttVersion;
var genStr = function (str) {
var time = new Date();
return str + '.' + time.getTime();
};
describe('BasicTest', function () {
//var client = null;
var clientId = this.description;
var connected = false;
var failure = false;
var disconnectError = null;
var disconnectErrorMsg = null;
var subscribed = false;
var isMessageReceived = false;
var isMessageDelivered = false;
var strTopic = '/' + makeid() + '/World';
var strMessageReceived = '';
var strMessageSend = 'Hello';
var strTopicReceived = '';
function makeid() {
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < 5; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
function onConnectSuccess(responseObj) {
//console.log("connected %s",responseObj);
connected = true;
};
function onConnectionLost(err) {
connected = false;
if (err) {
disconnectError = err.errorCode;
disconnectErrorMsg = err.errorMessage;
}
console.log('connection lost. ErrorCode: %s, ErrorMsg: %s', disconnectError, disconnectErrorMsg);
};
function onConnectFailure(err) {
connected = false;
console.log('Connect fail. ErrorCode: %s, ErrorMsg: %s', err.errCode, err.errorMessage);
};
function onSubscribeSuccess() {
subscribed = true;
//console.log("subscribed",subscribed);
};
function onSubscribeFailure(err) {
subscribed = false;
console.log('subscribe fail. ErrorCode: %s, ErrorMsg: %s', err.errCode, err.errorMessage);
};
function onUnsubscribeSuccess() {
subscribed = false;
//console.log("unsubscribed",subscribed);
};
function messageDelivered(response) {
console.log('messageDelivered. topic:%s, duplicate:%s, payloadString:%s,qos:%s,retained:%s', response.destinationName, response.duplicate, response.payloadString, response.qos, response.retained);
isMessageDelivered = true;
//reponse.invocationContext.onMessageArrived = null;
};
function messageArrived(message) {
console.log('messageArrived.', 'topic:', message.destinationName, ' ;content:', message.payloadString);
isMessageReceived = true;
strMessageReceived = message.payloadString;
strTopicReceived = message.destinationName;
//reponse.invocationContext.onMessageArrived = null;
};
beforeEach(function () {
connected = false;
failure = false;
disconnectError = null;
disconnectErrorMsg = null;
//if(!client){
// client = new Paho.MQTT.Client(testServer, testPort, clientId);
//}
});
it('it should connect to a list of server(HA connection).', function () {
var defaultServer = testServer;
var defaultPort = testPort;
var arrHosts = ['localhost', testServer,];
var arrPorts = [2000, testPort];
var client = new Paho.MQTT.Client({
host: defaultServer,
port: defaultPort,
path: testPath,
clientId: genStr(clientId)
});
client.onConnectionLost = onConnectionLost;
expect(client).not.toBe(null);
console.log('should connect to a available server from list');
runs(function () {
client.connect({
onSuccess: onConnectSuccess,
onFailure: onConnectFailure,
hosts: arrHosts,
ports: arrPorts,
mqttVersion: testMqttVersion
});
});
waitsFor(function () {
return connected;
}, 'the client should connect', 2000);
runs(function () {
expect(connected).toBe(true);
//client.disconnect();
});
});
it('it should publish and subscribe.', function () {
var client = new Paho.MQTT.Client({ host: testServer, port: testPort, path: testPath, clientId: genStr(clientId) });
client.onMessageArrived = messageArrived;
client.onMessageDelivered = messageDelivered;
runs(function () {
client.connect({ onSuccess: onConnectSuccess, onFailure: onConnectFailure, mqttVersion: testMqttVersion });
});
waitsFor(function () {
return connected;
}, 'the client should connect', 2000);
runs(function () {
expect(connected).toBe(true);
});
console.log('Subscribe a topic...');
runs(function () {
client.subscribe(strTopic, { onSuccess: onSubscribeSuccess, onFailure: onSubscribeFailure });
});
waitsFor(function () {
return subscribed;
}, 'the client should subscribe', 2000);
runs(function () {
expect(subscribed).toBe(true);
});
console.log('Send and receive message...');
runs(function () {
var message = new Paho.MQTT.Message(strMessageSend);
message.destinationName = strTopic;
client.send(message);
});
waitsFor(function () {
return isMessageReceived;
}, 'the client should send and receive a message', 2000);
runs(function () {
//to do Check message sent
expect(isMessageDelivered).toBe(true);
//Check msg received
expect(isMessageReceived).toBe(true);
//Check message
expect(strMessageReceived).toEqual(strMessageSend);
//Check topic
expect(strTopicReceived).toEqual(strTopic);
//disconnect
//client.disconnect();
});
console.log('Unsubscribe a topic...');
runs(function () {
client.unsubscribe(strTopic, { onSuccess: onUnsubscribeSuccess });
});
waitsFor(function () {
return !subscribed;
}, 'the client should subscribe', 2000);
runs(function () {
expect(subscribed).toBe(false);
//disconnect
//client.disconnect();
});
//should not receive a message after unsubscribe
runs(function () {
//console.log('isMessageReceived',isMessageReceived);
isMessageDelivered = false;
isMessageReceived = false;
strMessageReceived = '';
strTopicReceived = '';
var message = new Paho.MQTT.Message(genStr(strMessageSend));
message.destinationName = strTopic;
client.send(message);
});
waitsFor(function () {
return isMessageDelivered;
}, 'the client should send and receive a message', 2000);
runs(function () {
//to do Check message sent
expect(isMessageDelivered).toBe(true);
//Check msg received
expect(isMessageReceived).toBe(false);
//Check message
expect(strMessageReceived).toEqual('');
//Check topic
expect(strTopicReceived).toEqual('');
//disconnect
//client.disconnect();
});
});
});