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

test: reduce request usage #1606

Merged
merged 5 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 2 additions & 4 deletions datastore/functions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "nodejs-docs-samples-functions-datastore",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
Expand All @@ -16,15 +15,14 @@
},
"dependencies": {
"@google-cloud/datastore": "^5.0.0",
"supertest": "^4.0.0"
"is-reachable": "^4.0.0"
},
"devDependencies": {
"@google-cloud/functions-framework": "^1.1.1",
"child-process-promise": "^2.2.1",
"gaxios": "^2.3.1",
"mocha": "^7.0.0",
"proxyquire": "^2.1.0",
"request": "^2.88.0",
"requestretry": "^4.0.0",
"sinon": "^8.0.0",
"uuid": "^3.3.2"
}
Expand Down
92 changes: 47 additions & 45 deletions datastore/functions/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,19 @@
'use strict';

const assert = require('assert');
const path = require('path');
const uuid = require('uuid');
const sinon = require('sinon');
const {request} = require('gaxios');
const isReachable = require('is-reachable');
const execPromise = require('child-process-promise').exec;
const {Datastore} = require('@google-cloud/datastore');

const datastore = new Datastore();
const program = require('../');
const uuid = require('uuid');
const path = require('path');
const execPromise = require('child-process-promise').exec;
const sinon = require('sinon');

const FF_TIMEOUT = 3000;

let requestRetry = require('requestretry');
requestRetry = requestRetry.defaults({
retryDelay: 500,
retryStrategy: requestRetry.RetryStrategies.NetworkError,
});

const cwd = path.join(__dirname, '..');

const NAME = 'sampletask1';
const KIND = `Task-${uuid.v4()}`;
const VALUE = {
Expand All @@ -54,17 +49,27 @@ const handleLinuxFailures = async proc => {
}
};

// Wait for the HTTP server to start listening
const waitForReady = async baseUrl => {
let ready = false;
while (!ready) {
await new Promise(r => setTimeout(r, 500));
ready = await isReachable(baseUrl);
}
};

describe('functions/datastore', () => {
describe('set', () => {
let ffProc;
const PORT = 8080;
const BASE_URL = `http://localhost:${PORT}`;

before(() => {
before(async () => {
ffProc = execPromise(
`functions-framework --target=set --signature-type=http --port=${PORT}`,
{timeout: FF_TIMEOUT, shell: true, cwd}
);
await waitForReady(BASE_URL);
});

after(async () => {
Expand Down Expand Up @@ -96,9 +101,7 @@ describe('functions/datastore', () => {
status: sinon.stub().returnsThis(),
send: sinon.stub(),
};

await program.set(req, res);

assert.ok(res.status.calledWith(500));
assert.ok(res.send.calledWith(errorMsg('Key')));
});
Expand All @@ -122,19 +125,18 @@ describe('functions/datastore', () => {
});

it('set: Saves an entity', async () => {
const response = await requestRetry({
const response = await request({
url: `${BASE_URL}/set`,
method: 'POST',
body: {
responseType: 'text',
data: {
kind: KIND,
key: NAME,
value: VALUE,
},
json: true,
});

assert.strictEqual(response.statusCode, 200);
assert.ok(response.body.includes(`Entity ${KIND}/${NAME} saved`));
assert.strictEqual(response.status, 200);
assert.ok(response.data.includes(`Entity ${KIND}/${NAME} saved`));
});
});

Expand All @@ -143,49 +145,49 @@ describe('functions/datastore', () => {
const PORT = 8081;
const BASE_URL = `http://localhost:${PORT}`;

before(() => {
before(async () => {
ffProc = execPromise(
`functions-framework --target=get --signature-type=http --port=${PORT}`,
{timeout: FF_TIMEOUT, shell: true, cwd}
);
await waitForReady(BASE_URL);
});

after(async () => {
await handleLinuxFailures(ffProc);
});

it('get: Fails when entity does not exist', async () => {
const response = await requestRetry({
const response = await request({
url: `${BASE_URL}/get`,
method: 'POST',
body: {
data: {
kind: KIND,
key: 'nonexistent',
},
json: true,
responseType: 'text',
validateStatus: () => true,
});

assert.strictEqual(response.statusCode, 500);
assert.strictEqual(response.status, 500);
assert.ok(
new RegExp(
/(Missing or insufficient permissions.)|(No entity found for key)/
).test(response.body)
).test(response.data)
);
});

it('get: Finds an entity', async () => {
const response = await requestRetry({
const response = await request({
method: 'POST',
url: `${BASE_URL}/get`,
body: {
data: {
kind: KIND,
key: NAME,
},
json: true,
});

assert.strictEqual(response.statusCode, 200);
assert.deepStrictEqual(response.body, {
assert.strictEqual(response.status, 200);
assert.deepStrictEqual(response.data, {
description: 'Buy milk',
});
});
Expand Down Expand Up @@ -228,11 +230,12 @@ describe('functions/datastore', () => {
const PORT = 8082;
const BASE_URL = `http://localhost:${PORT}`;

before(() => {
before(async () => {
ffProc = execPromise(
`functions-framework --target=del --signature-type=http --port=${PORT}`,
{timeout: FF_TIMEOUT, shell: true, cwd}
);
await waitForReady(BASE_URL);
});

after(async () => {
Expand Down Expand Up @@ -272,32 +275,31 @@ describe('functions/datastore', () => {
});

it(`del: Doesn't fail when entity does not exist`, async () => {
const response = await requestRetry({
const response = await request({
method: 'POST',
url: `${BASE_URL}/del`,
body: {
data: {
kind: KIND,
key: 'nonexistent',
},
json: true,
responseType: 'text',
});

assert.strictEqual(response.statusCode, 200);
assert.strictEqual(response.body, `Entity ${KIND}/nonexistent deleted.`);
assert.strictEqual(response.status, 200);
assert.strictEqual(response.data, `Entity ${KIND}/nonexistent deleted.`);
});

it('del: Deletes an entity', async () => {
const response = await requestRetry({
const response = await request({
method: 'POST',
url: `${BASE_URL}/del`,
body: {
data: {
kind: KIND,
key: NAME,
},
json: true,
responseType: 'text',
});
assert.strictEqual(response.statusCode, 200);
assert.strictEqual(response.body, `Entity ${KIND}/${NAME} deleted.`);
assert.strictEqual(response.status, 200);
assert.strictEqual(response.data, `Entity ${KIND}/${NAME} deleted.`);

const key = datastore.key([KIND, NAME]);
const [entity] = await datastore.get(key);
Expand Down