Skip to content

Commit be8ed53

Browse files
authored
fix: always fill the topic and sub names when creating from a PubSub object (#1816)
1 parent e480388 commit be8ed53

File tree

2 files changed

+101
-10
lines changed

2 files changed

+101
-10
lines changed

src/pubsub.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,17 @@ export class PubSub {
561561
return;
562562
}
563563
subscription.metadata = resp!;
564+
565+
// If this is the first call we've made, the projectId might be empty still.
566+
if (subscription.name?.includes(PROJECT_ID_PLACEHOLDER)) {
567+
if (subscription.metadata && subscription.metadata.name) {
568+
subscription.name = Subscription.formatName_(
569+
this.projectId,
570+
subscription.metadata.name
571+
);
572+
}
573+
}
574+
564575
callback!(null, subscription, resp!);
565576
}
566577
);
@@ -655,6 +666,14 @@ export class PubSub {
655666
return;
656667
}
657668
topic.metadata = resp!;
669+
670+
// If this is the first call we've made, the projectId might be empty still.
671+
if (topic.name?.includes(PROJECT_ID_PLACEHOLDER)) {
672+
if (topic.metadata && topic.metadata.name) {
673+
topic.name = Topic.formatName_(this.projectId, topic.metadata.name);
674+
}
675+
}
676+
658677
callback!(null, topic, resp!);
659678
}
660679
);

test/pubsub.ts

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const PKG = require('../../package.json');
3333
const sandbox = sinon.createSandbox();
3434

3535
const fakeCreds = {} as gax.grpc.ChannelCredentials;
36-
sandbox.stub(gax.grpc.credentials, 'createInsecure').returns(fakeCreds);
3736

3837
const subscriptionCached = subby.Subscription;
3938

@@ -49,6 +48,11 @@ function Subscription(
4948
return new overrideFn(pubsub, name, options);
5049
}
5150

51+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
52+
(Subscription as any).formatName_ = (): string => {
53+
return 'formatted';
54+
};
55+
5256
let promisified = false;
5357
const fakeUtil = Object.assign({}, util, {
5458
promisifySome(
@@ -92,6 +96,10 @@ class FakeTopic {
9296
constructor(...args: Array<{}>) {
9397
this.calledWith_ = args;
9498
}
99+
100+
static formatName_(): string {
101+
return 'foo';
102+
}
95103
}
96104

97105
let extended = false;
@@ -187,6 +195,11 @@ describe('PubSub', () => {
187195
googleAuthOverride = null;
188196
pubsub = new PubSub(OPTIONS);
189197
pubsub.projectId = PROJECT_ID;
198+
sandbox.stub(gax.grpc.credentials, 'createInsecure').returns(fakeCreds);
199+
});
200+
201+
afterEach(() => {
202+
sandbox.restore();
190203
});
191204

192205
describe('instantiation', () => {
@@ -554,13 +567,15 @@ describe('PubSub', () => {
554567

555568
it('should return Subscription & resp to the callback', done => {
556569
const subscription = {};
557-
pubsub.subscription = () => {
570+
sandbox.stub(pubsub, 'subscription').callsFake(() => {
558571
return subscription as subby.Subscription;
559-
};
572+
});
560573

561-
pubsub.request = (config, callback: Function) => {
562-
callback(null, apiResponse);
563-
};
574+
sandbox
575+
.stub(pubsub, 'request')
576+
.callsFake((config, callback: Function) => {
577+
callback(null, apiResponse);
578+
});
564579

565580
function callback(
566581
err?: Error | null,
@@ -575,6 +590,31 @@ describe('PubSub', () => {
575590

576591
pubsub.createSubscription?.(TOPIC_NAME, SUB_NAME, callback);
577592
});
593+
594+
it('should fill the subscription object name if projectId was empty', async () => {
595+
const subscription = {};
596+
pubsub.projectId = undefined;
597+
sandbox.stub(pubsub, 'subscription').callsFake(() => {
598+
// Simulate the project ID not being resolved.
599+
const sub = subscription as subby.Subscription;
600+
sub.name = '{{projectId}}/foo/bar';
601+
return sub;
602+
});
603+
604+
sandbox
605+
.stub(pubsub, 'request')
606+
.callsFake((config, callback: Function) => {
607+
callback(null, apiResponse);
608+
});
609+
610+
const [sub, resp] = await pubsub.createSubscription!(
611+
TOPIC_NAME,
612+
SUB_NAME
613+
)!;
614+
assert.strictEqual(sub, subscription);
615+
assert.strictEqual(sub.name.includes('{{'), false);
616+
assert.strictEqual(resp, apiResponse);
617+
});
578618
});
579619
});
580620

@@ -625,12 +665,17 @@ describe('PubSub', () => {
625665
});
626666

627667
describe('success', () => {
628-
const apiResponse = {};
668+
const apiResponse = {
669+
name: 'new-topic',
670+
};
671+
let requestStub: sinon.SinonStub<unknown[], unknown>;
629672

630673
beforeEach(() => {
631-
pubsub.request = (config, callback: Function) => {
632-
callback(null, apiResponse);
633-
};
674+
requestStub = sandbox
675+
.stub(pubsub, 'request')
676+
.callsFake((config, callback: Function) => {
677+
callback(null, apiResponse);
678+
});
634679
});
635680

636681
it('should return a Topic object', done => {
@@ -656,6 +701,33 @@ describe('PubSub', () => {
656701
done();
657702
});
658703
});
704+
705+
it('should fill the topic object name if projectId was empty', async () => {
706+
const topicName = 'new-topic';
707+
const topicInstance = {};
708+
709+
sandbox.stub(pubsub, 'topic').callsFake(name => {
710+
assert.strictEqual(name, topicName);
711+
712+
// Simulate the project ID not being resolved.
713+
const topic = topicInstance as Topic;
714+
topic.name = 'projects/{{projectId}}/topics/new-topic';
715+
return topic;
716+
});
717+
718+
requestStub.restore();
719+
sandbox
720+
.stub(pubsub, 'request')
721+
.callsFake((config, callback: Function) => {
722+
pubsub.projectId = 'projectId';
723+
callback(null, apiResponse);
724+
});
725+
726+
const [topic, resp] = await pubsub.createTopic!(topicName)!;
727+
assert.strictEqual(topic, topicInstance);
728+
assert.strictEqual(topic.name.includes('{{'), false);
729+
assert.strictEqual(resp, apiResponse);
730+
});
659731
});
660732
});
661733

0 commit comments

Comments
 (0)