Skip to content

Commit 412a779

Browse files
committed
implement prose test #2; re-arrange export of TokenBucket type
1 parent 2bce2a4 commit 412a779

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/operations/execute_operation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ async function executeOperationWithRetries<
284284
topology.tokenBucket.deposit(RETRY_COST);
285285
}
286286

287+
// Preserve the original error once a write has been performed.
288+
// Only update to the latest error if no writes were performed.
287289
if (error == null) {
288290
error = operationError;
289291
} else {

test/integration/client-backpressure/client-backpressure.prose.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { expect } from 'chai';
22
import * as sinon from 'sinon';
33

4-
import { type Collection, type MongoClient, MongoServerError } from '../../../src';
4+
import {
5+
type Collection,
6+
INITIAL_TOKEN_BUCKET_SIZE,
7+
type MongoClient,
8+
MongoServerError
9+
} from '../../mongodb';
510
import { clearFailPoint, configureFailPoint, measureDuration } from '../../tools/utils';
611

712
describe('Client Backpressure (Prose)', function () {
@@ -58,4 +63,19 @@ describe('Client Backpressure (Prose)', function () {
5863
expect(durationBackoff - durationNoBackoff).to.be.within(3100 - 1000, 3100 + 1000);
5964
}
6065
);
66+
67+
it('Test 2: Token Bucket capacity is Enforced', async () => {
68+
// 1-2. Assert that the client's retry token bucket is at full capacity and that the capacity
69+
// is DEFAULT_RETRY_TOKEN_CAPACITY.
70+
const tokenBucket = client.topology.tokenBucket;
71+
expect(tokenBucket).to.have.property('budget', INITIAL_TOKEN_BUCKET_SIZE);
72+
expect(tokenBucket).to.have.property('capacity', INITIAL_TOKEN_BUCKET_SIZE);
73+
74+
// 3. Execute a successful ping command.
75+
await client.db('admin').command({ ping: 1 });
76+
77+
// 4. Assert that the successful command did not increase the number of tokens in the bucket
78+
// above DEFAULT_RETRY_TOKEN_CAPACITY.
79+
expect(tokenBucket).to.have.property('budget').that.is.at.most(INITIAL_TOKEN_BUCKET_SIZE);
80+
});
6181
});

test/mongodb.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export * from '../src/sdam/topology_description';
128128
export * from '../src/sessions';
129129
export * from '../src/sort';
130130
export * from '../src/timeout';
131+
export * from '../src/token_bucket';
131132
export * from '../src/transactions';
132133
export * from '../src/utils';
133134
export * from '../src/write_concern';

0 commit comments

Comments
 (0)