Skip to content

Commit

Permalink
test: modernize mocha config (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Mar 29, 2020
1 parent f8bd91c commit d4cba53
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 75 deletions.
5 changes: 5 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"enable-source-maps": true,
"throw-deprecation": true,
"timeout": 10000
}
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
"eslint-plugin-prettier": "^3.0.0",
"grpc": "^1.24.0",
"gts": "^1.0.0",
"intelli-espower-loader": "^1.0.1",
"jsdoc": "^3.6.2",
"jsdoc-fresh": "^1.0.1",
"jsdoc-region-tag": "^1.0.2",
Expand All @@ -93,7 +92,6 @@
"mocha": "^7.0.0",
"mv": "^2.1.1",
"ncp": "^2.0.0",
"power-assert": "^1.4.4",
"prettier": "^1.18.2",
"proxyquire": "^2.0.0",
"sinon": "^9.0.0",
Expand Down
9 changes: 6 additions & 3 deletions samples/createTopic.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// description: Creates a new topic.
// usage: node createTopic.js <topic-name>

function main(topicName = 'YOUR_TOPIC_NAME') {
async function main(topicName = 'YOUR_TOPIC_NAME') {
// [START pubsub_create_topic]
/**
* TODO(developer): Uncomment this variable before running the sample.
Expand All @@ -46,8 +46,11 @@ function main(topicName = 'YOUR_TOPIC_NAME') {
console.log(`Topic ${topicName} created.`);
}

createTopic().catch(console.error);
createTopic();
// [END pubsub_create_topic]
}

main(...process.argv.slice(2));
main(...process.argv.slice(2)).catch(e => {
console.error(e);
process.exitCode = -1;
});
12 changes: 8 additions & 4 deletions samples/listenForErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// description: Listens to messages and errors for a subscription.
// usage: node listenForErrors.js <subscription-name> [timeout-in-seconds]

function main(subscriptionName = 'YOUR_SUBSCRIPTION_NAME', timeout = 10) {
async function main(subscriptionName = 'YOUR_SUBSCRIPTION_NAME', timeout = 10) {
timeout = Number(timeout);

// [START pubsub_subscriber_error_listener]
Expand All @@ -43,7 +43,7 @@ function main(subscriptionName = 'YOUR_SUBSCRIPTION_NAME', timeout = 10) {
// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function listenForErrors() {
function listenForErrors() {
// References an existing subscription
const subscription = pubSubClient.subscription(subscriptionName);

Expand All @@ -60,6 +60,7 @@ function main(subscriptionName = 'YOUR_SUBSCRIPTION_NAME', timeout = 10) {
const errorHandler = function(error) {
// Do something with the error
console.error(`ERROR: ${error}`);
throw error;
};

// Listen for new messages/errors until timeout is hit
Expand All @@ -72,8 +73,11 @@ function main(subscriptionName = 'YOUR_SUBSCRIPTION_NAME', timeout = 10) {
}, timeout * 1000);
}

listenForErrors().catch(console.error);
listenForErrors();
// [END pubsub_subscriber_error_listener]
}

main(...process.argv.slice(2));
main(...process.argv.slice(2)).catch(e => {
console.error(e);
process.exitCode = -1;
});
4 changes: 1 addition & 3 deletions samples/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"name": "nodejs-docs-samples-pubsub",
"files": [
"*.js",
"subscriptions/*.js",
"topics/*.js"
"*.js"
],
"private": true,
"license": "Apache-2.0",
Expand Down
65 changes: 26 additions & 39 deletions samples/system-test/subscriptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,23 @@
'use strict';

const {PubSub} = require('@google-cloud/pubsub');
const assertRejects = require('assert').rejects;
const {assert} = require('chai');
const {describe, it, before, after} = require('mocha');
const cp = require('child_process');
const uuid = require('uuid');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const execPromise = cmd =>
new Promise((resolve, reject) => {
cp.exec(cmd, {encoding: 'utf-8'}, (err, stdout, stderr) => {
if (err) {
err.stderr = stderr;
reject(err);
return;
}
resolve(stdout);
});
});

describe('subscriptions', () => {
const projectId = process.env.GCLOUD_PROJECT;
const pubsub = new PubSub({projectId});

const topicNameOne = `nodejs-docs-samples-test-${uuid.v4()}`;
const topicNameTwo = `nodejs-docs-samples-test-${uuid.v4()}`;
const subscriptionNameOne = `nodejs-docs-samples-test-sub-${uuid.v4()}`;
const subscriptionNameTwo = `nodejs-docs-samples-test-sub-${uuid.v4()}`;
const subscriptionNameThree = `nodejs-docs-samples-test-sub-${uuid.v4()}`;
const subscriptionNameFour = `nodejs-docs-samples-test-sub-${uuid.v4()}`;
const runId = uuid.v4();
const topicNameOne = `topic1-${runId}`;
const topicNameTwo = `topic2-${runId}`;
const subscriptionNameOne = `sub1-${runId}`;
const subscriptionNameTwo = `sub2-${runId}`;
const subscriptionNameThree = `sub3-${runId}`;
const subscriptionNameFour = `sub4-${runId}`;
const fullTopicNameOne = `projects/${projectId}/topics/${topicNameOne}`;
const fullSubscriptionNameOne = `projects/${projectId}/subscriptions/${subscriptionNameOne}`;
const fullSubscriptionNameTwo = `projects/${projectId}/subscriptions/${subscriptionNameTwo}`;
Expand All @@ -53,22 +41,24 @@ describe('subscriptions', () => {
return `node ${action}.js`;
}

before(() => {
before(async () => {
return Promise.all([
pubsub.createTopic(topicNameOne),
pubsub.createTopic(topicNameTwo),
]);
});

after(() => {
return Promise.all([
pubsub.subscription(subscriptionNameOne).delete(),
pubsub.subscription(subscriptionNameTwo).delete(),
pubsub.subscription(subscriptionNameThree).delete(),
pubsub.subscription(subscriptionNameFour).delete(),
pubsub.topic(topicNameOne).delete(),
pubsub.topic(topicNameTwo).delete(),
]).catch(console.error);
after(async () => {
const [subscriptions] = await pubsub.getSubscriptions();
const runSubs = subscriptions.filter(x => x.name.endsWith(runId));
for (const sub of runSubs) {
await sub.delete();
}
const [topics] = await pubsub.getTopics();
const runTops = topics.filter(x => x.name.endsWith(runId));
for (const t of runTops) {
await t.delete();
}
});

it('should create a subscription', async () => {
Expand Down Expand Up @@ -178,6 +168,13 @@ describe('subscriptions', () => {
assert(subscriptions.some(s => s.name === fullSubscriptionNameFour));
});

it('should listen for error messages', () => {
assert.throws(
() => execSync(`node listenForErrors nonexistent-subscription`),
/Resource not found/
);
});

it('should listen for ordered messages', async () => {
const timeout = 5;
const subscriptions = require('../listenForOrderedMessages');
Expand Down Expand Up @@ -239,16 +236,6 @@ describe('subscriptions', () => {
console.log = log; // eslint-disable-line require-atomic-updates
});

it('should listen for error messages', async () => {
assertRejects(
() =>
execPromise(
`${commandFor('listenForErrors')} nonexistent-subscription`
),
/Resource not found/
);
});

it('should set the IAM policy for a subscription', async () => {
execSync(`${commandFor('setSubscriptionPolicy')} ${subscriptionNameOne}`);
const results = await pubsub
Expand Down
47 changes: 27 additions & 20 deletions samples/system-test/topics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,34 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
describe('topics', () => {
const projectId = process.env.GCLOUD_PROJECT;
const pubsub = new PubSub({projectId});
const topicNameOne = `nodejs-docs-samples-test-${uuid.v4()}`;
const topicNameTwo = `nodejs-docs-samples-test-${uuid.v4()}`;
const subscriptionNameOne = `nodejs-docs-samples-test-${uuid.v4()}`;
const subscriptionNameTwo = `nodejs-docs-samples-test-${uuid.v4()}`;
const subscriptionNameThree = `nodejs-docs-samples-test-${uuid.v4()}`;
const subscriptionNameFour = `nodejs-docs-samples-test-${uuid.v4()}`;
const runId = uuid.v4();
console.log(`Topics runId: ${runId}`);
const topicNameOne = `top1-${runId}`;
const topicNameTwo = `top2-${runId}`;
const subscriptionNameOne = `sub1-${runId}`;
const subscriptionNameTwo = `sub2-${runId}`;
const subscriptionNameThree = `sub3-${runId}`;
const subscriptionNameFour = `sub4-${runId}`;
const fullTopicNameOne = `projects/${projectId}/topics/${topicNameOne}`;
const expectedMessage = {data: 'Hello, world!'};

function commandFor(action) {
return `node ${action}.js`;
}

before(() => {
return pubsub.createTopic(topicNameTwo).catch(console.error);
before(async () => {
await pubsub.createTopic(topicNameTwo);
});

after(() => {
return Promise.all([
pubsub.subscription(subscriptionNameOne).delete(),
pubsub.topic(topicNameOne).delete(),
pubsub.topic(topicNameTwo).delete(),
pubsub.subscription(subscriptionNameTwo).delete(),
pubsub.subscription(subscriptionNameThree).delete(),
pubsub.subscription(subscriptionNameFour).delete(),
]).catch(console.error);
after(async () => {
const [subscriptions] = await pubsub.getSubscriptions();
await Promise.all(
subscriptions.filter(x => x.name.endsWith(runId)).map(x => x.delete())
);
const [topics] = await pubsub.getTopics();
await Promise.all(
topics.filter(x => x.name.endsWith(runId)).map(x => x.delete())
);
});

// Helper function to pull one message.
Expand All @@ -73,7 +75,8 @@ describe('topics', () => {
const output = execSync(`${commandFor('createTopic')} ${topicNameOne}`);
assert.include(output, `Topic ${topicNameOne} created.`);
const [topics] = await pubsub.getTopics();
assert(topics.some(t => t.name === fullTopicNameOne));
const exists = topics.some(t => t.name === fullTopicNameOne);
assert.ok(exists, 'Topic was created');
});

it('should list topics', async () => {
Expand Down Expand Up @@ -175,10 +178,14 @@ describe('topics', () => {

const {data, publishTime} = await _pullOneMessage(subscription);
const actualWait = publishTime.getTime() - startTime;
const acceptableLatency = 100;
const acceptableLatency = 150;

assert.strictEqual(data.toString(), expectedMessage.data);
assert(actualWait <= waitTime + acceptableLatency);
assert.isAtMost(
actualWait,
waitTime + acceptableLatency,
'read is within acceptable latency'
);
});

it('should publish with retry settings', async () => {
Expand Down
4 changes: 0 additions & 4 deletions test/mocha.opts

This file was deleted.

0 comments on commit d4cba53

Please sign in to comment.