Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/packages/gax/src/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ export function createApiCall(
);
};
}
return _createApiCall(func, settings, descriptor);
return _createApiCall(func, settings, descriptor, true);
Comment thread
shivanee-p marked this conversation as resolved.
}

export {protobuf};
Expand Down
145 changes: 128 additions & 17 deletions core/packages/gax/test/unit/apiCallable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import * as sinon from 'sinon';

import {CancellableStream, GRPCCall, RequestType} from '../../src/apitypes';
import {createApiCall as realCreateApiCall} from '../../src/createApiCall';
import {createApiCall as gaxCreateApiCall} from '../../src/createApiCall';
import {createApiCall as fallbackCreateApiCall} from '../../src/fallback';
import {StreamDescriptor} from '../../src/descriptor';
import {StreamType} from '../../src/streamingCalls/streaming';
import * as gax from '../../src/gax';
Expand Down Expand Up @@ -380,7 +381,7 @@
return {cancel: () => {}};
}

const apiCall = realCreateApiCall(func, settings);
const apiCall = gaxCreateApiCall(func, settings);
await apiCall({param: 'test'}, undefined);

assert.strictEqual(traceAttemptSpy.calledOnce, true);
Expand Down Expand Up @@ -416,7 +417,7 @@
return Object.assign(s, {cancel: () => {}});
});

const apiCall = realCreateApiCall(
const apiCall = gaxCreateApiCall(
spy as unknown as GRPCCall,
settings,
new StreamDescriptor(StreamType.SERVER_STREAMING, true),
Expand Down Expand Up @@ -449,7 +450,7 @@
return {cancel: () => {}};
}

const apiCall = realCreateApiCall(func, settings);
const apiCall = gaxCreateApiCall(func, settings);
await apiCall({}, undefined);

assert.strictEqual(traceAttemptSpy.calledOnce, true);
Expand Down Expand Up @@ -484,7 +485,7 @@
return {cancel: () => {}};
}

const apiCall = realCreateApiCall(func, settings);
const apiCall = gaxCreateApiCall(func, settings);
await apiCall({}, undefined);

assert.strictEqual(traceAttemptSpy.called, false);
Expand Down Expand Up @@ -514,7 +515,7 @@
};
}

const apiCall = realCreateApiCall(func, settings);
const apiCall = gaxCreateApiCall(func, settings);
const [response] = (await apiCall({}, undefined)) as [
{data: string},
unknown,
Expand Down Expand Up @@ -564,7 +565,7 @@
};
}

const apiCall = realCreateApiCall(func, settings, undefined, true);
const apiCall = gaxCreateApiCall(func, settings, undefined, true);
await apiCall({}, undefined);

const spans = harness.getSpans('google-gax');
Expand All @@ -575,6 +576,116 @@
assert.strictEqual(span.attributes['gcp.method.type'], 'http');
});

it('passes fallback flag through when using fallback createApiCall with default options', async () => {
process.env.GOOGLE_SDK_NODE_EXPERIMENTAL_O11Y_ENABLED = 'true';
const traceAttemptSpy = sinon.spy(tracerHelper, 'traceAttempt');

const settings = new gax.CallSettings({
apiName: 'google.example.v1.Echo',
enableTelemetryTracing: true,
otherArgs: {
internalTelemetryInfo: telemetryInfo,
internalMethodName: 'Echo',
},
});

function func(
argument: {},
metadata: {},
options: {},
callback: (err: GoogleError | null, resp?: unknown) => void,
) {
callback(null, {data: 'hello'});
return {
cancel: () => {},
};
}

const apiCall = fallbackCreateApiCall(func, settings);
await apiCall({}, undefined);

assert.strictEqual(traceAttemptSpy.calledOnce, true);
const [dynamicArgs] = traceAttemptSpy.firstCall.args;
assert.strictEqual(dynamicArgs.rpcType, 'http');

const spans = harness.getSpans('google-gax');
assert.strictEqual(spans.length, 1);
const span = spans[0];
assert.strictEqual(span.name, 'EchoClient.Echo');
assert.strictEqual(span.ended, true);
assert.strictEqual(span.attributes['gcp.method.type'], 'http');
});

