Skip to content

Commit

Permalink
Convert shared helper to a class
Browse files Browse the repository at this point in the history
Motivation as in 9e28603. To expand a little, in order to implement
ECO-4821 (gathering detailed information about private API usage in the
test suite), we will give each test function access to a unique helper
object which is aware of the identity of the test case in which it is
executing. This helper object will offer a method for recording the fact
that a test function is calling a private API. By using the existing
shared_helper object, we can also record the usage of private APIs by
the various helper methods, access to which is currently funnelled
through the shared_helper.

The couple of methods that I’ve made static (randomString,
whenPromiseSettles) are ones that are very frequently called _and_ which
definitely don’t use any private APIs, so I’ve made them static so that,
when we make the aforementioned change giving each test case its own
helper object, we don’t need to pass a helper around just to enable
these methods to be called. The choice of making just these two methods
static was pretty arbitrary; once we’ve catalogued all of the private
API usage it might turn out there some other ones that can be made
static.
  • Loading branch information
lawrence-forooghian committed Jul 24, 2024
1 parent 2b7c2f5 commit 1fa5e13
Show file tree
Hide file tree
Showing 39 changed files with 749 additions and 682 deletions.
4 changes: 3 additions & 1 deletion test/browser/connection.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

define(['shared_helper', 'chai'], function (helper, chai) {
define(['shared_helper', 'chai'], function (Helper, chai) {
const helper = new Helper();

var { expect, assert } = chai;
var transportPreferenceName = 'ably-transport-preference';

Expand Down
8 changes: 5 additions & 3 deletions test/browser/http.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) {
const helper = new Helper();

var rest;
var expect = chai.expect;

Expand Down Expand Up @@ -35,7 +37,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
/** @nospec */
it('Should succeed in using fetch to publish a message', function (done) {
const channel = rest.channels.get('http_test_channel');
helper.whenPromiseSettles(channel.publish('test', 'Testing fetch support'), (err) => {
Helper.whenPromiseSettles(channel.publish('test', 'Testing fetch support'), (err) => {
expect(err).to.not.exist;
done();
});
Expand All @@ -49,7 +51,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
*/
it('Should pass errors correctly', function (done) {
const channel = rest.channels.get('');
helper.whenPromiseSettles(channel.publish('test', 'Invalid message'), (err) => {
Helper.whenPromiseSettles(channel.publish('test', 'Invalid message'), (err) => {
expect(err).to.exist;
done();
});
Expand Down
11 changes: 6 additions & 5 deletions test/browser/modular.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import {
MessageInteractions,
} from '../../build/modular/index.mjs';

function registerAblyModularTests(helper) {
function registerAblyModularTests(Helper) {
describe('browser/modular', function () {
this.timeout(10 * 1000);
const helper = new Helper();
const expect = chai.expect;
const BufferUtils = BaseRest.Platform.BufferUtils;
const loadTestData = async (dataPath) => {
Expand Down Expand Up @@ -638,7 +639,7 @@ function registerAblyModularTests(helper) {

const txClient = new BaseRealtime(
helper.ablyClientOptions({
clientId: helper.randomString(),
clientId: Helper.randomString(),
plugins: {
WebSocketTransport,
FetchRequest,
Expand Down Expand Up @@ -681,7 +682,7 @@ function registerAblyModularTests(helper) {
const rxChannel = rxClient.channels.get('channel');

await monitorConnectionThenCloseAndFinish(async () => {
const txClientId = helper.randomString();
const txClientId = Helper.randomString();
const txClient = new BaseRealtime(
helper.ablyClientOptions({
clientId: txClientId,
Expand Down Expand Up @@ -958,8 +959,8 @@ function registerAblyModularTests(helper) {
// This function is called by browser_setup.js once `require` is available
window.registerAblyModularTests = async () => {
return new Promise((resolve) => {
require(['shared_helper'], (helper) => {
registerAblyModularTests(helper);
require(['shared_helper'], (Helper) => {
registerAblyModularTests(Helper);
resolve();
});
});
Expand Down
4 changes: 3 additions & 1 deletion test/browser/push.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

define(['ably', 'shared_helper', 'chai', 'push'], function (Ably, helper, chai, PushPlugin) {
define(['ably', 'shared_helper', 'chai', 'push'], function (Ably, Helper, chai, PushPlugin) {
const helper = new Helper();

const expect = chai.expect;
const swUrl = '/push_sw.js';
let rest;
Expand Down
8 changes: 5 additions & 3 deletions test/browser/simple.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) {
const helper = new Helper();

var expect = chai.expect;

describe('browser/simple', function () {
Expand Down Expand Up @@ -71,7 +73,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
ably.connection.on('connected', function () {
connectionTimeout.stop();
heartbeatTimeout = failWithin(25, done, ably, 'wait for heartbeat');
helper.whenPromiseSettles(ably.connection.ping(), function (err) {
Helper.whenPromiseSettles(ably.connection.ping(), function (err) {
heartbeatTimeout.stop();
done(err);
ably.close();
Expand Down Expand Up @@ -115,7 +117,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
receiveMessagesTimeout = failWithin(15, done, ably, 'wait for published messages to be received');

timer = setInterval(function () {
helper.whenPromiseSettles(channel.publish('event0', 'Hello world at: ' + new Date()), function (err) {
Helper.whenPromiseSettles(channel.publish('event0', 'Hello world at: ' + new Date()), function (err) {
sentCbCount++;
checkFinish();
});
Expand Down
Loading

0 comments on commit 1fa5e13

Please sign in to comment.