Skip to content

Commit

Permalink
Refactor for completely parallel tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Mar 28, 2016
1 parent 89d2937 commit f3142ca
Show file tree
Hide file tree
Showing 32 changed files with 797 additions and 769 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ test/encrypted/nodejs-docs-samples.json
dump.rdb
logs/
*.iml
.idea/
.idea/
.nyc_output
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ before_install:
- npm set progress=false

after_success:
- npm run coveralls
- npm run report
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,29 @@ a service account file. You can download one from your Google project's
"permissions" page.
1. `npm test`

Since the tests use [Mocha.js](https://mochajs.org/), you can use the `--grep`
option to run only the tests that match a provided pattern. The `--invert`
option causes the matched tests to be excluded instead of included.
Since the tests use [AVA](https://github.com/sindresorhus/ava), you can use the
`--match` option to run only the tests that match a provided pattern.

__Run only the tests that match a pattern:__


npm test -- -- --grep <pattern>
npm test -- -- --match="<pattern>"

__Only run the tests for the `datastore` sample:__

npm test -- -- --grep datastore
npm test -- -- --match="datastore"

__Skip the tests that match a pattern:__

npm test -- -- --grep <pattern> --invert
npm test -- -- --match="!<pattern>"

__Run all but the `datastore` tests:__

npm test -- -- --grep datastore --invert
npm test -- -- --match="!datastore"

__Skip the tests that require Redis and Memcached:__

npm test -- -- --grep "express-memcached-session|redis" --invert
npm test -- -- --match="express-memcached-session|redis"

## License

Expand Down
95 changes: 64 additions & 31 deletions logging/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,70 +28,103 @@ var logging = gcloud.logging();
// [END setup]

// [START listSinks]
function listSinks(callback) {
/**
* @param {Function} callback Callback function.
*/
function listSinksExample(callback) {
// list all sinks in the authenticated project
logging.getSinks(callback);
logging.getSinks(function (err, sinks) {
if (err) {
return callback(err);
}

// Should have received all sinks
console.log('Found ' + sinks.length + ' sinks');
callback(null, sinks);
});
}
// [END listSinks]

// [START createSink]
function createSink(callback) {
// Get a reference to the Cloud Storage component
var gcs = gcloud.storage();

/**
* @param {string} sinkName Name of the new sink.
* @param {Object} config Configuration options for the new sink.
* @param {Function} callback Callback function.
*/
function createSinkExample(sinkName, config, callback) {
// create a new sink in the authenticated project
//
// This method only works if you are authenticated as yourself, e.g. using the
// gcloud SDK.
logging.createSink('mySink', {
destination: gcs.bucket('logging-bucket')
}, callback);
logging.createSink(sinkName, config, function (err, sink, apiResponse) {
if (err) {
return callback(err);
}

// Should have received newly created sink
console.log('Created ' + sinkName, sink);
callback(null, sink, apiResponse);
});
}
// [END createSink]

// [START updateSink]
function updateSink(callback) {
// Get a reference to the Cloud Storage component
var gcs = gcloud.storage();
/**
* @param {string} sinkName Name of the sink to update.
* @param {Object} config New configuration options for the sink.
* @param {Function} callback Callback function.
*/
function updateSinkExample(sinkName, config, callback) {
// Get a reference to an existing sink
var sink = logging.sink('mySink');
var sink = logging.sink(sinkName);

// update a sink
//
// This method only works if you are authenticated as yourself, e.g. using the
// gcloud SDK.
sink.setMetadata({
// change destination to something else
destination: gcs.bucket('other-logging-bucket')
}, callback);
sink.setMetadata(config, function (err, apiResponse) {
if (err) {
return callback(err);
}

console.log('Updated ' + sinkName);
callback(null, apiResponse);
});
}
// [END updateSink]

// [START deleteSink]
function deleteSink(callback) {
/**
* @param {string} sinkName Name of the sink to delete.
* @param {Function} callback Callback function.
*/
function deleteSinkExample(sinkName, callback) {
// Get a reference to an existing sink
var sink = logging.sink('mySink');
var sink = logging.sink(sinkName);

// delete a sink
//
// This method only works if you are authenticated as yourself, e.g. using the
// gcloud SDK.
sink.delete(callback);
sink.delete(function (err, apiResponse) {
if (err) {
return callback(err);
}

console.log('Deleted ' + sinkName);
callback(null, apiResponse);
});
}
// [END deleteSink]

exports.runExample = function (cb) {
listSinks(function (err, sinks, apiResponse) {
console.log(err, 'sinks:', sinks, 'apiResponse:', apiResponse);
if (typeof cb === 'function') {
cb(err, sinks);
}
});
// Run the examples
exports.main = function (cb) {
listSinksExample(cb || console.log);
};
exports.createSink = createSink;
exports.updateSink = updateSink;
exports.deleteSink = deleteSink;
exports.createSinkExample = createSinkExample;
exports.updateSinkExample = updateSinkExample;
exports.deleteSinkExample = deleteSinkExample;

if (module === require.main) {
exports.runExample();
exports.main();
}
37 changes: 23 additions & 14 deletions logging/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,33 @@ var gcloud = require('gcloud')({
// Get a reference to the logging component
var logging = gcloud.logging();

function list(callback) {
// Retrieve the latest 3 log entries from the authenticated project.
logging.getEntries({
pageSize: 3
}, callback);
}
// [END list]
/**
* @param {Object} [options] Configuration options for the request.
* @param {Function} callback Callback function.
*/
function listExample(options, callback) {
if (typeof options === 'function') {
callback = options;
}

exports.runExample = function (cb) {
console.log('retrieving latest 3 log entries...');
list(function (err, entries, apiResponse) {
console.log(err, 'entries:', entries, 'apiResponse:', apiResponse);
if (typeof cb === 'function') {
cb(err, entries, apiResponse);
// Retrieve the latest some log entries from the authenticated project.
logging.getEntries(options, function (err, entries, nextQuery, apiResponse) {
if (err) {
return callback(err);
}

// Should have received some log entries
console.log('Found ' + entries.length + ' entries');
callback(null, entries, nextQuery, apiResponse);
});
}
// [END list]

// Run the examples
exports.main = function (options, cb) {
listExample(options || { pageSize: 1 }, cb || console.log);
};

if (module === require.main) {
exports.runExample();
exports.main();
}
2 changes: 1 addition & 1 deletion logging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"export": "node export.js"
},
"dependencies": {
"gcloud": "^0.27.0"
"gcloud": "^0.29.0"
}
}
74 changes: 40 additions & 34 deletions logging/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/* jshint camelcase:false */
'use strict';

var async = require('async');

// [START write]
// [START setup]
// You must set the GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT
Expand All @@ -29,9 +31,13 @@ var gcloud = require('gcloud')({
var logging = gcloud.logging();
// [END setup]

function write(callback) {
/**
* @param {string} logName Name of the log to write to.
* @param {Function} callback Callback function.
*/
function writeExample(logName, callback) {
// Get a reference to an existing log
var log = logging.log('myLog');
var log = logging.log(logName);

// Modify this resource type to match a resource in your project
// See https://cloud.google.com/logging/docs/api/ref_v2beta1/rest \
Expand Down Expand Up @@ -61,50 +67,50 @@ function write(callback) {
log.write([
entry,
secondEntry
], callback);
], function (err, apiResponse) {
if (err) {
return callback(err);
}

console.log('Wrote to ' + logName);
callback(null, apiResponse);
});
}
// [END write]

// [START deleteLog]
function deleteLog(callback) {
/**
* @param {string} logName Name of the log to delete.
* @param {Function} callback Callback function.
*/
function deleteLogExample(logName, callback) {
// Get a reference to an existing log
var log = logging.log('myLog');
var log = logging.log(logName);

// Delete the log
log.delete(callback);
log.delete(function (err, apiResponse) {
if (err) {
return callback(err);
}

console.log('Deleted ' + logName);
callback(null, apiResponse);
});
}
// [END deleteLog]

exports.runExample = function (cb) {
var result = [];
console.log('writing 2 log entries...');
write(function (err, apiResponse) {
console.log(err, 'apiResponse:', apiResponse);
if (err) {
return typeof cb === 'function' ? cb(err) : undefined;
// Run the examples
exports.main = function (cb) {
async.series([
function (cb) {
writeExample('myLog', cb);
},
function (cb) {
deleteLogExample('myLog', cb);
}
result.push(apiResponse);
console.log('success!');
console.log('deleting the log entries...');
// If you remove this code, then you can find the two log entries that
// were written in the log view in the cloud console.
deleteLog(function (err, apiResponse) {
console.log(err, 'apiResponse:', apiResponse);
if (err && err.code === 404) {
err = undefined;
apiResponse = {};
}
if (!err) {
console.log('success!');
}
result.push(apiResponse);
if (typeof cb === 'function') {
cb(err, result);
}
});
});
], cb || console.log);
};

if (module === require.main) {
exports.runExample();
exports.main();
}
27 changes: 20 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
},
"scripts": {
"jshint": "jshint --exclude-path=.jshintignore .",
"mocha": "mocha --timeout 10000 --recursive",
"cover": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- -t 30000 --recursive",
"coveralls": "cat ./coverage/lcov.info | node_modules/.bin/coveralls",
"deps_appengine": "ava --match='*: dependencies should install*'",
"ava": "npm run deps_appengine && ava --match='!*: dependencies should install*'",
"cover": "npm run deps_appengine && nyc ava --match='!*: dependencies should install*'",
"report": "nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls",
"report-html": "nyc report --reporter=html",
"deps_datastore": "cd datastore; npm i; cd ../",
"deps_pubsub": "cd pubsub; npm i; cd ../",
"deps_storage": "cd storage; npm i; cd ../",
Expand All @@ -39,13 +41,24 @@
"pretest": "npm run deps_datastore; npm run deps_storage; npm run deps_pubsub; npm run deps_prediction; npm run deps_logging; npm run deps_functions; npm run deps_sendgrid; npm run pretest_geddy",
"test": "npm run jshint && npm run cover"
},
"ava": {
"files": [
"test/appengine/*.test.js",
"test/computeengine/*.test.js",
"test/functions/*.test.js",
"test/logging/*.test.js",
"test/prediction/*.test.js",
"test/pubsub/*.test.js",
"test/storage/*.test.js"
]
},
"devDependencies": {
"async": "^1.5.2",
"coveralls": "^2.11.6",
"googleapis": "^2.1.7",
"istanbul": "^0.4.2",
"ava": "^0.13.0",
"coveralls": "^2.11.9",
"googleapis": "^4.0.0",
"jshint": "~2.9.1",
"mocha": "^2.4.5",
"nyc": "^6.1.1",
"proxyquire": "^1.7.4",
"request": "^2.69.0",
"supertest": "^1.1.0"
Expand Down
Loading

0 comments on commit f3142ca

Please sign in to comment.