it('passes explicit _fallback through when using fallback createApiCall', async () => {
process.env.GOOGLE_SDK_NODE_EXPERIMENTAL_O11Y_ENABLED = 'true';
const traceAttemptSpy = sinon.spy(tracerHelper, 'traceAttempt');

const settings = new gax.CallSettings({
apiName: 'google.example.v1.Echo',
enableTelemetryTracing: true,
otherArgs: {
internalTelemetryInfo: telemetryInfo,
internalMethodName: 'Echo',
},
});

function func(
argument: {},
metadata: {},
options: {},
callback: (err: GoogleError | null, resp?: unknown) => void,
) {
callback(null, {data: 'hello'});
return {
cancel: () => {},
};
}

const apiCall = fallbackCreateApiCall(func, settings, undefined, 'rest');
await apiCall({}, undefined);

assert.strictEqual(traceAttemptSpy.calledOnce, true);
const [dynamicArgs] = traceAttemptSpy.firstCall.args;
assert.strictEqual(dynamicArgs.rpcType, 'http');

const spans = harness.getSpans('google-gax');
assert.strictEqual(spans.length, 1);
const span = spans[0];
assert.strictEqual(span.attributes['gcp.method.type'], 'http');
});

it('passes fallback flag and isStreamingCall as true for server-streaming fallback calls', () => {
process.env.GOOGLE_SDK_NODE_EXPERIMENTAL_O11Y_ENABLED = 'true';
const traceAttemptSpy = sinon.spy(tracerHelper, 'traceAttempt');

const settings = new gax.CallSettings({
apiName: 'google.example.v1.Echo',
enableTelemetryTracing: true,
otherArgs: {
internalTelemetryInfo: telemetryInfo,
internalMethodName: 'Echo',
},
});

const spy = sinon.spy(() => {
const s = new PassThrough({objectMode: true});
s.push(null);
return Object.assign(s, {cancel: () => {}});
});

const apiCall = fallbackCreateApiCall(
spy as unknown as GRPCCall,
settings,
new StreamDescriptor(StreamType.SERVER_STREAMING, true),
);
void apiCall({}, undefined);

assert.strictEqual(traceAttemptSpy.calledOnce, true);
const [dynamicArgs, , , isStreamingCall] = traceAttemptSpy.firstCall.args;
assert.strictEqual(dynamicArgs.rpcType, 'http');
assert.strictEqual(isStreamingCall, true);
});

it('sets rpcType to grpc when _fallback is boolean false', async () => {
process.env.GOOGLE_SDK_NODE_EXPERIMENTAL_O11Y_ENABLED = 'true';
const settings = new gax.CallSettings({
Expand All @@ -598,7 +709,7 @@
};
}

const apiCall = realCreateApiCall(func, settings, undefined, false);
const apiCall = gaxCreateApiCall(func, settings, undefined, false);
await apiCall({}, undefined);

const spans = harness.getSpans('google-gax');
Expand Down Expand Up @@ -630,7 +741,7 @@
};
}

const apiCall = realCreateApiCall(func, settings, undefined, 'rest');
const apiCall = gaxCreateApiCall(func, settings, undefined, 'rest');
await apiCall({}, undefined);

const spans = harness.getSpans('google-gax');
Expand Down Expand Up @@ -662,7 +773,7 @@
};
}

const apiCall = realCreateApiCall(func, settings, undefined, 'proto');
const apiCall = gaxCreateApiCall(func, settings, undefined, 'proto');
await apiCall({}, undefined);

const spans = harness.getSpans('google-gax');
Expand Down Expand Up @@ -704,7 +815,7 @@
};
}

const apiCall = realCreateApiCall(func, defaults.echo);
const apiCall = gaxCreateApiCall(func, defaults.echo);
await apiCall({}, undefined);

const spans = harness.getSpans('google-gax');
Expand Down Expand Up @@ -752,7 +863,7 @@
};
}

