Skip to content

Commit

Permalink
[testhub] update uuid imports
Browse files Browse the repository at this point in the history
  • Loading branch information
chradek authored and mikeharder committed May 27, 2020
1 parent b67e5ae commit a156887
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions sdk/eventhub/testhub/commands/sendReceive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,46 @@
import { CommandBuilder } from "yargs";
import { EventHubClient, EventPosition, EventData, delay } from "@azure/event-hubs";
import { log, setCurrentCommand, randomNumberFromInterval } from "../utils/util";
import uuid from "uuid/v4";
import { v4 as uuid } from "uuid";

export const command = "send-receive";

export const describe = "Sends a message to an event hub partition and receives the message " +
export const describe =
"Sends a message to an event hub partition and receives the message " +
"from that eventhub partition, thus verifying that the sent message was received successfully.";

export const builder: CommandBuilder = {
p: {
alias: "partitionId",
describe: "The partition id to send the message and receive from.",
default: "0",
string: true
string: true,
},
g: {
alias: "consumer",
describe: "Consumer group name.",
default: "$default",
string: true
string: true,
},
w: {
alias: "maxwait",
describe: "Max time in seconds to wait before sending the next message. " +
describe:
"Max time in seconds to wait before sending the next message. " +
"A random number between 5 and the provided number",
default: 2000,
number: true
number: true,
},
};


function validateArgs(argv: any) {
if (!argv) {
throw new Error(`argv cannot be null or undefined.`);
}

if (!argv.connStr && (!argv.key || !argv.keyName || !argv.address)) {
throw new Error(`Either provide --conn-str OR (--address "sb://{yournamespace}.servicebus.windows.net" --key-name "<shared-access-key-name>" --key "<shared-access-key-value>")`);
throw new Error(
`Either provide --conn-str OR (--address "sb://{yournamespace}.servicebus.windows.net" --key-name "<shared-access-key-name>" --key "<shared-access-key-value>")`
);
}
}

Expand Down Expand Up @@ -68,7 +71,9 @@ export async function handler(argv: any): Promise<void> {
}
client1 = EventHubClient.createFromConnectionString(connectionString, argv.hub);
client2 = EventHubClient.createFromConnectionString(connectionString, argv.hub);
log(`Created Receiver: for partition: "${partitionId}" in consumer group: "${consumerGroup}" in event hub "${argv.hub}".`);
log(
`Created Receiver: for partition: "${partitionId}" in consumer group: "${consumerGroup}" in event hub "${argv.hub}".`
);
log(`Created Sender: for partition: "${partitionId}" in event hub "${argv.hub}".`);
const onMessage = (m: EventData) => {
const mid = m.properties!.message_id as string;
Expand All @@ -85,7 +90,10 @@ export async function handler(argv: any): Promise<void> {
log("^^^^^^^^^^ An error occured with the receiver: %o", err);
};
const now = Date.now();
client1.receive(partitionId, onMessage, onError, { consumerGroup: consumerGroup, eventPosition: EventPosition.fromEnqueuedTime(now) });
client1.receive(partitionId, onMessage, onError, {
consumerGroup: consumerGroup,
eventPosition: EventPosition.fromEnqueuedTime(now),
});
log("Started receiving messages from enqueued time : '%s'.", new Date(now).toString());
await delay(3000);
while (true) {
Expand All @@ -95,22 +103,29 @@ export async function handler(argv: any): Promise<void> {
const m: EventData = {
body: "Hello World Event Hub " + new Date().toString(),
properties: {
message_id: messageId
}
message_id: messageId,
},
};
if (cache[messageId]) {
log("Looks like message with id '%s' is already present in the cache %o", messageId, cache);
}
cache[messageId] = m;
try {
const delivery = await client2.send(m, partitionId);
log("Sent message with id '%s'. Delivery id '%d', tag '%s'.",
messageId, delivery.id, delivery.tag.toString());
log(
"Sent message with id '%s'. Delivery id '%d', tag '%s'.",
messageId,
delivery.id,
delivery.tag.toString()
);
} catch (err) {
log("An error occurred while sending the message with id '%s', %o", messageId, err);
}
log("Sleeping for %d seconds. Next message will be sent around %s",
waitTime / 1000, new Date(nextIterationAt).toString());
log(
"Sleeping for %d seconds. Next message will be sent around %s",
waitTime / 1000,
new Date(nextIterationAt).toString()
);
try {
await delay(waitTime);
} catch (err) {
Expand Down

0 comments on commit a156887

Please sign in to comment.