Skip to content

Commit 9f07383

Browse files
feat!: replacing the isRunning method with status (#459)
* feat!: replacing the isRunning method with getStatus * chore: putting debugger back * chore: putting test back
1 parent 2d342cd commit 9f07383

File tree

7 files changed

+61
-27
lines changed

7 files changed

+61
-27
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,15 @@ By default, the value of `abort` is set to `false` which means pre existing requ
125125
126126
`consumer.stop({ abort: true })`
127127
128-
### `consumer.isRunning`
128+
### `consumer.status`
129129
130-
Returns the current polling state of the consumer: `true` if it is actively polling, `false` if it is not.
130+
Returns the current status of the consumer.
131+
132+
- `isRunning` - `true` if the consumer has been started and not stopped, `false` if was not started or if it was stopped.
133+
- `isPolling` - `true` if the consumer is actively polling, `false` if it is not.
134+
135+
> **Note:**
136+
> This method is not available in versions before v9.0.0 and replaced the method `isRunning` to supply both running and polling states.
131137
132138
### `consumer.updateOption(option, value)`
133139

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"lcov": "c8 mocha && c8 report --reporter=lcov",
2222
"lint": "eslint . --ext .ts",
2323
"lint:fix": "eslint . --fix",
24-
"format": "prettier --loglevel warn --write \"**/*.{js,json,jsx,md,ts,tsx,html}\"",
24+
"format": "prettier --log-level warn --write \"**/*.{js,json,jsx,md,ts,tsx,html}\"",
2525
"format:check": "prettier --check \"**/*.{js,json,jsx,md,ts,tsx,html}\"",
2626
"posttest": "npm run lint && npm run format:check",
2727
"generate-docs": "typedoc"

src/consumer.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class Consumer extends TypedEventEmitter {
5656
private pollingWaitTimeMs: number;
5757
private pollingCompleteWaitTimeMs: number;
5858
private heartbeatInterval: number;
59-
private isPolling: boolean;
59+
private isPolling = false;
6060
private stopRequestedAtTimestamp: number;
6161
public abortController: AbortController;
6262

@@ -174,10 +174,17 @@ export class Consumer extends TypedEventEmitter {
174174
}
175175

176176
/**
177-
* Returns the current polling state of the consumer: `true` if it is actively polling, `false` if it is not.
177+
* Returns the current status of the consumer.
178+
* This includes whether it is running or currently polling.
178179
*/
179-
public get isRunning(): boolean {
180-
return !this.stopped;
180+
public get status(): {
181+
isRunning: boolean;
182+
isPolling: boolean;
183+
} {
184+
return {
185+
isRunning: !this.stopped,
186+
isPolling: this.isPolling
187+
};
181188
}
182189

183190
/**

test/features/step_definitions/gracefulShutdown.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Then('the application is stopped while messages are in flight', async () => {
3131

3232
consumer.stop();
3333

34-
assert.strictEqual(consumer.isRunning, false);
34+
assert.strictEqual(consumer.status.isRunning, false);
3535
});
3636

3737
Then(

test/features/step_definitions/handleMessage.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ Given('a message is sent to the SQS queue', async () => {
2626
Then('the message should be consumed without error', async () => {
2727
consumer.start();
2828

29-
const isRunning = consumer.isRunning;
30-
31-
assert.strictEqual(isRunning, true);
29+
assert.strictEqual(consumer.status.isRunning, true);
3230

3331
await pEvent(consumer, 'response_processed');
3432

3533
consumer.stop();
36-
assert.strictEqual(consumer.isRunning, false);
34+
assert.strictEqual(consumer.status.isRunning, false);
3735

3836
const size = await producer.queueSize();
3937
assert.strictEqual(size, 0);
@@ -61,7 +59,7 @@ Then(
6159
async () => {
6260
consumer.start();
6361

64-
assert.strictEqual(consumer.isRunning, true);
62+
assert.strictEqual(consumer.status.isRunning, true);
6563

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

8381
consumer.stop();
8482

85-
assert.strictEqual(consumer.isRunning, false);
83+
assert.strictEqual(consumer.status.isRunning, false);
8684
}
8785
);
8886

test/features/step_definitions/handleMessageBatch.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ Given('a message batch is sent to the SQS queue', async () => {
2626
Then('the message batch should be consumed without error', async () => {
2727
consumer.start();
2828

29-
const isRunning = consumer.isRunning;
30-
31-
assert.strictEqual(isRunning, true);
29+
assert.strictEqual(consumer.status.isRunning, true);
3230

3331
await pEvent(consumer, 'response_processed');
3432

3533
consumer.stop();
36-
assert.strictEqual(consumer.isRunning, false);
34+
assert.strictEqual(consumer.status.isRunning, false);
3735

3836
const size = await producer.queueSize();
3937
assert.strictEqual(size, 0);
@@ -61,9 +59,7 @@ Then(
6159
async () => {
6260
consumer.start();
6361

64-
const isRunning = consumer.isRunning;
65-
66-
assert.strictEqual(isRunning, true);
62+
assert.strictEqual(consumer.status.isRunning, true);
6763

6864
await pEvent(consumer, 'message_received');
6965

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

7874
consumer.stop();
79-
assert.strictEqual(consumer.isRunning, false);
75+
assert.strictEqual(consumer.status.isRunning, false);
8076
}
8177
);
8278

test/tests/consumer.test.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -1556,17 +1556,44 @@ describe('Consumer', () => {
15561556
});
15571557
});
15581558

1559-
describe('isRunning', async () => {
1560-
it('returns true if the consumer is polling', () => {
1559+
describe('status', async () => {
1560+
it('returns the defaults before the consumer is started', () => {
1561+
assert.isFalse(consumer.status.isRunning);
1562+
assert.isFalse(consumer.status.isPolling);
1563+
});
1564+
1565+
it('returns true for `isRunning` if the consumer has not been stopped', () => {
15611566
consumer.start();
1562-
assert.isTrue(consumer.isRunning);
1567+
assert.isTrue(consumer.status.isRunning);
15631568
consumer.stop();
15641569
});
15651570

1566-
it('returns false if the consumer is not polling', () => {
1571+
it('returns false for `isRunning` if the consumer has been stopped', () => {
1572+
consumer.start();
1573+
consumer.stop();
1574+
assert.isFalse(consumer.status.isRunning);
1575+
});
1576+
1577+
it('returns true for `isPolling` if the consumer is polling for messages', async () => {
1578+
sqs.send.withArgs(mockReceiveMessage).resolves({
1579+
Messages: [
1580+
{ MessageId: '1', ReceiptHandle: 'receipt-handle-1', Body: 'body-1' }
1581+
]
1582+
});
1583+
consumer = new Consumer({
1584+
queueUrl: QUEUE_URL,
1585+
region: REGION,
1586+
handleMessage: () => new Promise((resolve) => setTimeout(resolve, 20)),
1587+
sqs
1588+
});
1589+
15671590
consumer.start();
1591+
await Promise.all([clock.tickAsync(1)]);
1592+
assert.isTrue(consumer.status.isPolling);
15681593
consumer.stop();
1569-
assert.isFalse(consumer.isRunning);
1594+
assert.isTrue(consumer.status.isPolling);
1595+
await Promise.all([clock.tickAsync(21)]);
1596+
assert.isFalse(consumer.status.isPolling);
15701597
});
15711598
});
15721599

0 commit comments

Comments
 (0)