Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: drop dependencies on google-proto-files and async #329

Merged
merged 3 commits into from Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions package.json
Expand Up @@ -55,7 +55,6 @@
"extend": "^3.0.1",
"google-auth-library": "^2.0.0",
"google-gax": "^0.20.0",
"google-proto-files": "^0.17.0",
"is": "^3.0.1",
"lodash.chunk": "^4.2.0",
"lodash.merge": "^4.6.0",
Expand All @@ -67,7 +66,6 @@
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"@types/arrify": "^1.0.4",
"@types/async": "^2.0.50",
"@types/duplexify": "^3.6.0",
"@types/extend": "^3.0.0",
"@types/is": "0.0.21",
Expand All @@ -76,7 +74,6 @@
"@types/sinon": "^5.0.5",
"@types/through2": "^2.0.34",
"@types/uuid": "^3.4.4",
"async": "^2.6.0",
"codecov": "^3.0.0",
"eslint": "^5.0.0",
"eslint-config-prettier": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/publisher.ts
Expand Up @@ -148,7 +148,7 @@ export class Publisher {
* //-
* publisher.publish(data).then((messageId) => {});
*/
publish(data: Buffer, attributes, callback?) {
publish(data: Buffer, attributes?, callback?) {
if (!(data instanceof Buffer)) {
throw new TypeError('Data must be in the form of a Buffer.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/subscription.ts
Expand Up @@ -297,7 +297,7 @@ export class Subscription extends Subscriber {
* const apiResponse = data[0];
* });
*/
delete(gaxOpts, callback?) {
delete(gaxOpts?, callback?) {
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = {};
Expand Down
4 changes: 2 additions & 2 deletions src/topic.ts
Expand Up @@ -138,7 +138,7 @@ export class Topic {
* const apiResponse = data[1];
* });
*/
create(gaxOpts, callback?) {
create(gaxOpts?, callback?) {
this.pubsub.createTopic(this.name, gaxOpts, callback);
}
/**
Expand Down Expand Up @@ -207,7 +207,7 @@ export class Topic {
* const apiResponse = data[0];
* });
*/
delete(gaxOpts, callback?) {
delete(gaxOpts?, callback?) {
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = {};
Expand Down
140 changes: 33 additions & 107 deletions system-test/pubsub.ts
Expand Up @@ -15,7 +15,6 @@
*/

import * as assert from 'assert';
import * as async from 'async';
import * as uuid from 'uuid';
import {PubSub, Subscription, Topic} from '../src';

Expand Down Expand Up @@ -52,67 +51,29 @@ describe('pubsub', function() {
return topic.name.split('/').pop();
}

function publishPop(message, options, callback) {
if (!callback) {
callback = options;
options = {};
}

options = options || {};

async function publishPop(message, options = {}) {
const topic = pubsub.topic(generateTopicName());
const publisher = topic.publisher();
const subscription = topic.subscription(generateSubName());

async.series(
[
topic.create.bind(topic),
subscription.create.bind(subscription),
function(callback) {
async.times(
6,
function(_, callback) {
publisher.publish(Buffer.from(message), options, callback);
},
callback
);
},
],
function(err) {
if (err) {
callback(err);
return;
}

subscription.on('error', callback);

subscription.once('message', function(message) {
callback(null, message);
});
}
);
await topic.create();
await subscription.create();
for (let i=0; i<6; i++) {
await publisher.publish(Buffer.from(message), options);
}
return new Promise((resolve, reject) => {
subscription.on('error', reject);
subscription.once('message', resolve);
});
}

before(function(done) {
before(() => {
// create all needed topics
async.each(
TOPICS,
function(topic, cb) {
topic.create(cb);
},
done
);
return Promise.all(TOPICS.map(t => t.create()));
});

after(function(done) {
after(() => {
// Delete topics
async.each(
TOPICS,
function(topic, callback) {
topic.delete(callback);
},
done
);
return Promise.all(TOPICS.map(t => t.delete()));
});

describe('Topic', function() {
Expand Down Expand Up @@ -213,20 +174,15 @@ describe('pubsub', function() {
});
});

it('should publish a message with attributes', function(done) {
it('should publish a message with attributes', async () => {
const data = Buffer.from('raw message data');
const attrs = {
customAttribute: 'value',
};

publishPop(data, attrs, function(err, message) {
assert.ifError(err);

assert.deepStrictEqual(message.data, data);
assert.deepStrictEqual(message.attributes, attrs);

done();
});
// tslint:disable-next-line no-any
const message: any = await publishPop(data, attrs);
assert.deepStrictEqual(message.data, data);
assert.deepStrictEqual(message.attributes, attrs);
});

it('should get the metadata of a topic', function(done) {
Expand All @@ -251,52 +207,24 @@ describe('pubsub', function() {
topic.subscription(SUB_NAMES[1], {ackDeadline: 60000}),
];

before(function(done) {
topic.create(function(err) {
assert.ifError(err);

function createSubscription(subscription, callback) {
subscription.create(callback);
}

async.each(SUBSCRIPTIONS, createSubscription, function(err) {
if (err) {
done(err);
return;
}

async.times(
10,
function(_, next) {
publisher.publish(Buffer.from('hello'), next);
},
function(err) {
if (err) {
done(err);
return;
}

// Consistency delay for subscriptions to be returned via
// `topic.getSubscriptions`.
setTimeout(done, 2500);
}
);
});
});
before(async () => {
await topic.create();
await Promise.all(SUBSCRIPTIONS.map(s => s.create()));
for (let i=0; i<10; i++) {
await publisher.publish(Buffer.from('hello'));
}
await new Promise(r => setTimeout(r, 2500));
});

after(function(done) {
after(() => {
// Delete subscriptions
async.each(
SUBSCRIPTIONS,
function(sub, callback) {
sub.delete(callback);
},
function(err) {
assert.ifError(err);
topic.delete(done);
return SUBSCRIPTIONS.map(async s => {
JustinBeckwith marked this conversation as resolved.
Show resolved Hide resolved
try {
await s.delete();
} catch (e) {
await topic.delete();
}
);
});
});

it('should return error if creating an existing subscription', function(done) {
Expand Down Expand Up @@ -330,8 +258,6 @@ describe('pubsub', function() {

it('should list all topic subscriptions as a stream', function(done) {
const subscriptionsEmitted: {}[] = [];

// tslint:disable-next-line no-any
topic
.getSubscriptionsStream()
.on('error', done)
Expand Down