Skip to content

Commit

Permalink
core: add test for network errors
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Sep 18, 2023
1 parent f06ce4c commit 8a99adc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions tests/base/local_wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const baseConfig = {
defaultParams: {
assert: 'user',
},
maxRetries: 1,
retryPause: 100,
};

let bot = new Mwn({
Expand Down
11 changes: 11 additions & 0 deletions tests/core.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { Request, Response } = require('../build/core');
const logger = require('../build/log');
const { MwnError } = require('../build/error');
const nock = require('nock');

const { expect, Mwn } = require('./base/test_base');
const { bot, setup, teardown, sinon } = require('./base/local_wiki');
Expand Down Expand Up @@ -51,6 +52,16 @@ describe('core', function () {
before('logs in and gets token & namespaceInfo', setup);
after('teardown', teardown);

it('logs on network errors and retries them', async function () {
nock('http://localhost:8080/api.php', { allowUnmocked: true }).get(/.*?/).times(1).reply(500, 'body', {});
sinon.spy(console, 'log');
await expect(bot.query({ action: 'query' })).to.be.eventually.deep.eq({ batchcomplete: true });
expect(console.log).to.have.been.calledTwice;
expect(console.log.firstCall.firstArg).to.include('Retrying in ');
expect(console.log.secondCall.firstArg).to.be.instanceOf(Object).that.has.keys('request', 'response');
sinon.restore();
});

// Errors

it('default legacy error format (bc)', async () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('testing for error recoveries', function () {
mockApi(1, maxlagErrorBody); // without Retry-After header
await bot.query({});
expect(retrySpy).to.have.been.called;
expect(sleepStub).to.have.been.calledOnceWith(5000);
expect(sleepStub).to.have.been.calledOnceWith(bot.options.retryPause);
sleepStub.restore();
retrySpy.restore();
});
Expand All @@ -144,7 +144,7 @@ describe('testing for error recoveries', function () {
sleepStub.resolves();
await bot.query({});
expect(retrySpy).to.have.been.called;
expect(sleepStub).to.have.been.calledWith(5000);
expect(sleepStub).to.have.been.calledWith(bot.options.retryPause);
sleepStub.restore();
retrySpy.restore();
});
Expand Down

0 comments on commit 8a99adc

Please sign in to comment.