Skip to content

Commit

Permalink
Add specs for variations of calling channel#publish & presence#enter
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Woolf committed May 11, 2015
1 parent 440f4ec commit a248d05
Show file tree
Hide file tree
Showing 2 changed files with 280 additions and 1 deletion.
107 changes: 107 additions & 0 deletions spec/realtime/message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,113 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
}
};

exports.publishVariations = function(test) {
var transport = 'binary';

test.expect(32);
try {
/* set up realtime */
var realtime = helper.AblyRealtime();
var rest = helper.AblyRest();

/* connect and attach */
realtime.connection.on('connected', function() {
var rtChannel = realtime.channels.get('publishVariations');
var testData = 'Some data'
var errorCallback = function(err){
if(err) {
test.ok(false, 'Error received by publish callback ' + err);
test.done();
realtime.close();
return;
}
}
rtChannel.attach(function(err) {
if(err) {
test.ok(false, 'Attach failed with error: ' + err);
test.done();
realtime.close();
return;
}

/* subscribe to different message types */
var messagesReceived = 0
rtChannel.subscribe(function(msg) {
test.ok(true, 'Received ' + msg.name);
++messagesReceived;
switch(msg.name) {
case 'objectWithName':
case 'objectWithNameAndCallback':
case 'objectWithNameAndNullData':
case 'objectWithNameAndUndefinedData':
case 'nameAndNullData':
case 'nameAndUndefinedData':
test.equal(typeof(msg.data), 'undefined', 'Msg data was received where none expected');
break;
case 'nameAndData':
case 'nameAndDataAndCallback':
case 'objectWithNameAndData':
case 'objectWithNameAndDataAndCallback':
test.equal(msg.data, testData, 'Msg data ' + msg.data + 'Unexpected message data received');
break;
case undefined:
if (msg.data) {
// 3 messages: null name and data, null name and data and callback, object with null name and data
test.equal(msg.data, testData, 'Msg data ' + msg.data + 'Unexpected message data received');
} else {
// 3 messages: null name and null data, object with null name and no data, object with null name and null data
test.equal(typeof(msg.data), 'undefined', 'Msg data was received where none expected');
}
break;
default:
test.ok(false, 'Unexpected message ' + msg.name + 'received');
test.done();
realtime.close();
}

if (messagesReceived == 16) {

This comment has been minimized.

Copy link
@paddybyers

paddybyers May 12, 2015

Member

Isn't there a way to avoid having this hard-coded here?

For example:

var testArguments = [
  [{name: 'objectWithName'}],
  [{name: 'objectWithNameAndCallback'}, errorCallback],
  ...
];

for(var i = 0; i < testArguments.length; i++) {
  restChannel.publish.apply(restChannel, testArguments[i]);
}

...
  if (messagesReceived == test.Messages.length) {
  ...

This comment has been minimized.

Copy link
@SimonWoolf

SimonWoolf May 12, 2015

Member

👍 00e46b5

test.done();
realtime.close();
}
});

/* publish events */
var restChannel = rest.channels.get('publishVariations');
restChannel.publish({name: 'objectWithName'});
restChannel.publish({name: 'objectWithNameAndCallback'}, errorCallback);
restChannel.publish({name: 'objectWithNameAndNullData', data: null});
restChannel.publish({name: 'objectWithNameAndUndefinedData', data: undefined});
restChannel.publish('nameAndNullData', null);
restChannel.publish('nameAndUndefinedData', undefined);
restChannel.publish('nameAndData', testData);
restChannel.publish('nameAndDataAndCallback', testData, errorCallback);
restChannel.publish({name: 'objectWithNameAndData', data: testData});
restChannel.publish({name: 'objectWithNameAndDataAndCallback', data: testData}, errorCallback);
// 6 messages with null name:
restChannel.publish(null, testData);
restChannel.publish(null, testData, errorCallback);
restChannel.publish({name: null, data: testData});
restChannel.publish(null, null);
restChannel.publish({name: null});
restChannel.publish({name: null, data: null});
});
});
var exitOnState = function(state) {
realtime.connection.on(state, function () {
test.ok(false, transport + ' connection to server failed');
test.done();
realtime.close();
});
};
exitOnState('failed');
exitOnState('suspended');
} catch(e) {
test.ok(false, 'Channel attach failed with exception: ' + e.stack);
test.done();
realtime.close();
}
};

exports.restpublish = function(test) {
var count = 10;
var rest = helper.AblyRest();
Expand Down
174 changes: 173 additions & 1 deletion spec/realtime/presence.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ define(['ably', 'shared_helper', 'async'], function(Ably, helper, async) {
};

/*
* Attach to channel, enter presence channel and await entered event
* Attach to channel, enter presence channel with data and await entered event
*/
exports.enter0 = function(test) {
var clientRealtime;
Expand Down Expand Up @@ -343,6 +343,178 @@ define(['ably', 'shared_helper', 'async'], function(Ably, helper, async) {
}
};

/*
* Attach to channel, enter presence channel with a callback but no data and await entered event
*/
exports.enter4 = function(test) {
var clientRealtime;
var isDone = false, done = function() {
if(!isDone) {
isDone = true;
setTimeout(function() {
test.done(); clientRealtime.close();
}, 3000);
}
};
try {
var transport = 'web_socket';

test.expect(2);
/* listen for the enter event, test is complete when received */
var presenceHandler = function() {
if(this.event == 'enter') {
test.ok(true, 'Presence event received');
done();
presenceChannel.presence.off(presenceHandler);
}
};
presenceChannel.presence.on(presenceHandler);

/* set up authenticated connection */
clientRealtime = helper.AblyRealtime({ clientId: testClientId, authToken: authToken, transports: [transport] });
clientRealtime.connection.on('connected', function() {
/* get channel, attach, and enter */
var clientChannel = clientRealtime.channels.get('presence0');
clientChannel.attach(function(err) {
if(err) {
test.ok(false, 'Attach failed with error: ' + err);
done();
return;
}
clientChannel.presence.enter(function(err) {
if(err) {
test.ok(false, 'Enter failed with error: ' + err);
done();
return;
}
test.ok(true, 'Presence event sent');
});
});
});
var exitOnState = function(state) {
clientRealtime.connection.on(state, function () {
test.ok(false, transport + ' connection to server failed');
done();
});
};
exitOnState('failed');
exitOnState('suspended');
} catch(e) {
test.ok(false, 'presence.enter4 failed with exception: ' + e.stack);
done();
}
};

/*
* Attach to channel, enter presence channel with neither callback nor data and await entered event
*/
exports.enter5 = function(test) {
var clientRealtime;
var isDone = false, done = function() {
if(!isDone) {
isDone = true;
setTimeout(function() {
test.done(); clientRealtime.close();
}, 3000);
}
};
try {
var transport = 'web_socket';

test.expect(1);
/* listen for the enter event, test is complete when received */
var presenceHandler = function() {
if(this.event == 'enter') {
test.ok(true, 'Presence event received');
done();
presenceChannel.presence.off(presenceHandler);
}
};
presenceChannel.presence.on(presenceHandler);

/* set up authenticated connection */
clientRealtime = helper.AblyRealtime({ clientId: testClientId, authToken: authToken, transports: [transport] });
clientRealtime.connection.on('connected', function() {
/* get channel, attach, and enter */
var clientChannel = clientRealtime.channels.get('presence0');
clientChannel.attach(function(err) {
if(err) {
test.ok(false, 'Attach failed with error: ' + err);
done();
return;
}
clientChannel.presence.enter();
});
});
var exitOnState = function(state) {
clientRealtime.connection.on(state, function () {
test.ok(false, transport + ' connection to server failed');
done();
});
};
exitOnState('failed');
exitOnState('suspended');
} catch(e) {
test.ok(false, 'presence.enter5 failed with exception: ' + e.stack);
done();
}
};

/*
* Attach to channel, enter presence channel with data but no callback and await entered event
*/
exports.enter6 = function(test) {
var clientRealtime;
var isDone = false, done = function() {
if(!isDone) {
isDone = true;
setTimeout(function() {
test.done(); clientRealtime.close();
}, 3000);
}
};
try {
var transport = 'web_socket';

test.expect(1);
/* listen for the enter event, test is complete when received */
var presenceHandler = function() {
if(this.event == 'enter') {
test.ok(true, 'Presence event received');
done();
presenceChannel.presence.off(presenceHandler);
}
};
presenceChannel.presence.on(presenceHandler);

/* set up authenticated connection */
clientRealtime = helper.AblyRealtime({ clientId: testClientId, authToken: authToken, transports: [transport] });
clientRealtime.connection.on('connected', function() {
/* get channel, attach, and enter */
var clientChannel = clientRealtime.channels.get('presence0');
clientChannel.attach(function(err) {
if(err) {
test.ok(false, 'Attach failed with error: ' + err);
done();
return;
}
clientChannel.presence.enter('Test client data (enter6)');
});
});
var exitOnState = function(state) {
clientRealtime.connection.on(state, function () {
test.ok(false, transport + ' connection to server failed');
done();
});
};
exitOnState('failed');
exitOnState('suspended');
} catch(e) {
test.ok(false, 'presence.enter6 failed with exception: ' + e.stack);
done();
}
};

/*
* Attach to channel, enter+leave presence channel and await leave event
*/
Expand Down

0 comments on commit a248d05

Please sign in to comment.