Skip to content

Commit 8aeb3d1

Browse files
author
Ace Nassri
authored
Update functions/background sample (#1353)
* Use system tests + update sample + fix lint * Swap emulator with Node.js FF * Put emulator back * More context-sensitive emulator install (to avoid FF conflicts) * Tweak build cmd * Attempt to fix failing test: add missing pkg + increase timeout * DEBUG: debug CI * DEBUG: print base URL * FIX: move BASE_URL into emulator-only block * Revert dbg commits + handle timeout (for Linux systems) * Delete debug, take 2
1 parent 9265b1c commit 8aeb3d1

4 files changed

Lines changed: 58 additions & 45 deletions

File tree

.kokoro/build.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export SENDGRID_API_KEY=$(cat $KOKORO_GFILE_DIR/secrets-sendgrid-api-key.txt)
4141
# Configure GCF variables
4242
export FUNCTIONS_TOPIC=integration-tests-instance
4343
export FUNCTIONS_BUCKET=$GCLOUD_PROJECT
44-
export BASE_URL="http://localhost:8010/${GCLOUD_PROJECT}/${GCF_REGION}"
4544

4645
# Configure IoT variables
4746
export NODEJS_IOT_EC_PUBLIC_KEY=${KOKORO_GFILE_DIR}/ec_public.pem
@@ -58,8 +57,12 @@ export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/secrets-key.json
5857
gcloud auth activate-service-account --key-file "$GOOGLE_APPLICATION_CREDENTIALS"
5958
gcloud config set project $GCLOUD_PROJECT
6059

60+
npm install -g @google-cloud/functions-framework
61+
6162
# Start functions emulator, if appropriate
62-
if [[ $PROJECT == functions/* ]]; then
63+
if [[ $PROJECT == functions/* ]] && grep --quiet functions-emulator package.json; then
64+
export BASE_URL="http://localhost:8010/${GCLOUD_PROJECT}/${GCF_REGION}"
65+
6366
export FUNCTIONS_LOG_PATH=$(pwd)/logs/cloud-functions-emulator.log
6467
npm install -g @google-cloud/functions-emulator
6568
touch "$FUNCTIONS_LOG_PATH"

functions/background/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ const requestPromiseNative = require('request-promise-native');
2222
* Background Cloud Function that returns a Promise. Note that we don't pass
2323
* a "callback" argument to the function.
2424
*
25-
* @param {object} event The Cloud Functions event.
26-
* @param {object} event.data The event data.
25+
* @param {object} data The event data
26+
* @param {object} data.endpoint The URL to send the request to.
2727
* @returns {Promise}
2828
*/
29-
exports.helloPromise = event => {
29+
exports.helloPromise = data => {
3030
return requestPromiseNative({
31-
uri: event.data.endpoint,
31+
uri: data.endpoint,
3232
});
3333
};
3434
// [END functions_background_promise]
@@ -38,12 +38,11 @@ exports.helloPromise = event => {
3838
* Background Cloud Function that returns synchronously. Note that we don't pass
3939
* a "callback" argument to the function.
4040
*
41-
* @param {object} event The Cloud Functions event.
42-
* @param {object} event.data The event data.
41+
* @param {object} data The event data
4342
*/
44-
exports.helloSynchronous = event => {
43+
exports.helloSynchronous = data => {
4544
// This function returns synchronously
46-
if (event.data.something === true) {
45+
if (data.something === true) {
4746
return 'Something is true!';
4847
} else {
4948
throw new Error('Something was not true!');

functions/background/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
"request-promise-native": "^1.0.5"
2020
},
2121
"devDependencies": {
22-
"@google-cloud/nodejs-repo-tools": "^3.3.0",
22+
"@google-cloud/functions-framework": "^1.1.1",
23+
"child-process-promise": "^2.2.1",
2324
"mocha": "^6.0.0",
24-
"proxyquire": "^2.1.0",
25-
"sinon": "^7.2.7"
25+
"requestretry": "^4.0.0"
2626
},
2727
"cloud-repo-tools": {
2828
"requiresKeyFile": true,

functions/background/test/index.test.js

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,62 @@
1515

1616
'use strict';
1717

18-
const proxyquire = require('proxyquire').noCallThru();
19-
const sinon = require('sinon');
2018
const assert = require('assert');
21-
const tools = require('@google-cloud/nodejs-repo-tools');
19+
const requestRetry = require('requestretry');
20+
const execPromise = require('child-process-promise').exec;
21+
const path = require('path');
2222

23-
function getSample() {
24-
const requestPromiseNative = sinon.stub().returns(Promise.resolve('test'));
23+
const program = require('..');
2524

26-
return {
27-
program: proxyquire('../', {
28-
'request-promise-native': requestPromiseNative,
29-
}),
30-
mocks: {
31-
requestPromiseNative: requestPromiseNative,
32-
},
33-
};
34-
}
25+
const BASE_URL = process.env.BASE_URL || 'http://localhost:8080';
3526

36-
beforeEach(tools.stubConsole);
37-
afterEach(tools.restoreConsole);
27+
const cwd = path.join(__dirname, '..');
3828

39-
it('should make a promise request', () => {
40-
const sample = getSample();
29+
let ffProc;
30+
31+
before(() => {
32+
ffProc = execPromise(
33+
`functions-framework --target=helloPromise --signature-type=event`,
34+
{timeout: 2000, shell: true, cwd}
35+
);
36+
});
37+
38+
after(async () => {
39+
try {
40+
await ffProc;
41+
} catch (err) {
42+
// Timeouts always cause errors on Linux, so catch them
43+
if (err.name && err.name === 'ChildProcessError') {
44+
return;
45+
}
46+
47+
throw err;
48+
}
49+
});
50+
51+
it('should make a promise request', async () => {
4152
const event = {
4253
data: {
43-
endpoint: 'foo.com',
54+
endpoint: 'https://example.com',
4455
},
4556
};
4657

47-
return sample.program.helloPromise(event).then(result => {
48-
assert.deepStrictEqual(sample.mocks.requestPromiseNative.firstCall.args, [
49-
{uri: 'foo.com'},
50-
]);
51-
assert.strictEqual(result, 'test');
58+
const response = await requestRetry({
59+
url: `${BASE_URL}/`,
60+
method: 'POST',
61+
body: event,
62+
retryDelay: 200,
63+
json: true,
5264
});
65+
66+
assert.strictEqual(response.statusCode, 200);
67+
assert.ok(response.body.includes(`Example Domain`));
5368
});
5469

5570
it('should return synchronously', () => {
5671
assert.strictEqual(
57-
getSample().program.helloSynchronous({
58-
data: {
59-
something: true,
60-
},
72+
program.helloSynchronous({
73+
something: true,
6174
}),
6275
'Something is true!'
6376
);
@@ -66,10 +79,8 @@ it('should return synchronously', () => {
6679
it('should throw an error', () => {
6780
assert.throws(
6881
() => {
69-
getSample().program.helloSynchronous({
70-
data: {
71-
something: false,
72-
},
82+
program.helloSynchronous({
83+
something: false,
7384
});
7485
},
7586
Error,

0 commit comments

Comments
 (0)