const apiCall = realCreateApiCall(failingFunc, settings);
const apiCall = gaxCreateApiCall(failingFunc, settings);
const promise = apiCall({}, undefined);
assert.strictEqual(harness.getSpans('google-gax').length, 0);

Expand Down Expand Up @@ -800,7 +911,7 @@
};
}

const apiCall = realCreateApiCall(asyncFunc, settings);
const apiCall = gaxCreateApiCall(asyncFunc, settings);
const promise = apiCall({}, undefined);

// Verify the span is not ended prematurely while the call is in flight
Expand Down Expand Up @@ -847,7 +958,7 @@
};
}

const apiCall = realCreateApiCall(cancellableFunc, settings);
const apiCall = gaxCreateApiCall(cancellableFunc, settings);
const promise = apiCall({}, undefined);
assert.strictEqual(typeof promise.cancel, 'function');
promise.cancel();
Expand Down Expand Up @@ -885,7 +996,7 @@
};
}

const apiCall = realCreateApiCall(func, settings);
const apiCall = gaxCreateApiCall(func, settings);
await apiCall({}, undefined);

const spans = harness.getSpans('google-gax');
Expand Down Expand Up @@ -913,7 +1024,7 @@
return Object.assign(s, {cancel: () => {}});
});

const apiCall = realCreateApiCall(
const apiCall = gaxCreateApiCall(
spy as unknown as GRPCCall,
settings,
new StreamDescriptor(StreamType.SERVER_STREAMING, true),
Expand Down Expand Up @@ -964,7 +1075,7 @@
return Object.assign(s, {cancel: () => {}});
});

const apiCall = realCreateApiCall(
const apiCall = gaxCreateApiCall(
spy as unknown as GRPCCall,
settings,
new StreamDescriptor(StreamType.SERVER_STREAMING, true),
Expand Down Expand Up @@ -1013,9 +1124,9 @@
assert.ok(Array.isArray(response));
assert.strictEqual(response[0], 42);
assert.ok(deadlineArg);
return done();

Check warning on line 1127 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise

Check warning on line 1127 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
})
.catch(done);

Check warning on line 1129 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise

Check warning on line 1129 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
});

it('emits error on rejected promise', async () => {
Expand Down Expand Up @@ -1043,12 +1154,12 @@
const promise = (apiCall as any)(null);
promise
.then(() => {
return done(new Error('should not reach'));

Check warning on line 1157 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise

Check warning on line 1157 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
})
.catch((err: {code: number}) => {
assert(err instanceof GoogleError);
assert.strictEqual(err.code, status.CANCELLED);
done();

Check warning on line 1162 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise

Check warning on line 1162 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
});
promise.cancel();
});
Expand Down Expand Up @@ -1083,13 +1194,13 @@
const promise = (apiCall as any)(null);
promise
.then(() => {
return done(new Error('should not reach'));

Check warning on line 1197 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise

Check warning on line 1197 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
})
.catch(() => {
assert(callCount < 4);
done();

Check warning on line 1201 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise

Check warning on line 1201 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
})
.catch(done);

Check warning on line 1203 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise

Check warning on line 1203 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
setTimeout(() => {
promise.cancel();
}, 15);
Expand Down Expand Up @@ -1172,9 +1283,9 @@
assert.strictEqual(resp[0], 1729);
assert.strictEqual(toAttempt, 0);
assert.ok(deadlineArg);
return done();

Check warning on line 1286 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise

Check warning on line 1286 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
})
.catch(done);

Check warning on line 1288 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise

Check warning on line 1288 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
});

it('cancels in the middle of retries', done => {
Expand All @@ -1197,7 +1308,7 @@
const promise = apiCall({}, undefined);
promise
.then(() => {
return done(new Error('should not reach'));

Check warning on line 1311 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise

Check warning on line 1311 in core/packages/gax/test/unit/apiCallable.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
})
.catch((err: Error) => {
assert(err instanceof Error);
Expand Down
Loading