Skip to content

Commit

Permalink
Extract delta tests into a reusable function
Browse files Browse the repository at this point in the history
I’m going to use them inside `modules.test.js` as part of #1492
(removing the ClientOptions.plugins mechanism).
  • Loading branch information
lawrence-forooghian committed Nov 27, 2023
1 parent d9c861f commit b6d860c
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 273 deletions.
1 change: 1 addition & 0 deletions test/common/globals/named_dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ define(function () {
shared_helper: { browser: 'test/common/modules/shared_helper', node: 'test/common/modules/shared_helper' },
async: { browser: 'node_modules/async/lib/async' },
chai: { browser: 'node_modules/chai/chai', node: 'node_modules/chai/chai' },
delta_tests: { browser: 'test/realtime/shared/delta_tests', node: 'test/realtime/shared/delta_tests' },
});
});
275 changes: 2 additions & 273 deletions test/realtime/delta.test.js
Original file line number Diff line number Diff line change
@@ -1,276 +1,5 @@
'use strict';

define(['shared_helper', 'vcdiff-decoder', 'async', 'chai'], function (helper, vcdiffDecoder, async, chai) {
var expect = chai.expect;
var displayError = helper.displayError;
var closeAndFinish = helper.closeAndFinish;
var monitorConnection = helper.monitorConnection;
var whenPromiseSettles = helper.whenPromiseSettles;
var testData = [
{ foo: 'bar', count: 1, status: 'active' },
{ foo: 'bar', count: 2, status: 'active' },
{ foo: 'bar', count: 2, status: 'inactive' },
{ foo: 'bar', count: 3, status: 'inactive' },
{ foo: 'bar', count: 3, status: 'active' },
];

function equals(a, b) {
return JSON.stringify(a) === JSON.stringify(b);
}

function getTestVcdiffDecoder() {
return {
numberOfCalls: 0,
decode: function (delta, base) {
this.numberOfCalls++;
return vcdiffDecoder.decode(delta, base);
},
};
}

describe('realtime/delta', function () {
this.timeout(60 * 1000);

before(function (done) {
helper.setupApp(function (err) {
if (err) {
done(err);
}
done();
});
});

it('deltaPlugin', function (done) {
var testName = 'deltaPlugin';
try {
var testVcdiffDecoder = getTestVcdiffDecoder();
var realtime = helper.AblyRealtime({
plugins: {
vcdiff: testVcdiffDecoder,
},
});
var channel = realtime.channels.get(testName, { params: { delta: 'vcdiff' } });

whenPromiseSettles(channel.attach(), function (err) {
if (err) {
closeAndFinish(done, realtime, err);
}

channel.on('attaching', function (stateChange) {
done(
new Error(
'Channel reattaching, presumably due to decode failure; reason =' + displayError(stateChange.reason)
)
);
});

channel.subscribe(function (message) {
try {
var index = Number(message.name);
expect(equals(testData[index], message.data), 'Check message.data').to.be.ok;

if (index === testData.length - 1) {
expect(testVcdiffDecoder.numberOfCalls).to.equal(testData.length - 1, 'Check number of delta messages');
closeAndFinish(done, realtime);
}
} catch (err) {
closeAndFinish(done, realtime, err);
}
});

async.timesSeries(testData.length, function (i, cb) {
channel.publish(i.toString(), testData[i], cb);
});
});

monitorConnection(done, realtime);
} catch (err) {
closeAndFinish(done, realtime, err);
}
});

it('unusedPlugin', function (done) {
var testName = 'unusedPlugin';
try {
var testVcdiffDecoder = getTestVcdiffDecoder();
var realtime = helper.AblyRealtime({
plugins: {
vcdiff: testVcdiffDecoder,
},
});
var channel = realtime.channels.get(testName);

whenPromiseSettles(channel.attach(), function (err) {
if (err) {
closeAndFinish(done, realtime, err);
}
channel.subscribe(function (message) {
try {
var index = Number(message.name);
expect(equals(testData[index], message.data), 'Check message.data').to.be.ok;

if (index === testData.length - 1) {
expect(testVcdiffDecoder.numberOfCalls).to.equal(0, 'Check number of delta messages');
closeAndFinish(done, realtime);
}
} catch (err) {
closeAndFinish(done, realtime, err);
}
});

async.timesSeries(testData.length, function (i, cb) {
channel.publish(i.toString(), testData[i], cb);
});
});

monitorConnection(done, realtime);
} catch (err) {
closeAndFinish(done, realtime, err);
}
});

it('lastMessageNotFoundRecovery', function (done) {
var testName = 'lastMessageNotFoundRecovery';
try {
var testVcdiffDecoder = getTestVcdiffDecoder();
var realtime = helper.AblyRealtime({
plugins: {
vcdiff: testVcdiffDecoder,
},
});
var channel = realtime.channels.get(testName, { params: { delta: 'vcdiff' } });

whenPromiseSettles(channel.attach(), function (err) {
if (err) {
closeAndFinish(done, realtime, err);
}
channel.subscribe(function (message) {
var index = Number(message.name);
try {
expect(equals(testData[index], message.data), 'Check message.data').to.be.ok;
} catch (err) {
closeAndFinish(done, realtime, err);
}

if (index === 1) {
/* Simulate issue */
channel._lastPayload.messageId = null;
channel.once('attaching', function (stateChange) {
try {
expect(stateChange.reason.code).to.equal(40018, 'Check error code passed through per RTL18c');
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}
channel.on('attaching', function (stateChange) {
closeAndFinish(
done,
realtime,
new Error('Check no further decode failures; reason =' + displayError(stateChange.reason))
);
});
});
} else if (index === testData.length - 1) {
try {
expect(testVcdiffDecoder.numberOfCalls).to.equal(testData.length - 2, 'Check number of delta messages');
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}
closeAndFinish(done, realtime);
}
});

async.timesSeries(testData.length, function (i, cb) {
channel.publish(i.toString(), testData[i], cb);
});
});

monitorConnection(done, realtime);
} catch (err) {
closeAndFinish(done, realtime, err);
}
});

it('deltaDecodeFailureRecovery', function (done) {
var testName = 'deltaDecodeFailureRecovery';
try {
var failingTestVcdiffDecoder = {
decode: function (delta, base) {
throw new Error('Failed to decode delta.');
},
};

var realtime = helper.AblyRealtime({
plugins: {
vcdiff: failingTestVcdiffDecoder,
},
});
var channel = realtime.channels.get(testName, { params: { delta: 'vcdiff' } });

whenPromiseSettles(channel.attach(), function (err) {
if (err) {
closeAndFinish(done, realtime, err);
}
channel.on('attaching', function (stateChange) {
try {
expect(stateChange.reason.code).to.equal(40018, 'Check error code passed through per RTL18c');
} catch (err) {
closeAndFinish(done, realtime, err);
}
});
channel.subscribe(function (message) {
var index = Number(message.name);
try {
expect(equals(testData[index], message.data), 'Check message.data').to.be.ok;
} catch (err) {
closeAndFinish(done, realtime, err);
}

if (index === testData.length - 1) {
closeAndFinish(done, realtime);
}
});

async.timesSeries(testData.length, function (i, cb) {
channel.publish(i.toString(), testData[i], cb);
});
});

monitorConnection(done, realtime);
} catch (err) {
closeAndFinish(done, realtime, err);
}
});

/* Check that channel becomes failed if we get deltas when we don't have a vcdiff plugin */
it('noPlugin', function (done) {
try {
var realtime = helper.AblyRealtime();
var channel = realtime.channels.get('noPlugin', { params: { delta: 'vcdiff' } });

whenPromiseSettles(channel.attach(), function (err) {
if (err) {
closeAndFinish(done, realtime, err);
}
channel.once('failed', function (stateChange) {
try {
expect(stateChange.reason.code).to.equal(40019, 'Check error code');
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}
closeAndFinish(done, realtime);
});
async.timesSeries(testData.length, function (i, cb) {
channel.publish(i.toString(), testData[i], cb);
});
});

monitorConnection(done, realtime);
} catch (err) {
closeAndFinish(done, realtime, err);
}
});
});
define(['delta_tests'], function (registerDeltaTests) {
registerDeltaTests('realtime/delta');
});
Loading

0 comments on commit b6d860c

Please sign in to comment.