Skip to content

Commit a769a95

Browse files
authored
feat: getAppProfilesStream emits (if there are any failed locations present) decorated error with failedLocations info (#795)
1 parent 367f476 commit a769a95

4 files changed

Lines changed: 127 additions & 13 deletions

File tree

handwritten/bigtable/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,9 @@ export class Bigtable {
824824
.on('error', (err: Error) => {
825825
stream.destroy(err);
826826
})
827+
.on('response', response => {
828+
stream.emit('response', response);
829+
})
827830
.pipe(stream);
828831
});
829832
}

handwritten/bigtable/src/instance.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -758,14 +758,38 @@ Please use the format 'my-instance' or '${bigtable.projectName}/instances/my-ins
758758
appProfile.metadata = chunk;
759759
callback(null, appProfile);
760760
};
761+
let failedLocations: string[] = [];
762+
const flush = (callback: Function) => {
763+
if (failedLocations.length > 0) {
764+
callback(
765+
new Error(
766+
`Resources from the following locations are currently not available\n${JSON.stringify(
767+
failedLocations
768+
)}`
769+
)
770+
);
771+
} else {
772+
callback();
773+
}
774+
};
775+
const stream = this.bigtable.request({
776+
client: 'BigtableInstanceAdminClient',
777+
method: 'listAppProfilesStream',
778+
reqOpts,
779+
gaxOpts,
780+
});
781+
stream.on('response', apiResp => {
782+
if (arrify(apiResp.failedLocations).length > 0) {
783+
failedLocations = failedLocations.concat(apiResp.failedLocations);
784+
}
785+
});
761786
return pumpify.obj([
762-
this.bigtable.request({
763-
client: 'BigtableInstanceAdminClient',
764-
method: 'listAppProfilesStream',
765-
reqOpts,
766-
gaxOpts,
787+
stream,
788+
new Transform({
789+
objectMode: true,
790+
transform: transformToAppProfile,
791+
flush,
767792
}),
768-
new Transform({objectMode: true, transform: transformToAppProfile}),
769793
]);
770794
}
771795

handwritten/bigtable/test/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,7 @@ describe('Bigtable', () => {
635635
gaxOpts: {},
636636
};
637637

638-
const gapicStreamingMethods = [
639-
'listTablesStream',
640-
'listInstancesStream',
641-
'listAppProfilesStream',
642-
'listClustersStream',
643-
];
638+
const gapicStreamingMethods = ['listTablesStream', 'listAppProfilesStream'];
644639

645640
beforeEach(() => {
646641
bigtable.getProjectId_ = (callback: Function) => {
@@ -955,6 +950,17 @@ describe('Bigtable', () => {
955950
done();
956951
});
957952
});
953+
954+
it('should emit resmonse from GAX stream', done => {
955+
const response = {};
956+
const requestStream = bigtable.request(config);
957+
requestStream.emit('reading');
958+
requestStream.on('response', (resp: {}) => {
959+
assert.strictEqual(resp, response);
960+
done();
961+
});
962+
GAX_STREAM.emit('response', response);
963+
});
958964
});
959965
});
960966
});

handwritten/bigtable/test/instance.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ describe('Bigtable/Instance', () => {
769769
});
770770
});
771771

772-
describe('getApprofilesStream', () => {
772+
describe('getAppProfilesStream', () => {
773773
let returnStream: PassThrough;
774774
beforeEach(() => {
775775
returnStream = new PassThrough({
@@ -867,6 +867,87 @@ describe('Bigtable/Instance', () => {
867867
});
868868
});
869869

870+
it('should return a decorated error with failedLocations list', done => {
871+
let counter = 0;
872+
let failedLocations: string[] = [];
873+
const pages = [
874+
{
875+
appProfiles: [
876+
{
877+
name: '/projects/p/instances/i/appProfiles/profile-a',
878+
},
879+
{
880+
name: '/projects/p/instances/i/appProfiles/profile-b',
881+
},
882+
],
883+
response: {failedLocations: []},
884+
},
885+
{
886+
appProfiles: [
887+
{
888+
name: '/projects/p/instances/i/appProfiles/profile-c',
889+
},
890+
{
891+
name: '/projects/p/instances/i/appProfiles/profile-d',
892+
},
893+
],
894+
response: {failedLocations: ['us-east1-a']},
895+
},
896+
897+
{
898+
appProfiles: [
899+
{
900+
name: '/projects/p/instances/i/appProfiles/profile-e',
901+
},
902+
{
903+
name: '/projects/p/instances/i/appProfiles/profile-f',
904+
},
905+
],
906+
response: {failedLocations: ['us-west1-b', 'us-west1-c']},
907+
},
908+
];
909+
910+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
911+
(instance.bigtable.request as Function) = () => {
912+
return returnStream;
913+
};
914+
915+
setImmediate(() => {
916+
pages.forEach(p => {
917+
failedLocations = failedLocations.concat(p.response.failedLocations);
918+
returnStream.emit('response', p.response);
919+
p.appProfiles.forEach(a => {
920+
returnStream.push(a);
921+
counter++;
922+
});
923+
});
924+
returnStream.push(null);
925+
});
926+
const appProfiles: AppProfile[] = [];
927+
instance
928+
.getAppProfilesStream()
929+
.on('error', err => {
930+
assert.strictEqual(appProfiles.length, counter);
931+
console.log(err.message);
932+
assert.deepStrictEqual(
933+
err,
934+
new Error(
935+
`Resources from the following locations are currently not available\n${JSON.stringify(
936+
failedLocations
937+
)}`
938+
)
939+
);
940+
done();
941+
})
942+
.on('data', appProfile => {
943+
assert(appProfile instanceof FakeAppProfile);
944+
appProfiles.push(appProfile);
945+
})
946+
.on('end', () => {
947+
done();
948+
});
949+
});
950+
870951
it('should return an array of AppProfile objects', done => {
871952
const response = [
872953
{

0 commit comments

Comments
 (0)