Skip to content

Commit

Permalink
feat!: replacing the isRunning method with status (#459)
Browse files Browse the repository at this point in the history
* feat!: replacing the isRunning method with getStatus

* chore: putting debugger back

* chore: putting test back
  • Loading branch information
nicholasgriffintn committed Mar 11, 2024
1 parent 2d342cd commit 9f07383
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 27 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,15 @@ By default, the value of `abort` is set to `false` which means pre existing requ

`consumer.stop({ abort: true })`

### `consumer.isRunning`
### `consumer.status`

Returns the current polling state of the consumer: `true` if it is actively polling, `false` if it is not.
Returns the current status of the consumer.

- `isRunning` - `true` if the consumer has been started and not stopped, `false` if was not started or if it was stopped.
- `isPolling` - `true` if the consumer is actively polling, `false` if it is not.

> **Note:**
> This method is not available in versions before v9.0.0 and replaced the method `isRunning` to supply both running and polling states.
### `consumer.updateOption(option, value)`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"lcov": "c8 mocha && c8 report --reporter=lcov",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --fix",
"format": "prettier --loglevel warn --write \"**/*.{js,json,jsx,md,ts,tsx,html}\"",
"format": "prettier --log-level warn --write \"**/*.{js,json,jsx,md,ts,tsx,html}\"",
"format:check": "prettier --check \"**/*.{js,json,jsx,md,ts,tsx,html}\"",
"posttest": "npm run lint && npm run format:check",
"generate-docs": "typedoc"
Expand Down
15 changes: 11 additions & 4 deletions src/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Consumer extends TypedEventEmitter {
private pollingWaitTimeMs: number;
private pollingCompleteWaitTimeMs: number;
private heartbeatInterval: number;
private isPolling: boolean;
private isPolling = false;
private stopRequestedAtTimestamp: number;
public abortController: AbortController;

Expand Down Expand Up @@ -174,10 +174,17 @@ export class Consumer extends TypedEventEmitter {
}

/**
* Returns the current polling state of the consumer: `true` if it is actively polling, `false` if it is not.
* Returns the current status of the consumer.
* This includes whether it is running or currently polling.
*/
public get isRunning(): boolean {
return !this.stopped;
public get status(): {
isRunning: boolean;
isPolling: boolean;
} {
return {
isRunning: !this.stopped,
isPolling: this.isPolling
};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/features/step_definitions/gracefulShutdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Then('the application is stopped while messages are in flight', async () => {

consumer.stop();

assert.strictEqual(consumer.isRunning, false);
assert.strictEqual(consumer.status.isRunning, false);
});

Then(
Expand Down
10 changes: 4 additions & 6 deletions test/features/step_definitions/handleMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ Given('a message is sent to the SQS queue', async () => {
Then('the message should be consumed without error', async () => {
consumer.start();

const isRunning = consumer.isRunning;

assert.strictEqual(isRunning, true);
assert.strictEqual(consumer.status.isRunning, true);

await pEvent(consumer, 'response_processed');

consumer.stop();
assert.strictEqual(consumer.isRunning, false);
assert.strictEqual(consumer.status.isRunning, false);

const size = await producer.queueSize();
assert.strictEqual(size, 0);
Expand Down Expand Up @@ -61,7 +59,7 @@ Then(
async () => {
consumer.start();

assert.strictEqual(consumer.isRunning, true);
assert.strictEqual(consumer.status.isRunning, true);

await pEvent(consumer, 'message_received');
const size = await producer.queueSize();
Expand All @@ -82,7 +80,7 @@ Then(

consumer.stop();

assert.strictEqual(consumer.isRunning, false);
assert.strictEqual(consumer.status.isRunning, false);
}
);

Expand Down
12 changes: 4 additions & 8 deletions test/features/step_definitions/handleMessageBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ Given('a message batch is sent to the SQS queue', async () => {
Then('the message batch should be consumed without error', async () => {
consumer.start();

const isRunning = consumer.isRunning;

assert.strictEqual(isRunning, true);
assert.strictEqual(consumer.status.isRunning, true);

await pEvent(consumer, 'response_processed');

consumer.stop();
assert.strictEqual(consumer.isRunning, false);
assert.strictEqual(consumer.status.isRunning, false);

const size = await producer.queueSize();
assert.strictEqual(size, 0);
Expand Down Expand Up @@ -61,9 +59,7 @@ Then(
async () => {
consumer.start();

const isRunning = consumer.isRunning;

assert.strictEqual(isRunning, true);
assert.strictEqual(consumer.status.isRunning, true);

await pEvent(consumer, 'message_received');

Expand All @@ -76,7 +72,7 @@ Then(
assert.strictEqual(size2, 0);

consumer.stop();
assert.strictEqual(consumer.isRunning, false);
assert.strictEqual(consumer.status.isRunning, false);
}
);

Expand Down
37 changes: 32 additions & 5 deletions test/tests/consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1556,17 +1556,44 @@ describe('Consumer', () => {
});
});

describe('isRunning', async () => {
it('returns true if the consumer is polling', () => {
describe('status', async () => {
it('returns the defaults before the consumer is started', () => {
assert.isFalse(consumer.status.isRunning);
assert.isFalse(consumer.status.isPolling);
});

it('returns true for `isRunning` if the consumer has not been stopped', () => {
consumer.start();
assert.isTrue(consumer.isRunning);
assert.isTrue(consumer.status.isRunning);
consumer.stop();
});

it('returns false if the consumer is not polling', () => {
it('returns false for `isRunning` if the consumer has been stopped', () => {
consumer.start();
consumer.stop();
assert.isFalse(consumer.status.isRunning);
});

it('returns true for `isPolling` if the consumer is polling for messages', async () => {
sqs.send.withArgs(mockReceiveMessage).resolves({
Messages: [
{ MessageId: '1', ReceiptHandle: 'receipt-handle-1', Body: 'body-1' }
]
});
consumer = new Consumer({
queueUrl: QUEUE_URL,
region: REGION,
handleMessage: () => new Promise((resolve) => setTimeout(resolve, 20)),
sqs
});

consumer.start();
await Promise.all([clock.tickAsync(1)]);
assert.isTrue(consumer.status.isPolling);
consumer.stop();
assert.isFalse(consumer.isRunning);
assert.isTrue(consumer.status.isPolling);
await Promise.all([clock.tickAsync(21)]);
assert.isFalse(consumer.status.isPolling);
});
});

Expand Down

0 comments on commit 9f07383

Please sign in to comment.