Skip to content

Commit

Permalink
fix: start doesn't throw (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr authored Jun 25, 2023
1 parent 046491b commit aeb95c4
Show file tree
Hide file tree
Showing 4 changed files with 600 additions and 583 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "squiss-ts",
"version": "5.2.0",
"version": "5.2.1",
"description": "High-volume SQS poller",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -48,9 +48,9 @@
},
"homepage": "https://github.com/PruvoNet/squiss-ts#readme",
"dependencies": {
"@aws-sdk/client-s3": "^3.348.0",
"@aws-sdk/client-sqs": "^3.348.0",
"@aws-sdk/types": "^3.347.0",
"@aws-sdk/client-s3": "^3.359.0",
"@aws-sdk/client-sqs": "^3.359.0",
"@aws-sdk/types": "^3.357.0",
"linked-list": "2.1.0",
"ts-type-guards": "^0.7.0",
"uuid": "^9.0.0"
Expand All @@ -75,7 +75,7 @@
"source-map-support": "^0.5.21",
"ts-node": "^10.9.1",
"tslint": "^6.1.3",
"typescript": "^5.0.4"
"typescript": "^5.1.3"
},
"nyc": {
"extends": "@istanbuljs/nyc-config-typescript",
Expand Down
12 changes: 7 additions & 5 deletions src/Squiss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ export class Squiss extends (EventEmitter as new() => SquissEmitter) {
this._inFlight--;
if (this._paused && this._slotsAvailable()) {
this._paused = false;
this._startPoller();
this._startPoller()
.catch((e: Error) => {
this.emit('error', e);
});
}
msg.emit('handled');
this.emit('handled', msg);
Expand Down Expand Up @@ -492,10 +495,9 @@ export class Squiss extends (EventEmitter as new() => SquissEmitter) {
private _startPoller(): Promise<void> {
return this._initTimeoutExtender()
.then(() => this.getQueueUrl())
.then((queueUrl) => this._getBatch(queueUrl))
.catch((e: Error) => {
this.emit('error', e);
});
.then((queueUrl): void => {
this._getBatch(queueUrl);
})
}

private _deleteXMessages(x?: number) {
Expand Down
40 changes: 31 additions & 9 deletions src/test/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import {IMessageOpts } from '../../Message';
// @ts-ignore
import * as sinon from 'sinon';
import * as chai from 'chai';
import {GetQueueUrlCommandInput, SendMessageBatchResult, SQS} from '@aws-sdk/client-sqs'
import {GetQueueUrlCommandInput, ReceiveMessageRequest, SendMessageBatchResult, SQS} from '@aws-sdk/client-sqs'
import {S3} from '@aws-sdk/client-s3'
import {EventEmitter} from 'events';
import {Blobs, S3Stub} from '../stubs/S3Stub';
import {HttpHandlerOptions} from '@aws-sdk/types';

const should = chai.should();
let inst: Squiss | null = null;
Expand Down Expand Up @@ -479,6 +480,33 @@ describe('index', () => {
inst!.inFlight.should.equal(3);
});
});
it('emits error when start polling fails', () => {
const errorSpy = sinon.spy();
inst = new SquissPatched({queueUrl: 'foo', deleteWaitMs: 1, maxInFlight: 5} as ISquissOptions);
const sqsStub = new SQSStub(5);
inst!.sqs = sqsStub as any as SQS;
let receiveMessageCallCount = 0;
const originalReceiveMessage = sqsStub.receiveMessage;
sqsStub.receiveMessage = (params?: ReceiveMessageRequest, opts?: HttpHandlerOptions) => {
receiveMessageCallCount++;
if (receiveMessageCallCount === 2) {
throw new Error('test');
}
return originalReceiveMessage.call(sqsStub, params, opts);
};
inst!.on('error', errorSpy);
return inst!.start()
.then(() => wait())
.then(async () => {
inst!.inFlight.should.equal(5);
await inst!.handledMessage(new EventEmitter() as any);
await wait(1);
}).then(() => {
inst!.inFlight.should.equal(4);
errorSpy.should.be.calledOnce();
errorSpy.should.be.calledWith(sinon.match.instanceOf(Error));
});
});
it('pauses polling when maxInFlight is reached; resumes after', () => {
const msgSpy = sinon.spy();
const maxSpy = sinon.spy();
Expand Down Expand Up @@ -770,18 +798,12 @@ describe('index', () => {
msgSpy.should.be.calledTwice();
});
});
it('emits error when GetQueueURL call fails', () => {
const spy = sinon.spy();
it('throws error when GetQueueURL call fails on start', () => {
inst = new SquissPatched({queueName: 'foo'} as ISquissOptions);
(inst!.sqs as any as SQSStub).getQueueUrl = (params: GetQueueUrlCommandInput) => {
return Promise.reject(new Error('test'));
};
inst!.on('error', spy);
inst!.start();
return wait().then(() => {
spy.should.be.calledOnce();
spy.should.be.calledWith(sinon.match.instanceOf(Error));
});
return inst!.start().should.be.rejected('not rejected');
});
});
describe('Testing', () => {
Expand Down
Loading

0 comments on commit aeb95c4

Please sign in to comment.