Skip to content

Commit 1cdf394

Browse files
authored
[Service Bus] maxMessageCount validation updates (Azure#12453)
1 parent 79b0fdf commit 1cdf394

File tree

6 files changed

+152
-172
lines changed

6 files changed

+152
-172
lines changed

sdk/servicebus/service-bus/src/core/managementClient.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { LinkEntity, RequestResponseLinkOptions } from "./linkEntity";
3535
import { managementClientLogger, receiverLogger, senderLogger, ServiceBusLogger } from "../log";
3636
import { toBuffer } from "../util/utils";
3737
import {
38+
InvalidMaxMessageCountError,
3839
throwErrorIfConnectionClosed,
3940
throwTypeErrorIfParameterIsEmptyString,
4041
throwTypeErrorIfParameterMissing,
@@ -384,7 +385,7 @@ export class ManagementClient extends LinkEntity<RequestResponseLink> {
384385
* @returns Promise<ReceivedSBMessage[]>
385386
*/
386387
async peek(
387-
messageCount?: number,
388+
messageCount: number,
388389
options?: OperationOptionsBase & SendManagementRequestOptions
389390
): Promise<ServiceBusReceivedMessage[]> {
390391
throwErrorIfConnectionClosed(this._context);
@@ -411,7 +412,7 @@ export class ManagementClient extends LinkEntity<RequestResponseLink> {
411412
*/
412413
async peekMessagesBySession(
413414
sessionId: string,
414-
messageCount?: number,
415+
messageCount: number,
415416
options?: OperationOptionsBase & SendManagementRequestOptions
416417
): Promise<ServiceBusReceivedMessage[]> {
417418
throwErrorIfConnectionClosed(this._context);
@@ -433,7 +434,7 @@ export class ManagementClient extends LinkEntity<RequestResponseLink> {
433434
*/
434435
async peekBySequenceNumber(
435436
fromSequenceNumber: Long,
436-
maxMessageCount?: number,
437+
maxMessageCount: number,
437438
sessionId?: string,
438439
options?: OperationOptionsBase & SendManagementRequestOptions
439440
): Promise<ServiceBusReceivedMessage[]> {
@@ -445,13 +446,20 @@ export class ManagementClient extends LinkEntity<RequestResponseLink> {
445446
throwTypeErrorIfParameterNotLong(connId, "fromSequenceNumber", fromSequenceNumber);
446447

447448
// Checks for maxMessageCount
448-
if (maxMessageCount !== undefined) {
449-
throwTypeErrorIfParameterTypeMismatch(connId, "maxMessageCount", maxMessageCount, "number");
450-
if (maxMessageCount <= 0) {
451-
return [];
452-
}
453-
} else {
454-
maxMessageCount = 1;
449+
throwTypeErrorIfParameterMissing(
450+
this._context.connectionId,
451+
"maxMessageCount",
452+
maxMessageCount
453+
);
454+
throwTypeErrorIfParameterTypeMismatch(
455+
this._context.connectionId,
456+
"maxMessageCount",
457+
maxMessageCount,
458+
"number"
459+
);
460+
461+
if (isNaN(maxMessageCount) || maxMessageCount < 1) {
462+
throw new TypeError(InvalidMaxMessageCountError);
455463
}
456464

457465
const messageList: ServiceBusReceivedMessage[] = [];

sdk/servicebus/service-bus/src/receivers/receiver.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import { ConnectionContext } from "../connectionContext";
1515
import {
1616
getAlreadyReceivingErrorMsg,
1717
getReceiverClosedErrorMsg,
18+
InvalidMaxMessageCountError,
1819
throwErrorIfConnectionClosed,
1920
throwTypeErrorIfParameterMissing,
2021
throwTypeErrorIfParameterNotLong,
21-
throwErrorIfInvalidOperationOnMessage
22+
throwErrorIfInvalidOperationOnMessage,
23+
throwTypeErrorIfParameterTypeMismatch
2224
} from "../util/errors";
2325
import { OnError, OnMessage, ReceiveOptions } from "../core/messageReceiver";
2426
import { StreamingReceiverInitArgs, StreamingReceiver } from "../core/streamingReceiver";
@@ -457,9 +459,20 @@ export class ServiceBusReceiverImpl implements ServiceBusReceiver {
457459
): Promise<ServiceBusReceivedMessage[]> {
458460
this._throwIfReceiverOrConnectionClosed();
459461
this._throwIfAlreadyReceiving();
462+
throwTypeErrorIfParameterMissing(
463+
this._context.connectionId,
464+
"maxMessageCount",
465+
maxMessageCount
466+
);
467+
throwTypeErrorIfParameterTypeMismatch(
468+
this._context.connectionId,
469+
"maxMessageCount",
470+
maxMessageCount,
471+
"number"
472+
);
460473

461-
if (maxMessageCount == undefined) {
462-
maxMessageCount = 1;
474+
if (isNaN(maxMessageCount) || maxMessageCount < 1) {
475+
throw new TypeError(InvalidMaxMessageCountError);
463476
}
464477

465478
const receiveMessages = async () => {
@@ -552,10 +565,6 @@ export class ServiceBusReceiverImpl implements ServiceBusReceiver {
552565
): Promise<ServiceBusReceivedMessage[]> {
553566
this._throwIfReceiverOrConnectionClosed();
554567

555-
if (maxMessageCount == undefined) {
556-
maxMessageCount = 1;
557-
}
558-
559568
const managementRequestOptions = {
560569
...options,
561570
associatedLinkName: this._getAssociatedReceiverName(),

sdk/servicebus/service-bus/src/receivers/sessionReceiver.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { MessageSession } from "../session/messageSession";
88
import {
99
getAlreadyReceivingErrorMsg,
1010
getReceiverClosedErrorMsg,
11+
InvalidMaxMessageCountError,
1112
throwErrorIfConnectionClosed,
1213
throwTypeErrorIfParameterMissing,
1314
throwTypeErrorIfParameterNotLong,
14-
throwErrorIfInvalidOperationOnMessage
15+
throwErrorIfInvalidOperationOnMessage,
16+
throwTypeErrorIfParameterTypeMismatch
1517
} from "../util/errors";
1618
import { OnError, OnMessage } from "../core/messageReceiver";
1719
import {
@@ -298,10 +300,6 @@ export class ServiceBusSessionReceiverImpl implements ServiceBusSessionReceiver
298300
): Promise<ServiceBusReceivedMessage[]> {
299301
this._throwIfReceiverOrConnectionClosed();
300302

301-
if (maxMessageCount == undefined) {
302-
maxMessageCount = 1;
303-
}
304-
305303
const managementRequestOptions = {
306304
...options,
307305
associatedLinkName: this._messageSession.name,
@@ -381,9 +379,20 @@ export class ServiceBusSessionReceiverImpl implements ServiceBusSessionReceiver
381379
): Promise<ServiceBusReceivedMessage[]> {
382380
this._throwIfReceiverOrConnectionClosed();
383381
this._throwIfAlreadyReceiving();
382+
throwTypeErrorIfParameterMissing(
383+
this._context.connectionId,
384+
"maxMessageCount",
385+
maxMessageCount
386+
);
387+
throwTypeErrorIfParameterTypeMismatch(
388+
this._context.connectionId,
389+
"maxMessageCount",
390+
maxMessageCount,
391+
"number"
392+
);
384393

385-
if (maxMessageCount == undefined) {
386-
maxMessageCount = 1;
394+
if (isNaN(maxMessageCount) || maxMessageCount < 1) {
395+
throw new TypeError(InvalidMaxMessageCountError);
387396
}
388397

389398
const receiveBatchOperationPromise = async () => {

sdk/servicebus/service-bus/src/util/errors.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ import { ReceiveMode } from "../models";
1818
export const entityPathMisMatchError =
1919
"The queue or topic name provided does not match the EntityPath in the connection string passed to the ServiceBusClient constructor.";
2020

21+
/**
22+
* Error message for when maxMessageCount provided is invalid.
23+
*
24+
* @internal
25+
* @ignore
26+
*/
27+
export const InvalidMaxMessageCountError = "'maxMessageCount' must be a number greater than 0.";
28+
2129
/**
2230
* @internal
2331
* @ignore
@@ -108,6 +116,7 @@ export function throwTypeErrorIfParameterMissing(
108116
* @param parameterValue Value of the parameter to type check
109117
* @param expectedType Expected type of the parameter
110118
*/
119+
111120
export function throwTypeErrorIfParameterTypeMismatch(
112121
connectionId: string,
113122
parameterName: string,

sdk/servicebus/service-bus/test/batchReceiver.spec.ts

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -52,74 +52,6 @@ function afterEachTest(): Promise<void> {
5252
}
5353

5454
describe("Batching Receiver", () => {
55-
describe("Batch Receiver - default values", function(): void {
56-
before(() => {
57-
serviceBusClient = createServiceBusClientForTests();
58-
});
59-
60-
after(() => {
61-
return serviceBusClient.test.after();
62-
});
63-
64-
afterEach(async () => {
65-
await afterEachTest();
66-
});
67-
68-
it(noSessionTestClientType + ": maxMessageCount defaults to 1", async function(): Promise<
69-
void
70-
> {
71-
await beforeEachTest(noSessionTestClientType);
72-
const testMessage = TestMessage.getSample();
73-
await sender.sendMessages(testMessage);
74-
75-
// @ts-expect-error
76-
const peekedMsgs = await receiver.peekMessages();
77-
should.equal(peekedMsgs.length, 1, "Unexpected number of messages peeked.");
78-
should.equal(
79-
peekedMsgs[0].body,
80-
testMessage.body,
81-
"Peeked message body is different than expected"
82-
);
83-
84-
// @ts-expect-error
85-
const msgs = await receiver.receiveMessages();
86-
should.equal(msgs.length, 1, "Unexpected number of messages received.");
87-
should.equal(
88-
msgs[0].body,
89-
testMessage.body,
90-
"Received message body is different than expected"
91-
);
92-
await receiver.completeMessage(msgs[0]);
93-
});
94-
95-
it(withSessionTestClientType + ": maxMessageCount defaults to 1", async function(): Promise<
96-
void
97-
> {
98-
await beforeEachTest(withSessionTestClientType);
99-
const testMessage = TestMessage.getSessionSample();
100-
await sender.sendMessages(testMessage);
101-
102-
// @ts-expect-error
103-
const peekedMsgs = await receiver.peekMessages();
104-
should.equal(peekedMsgs.length, 1, "Unexpected number of messages peeked.");
105-
should.equal(
106-
peekedMsgs[0].body,
107-
testMessage.body,
108-
"Peeked message body is different than expected"
109-
);
110-
111-
// @ts-expect-error
112-
const msgs = await receiver.receiveMessages();
113-
should.equal(msgs.length, 1, "Unexpected number of messages received.");
114-
should.equal(
115-
msgs[0].body,
116-
testMessage.body,
117-
"Received message body is different than expected"
118-
);
119-
await receiver.completeMessage(msgs[0]);
120-
});
121-
});
122-
12355
describe("Batch Receiver - Settle message", function(): void {
12456
const maxDeliveryCount = 10;
12557

0 commit comments

Comments
 (0)