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 29, 2016
1 parent 89d2937 commit 2c3391c
Show file tree
Hide file tree
Showing 41 changed files with 1,130 additions and 1,334 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
4 changes: 2 additions & 2 deletions datastore/concepts.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,7 @@ Query.prototype.testRunQueryProjection = function(callback) {

self.datastore.runQuery(query, function(err) {
if (err) {
callback(err);
return;
return callback(err);
}

queryCallback.apply(null, arguments);
Expand All @@ -808,6 +807,7 @@ Query.prototype.testRunQueryProjection = function(callback) {
var percentCompletes = [];

datastore.runQuery(query, function(err, tasks) {
console.log(err, tasks);
if (err) {
// An error occurred while running the query.
return;
Expand Down
2 changes: 1 addition & 1 deletion datastore/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ function runQuery(cb) {
exports.runQuery = runQuery;

if (module === require.main) {
runQuery(function () {});
runQuery(console.log);
}
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();
}
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();
}
Loading

0 comments on commit 2c3391c

Please sign in to comment.