Skip to content

Commit 858ebab

Browse files
committed
Refactor for completely parallel tests.
1 parent 89d2937 commit 858ebab

32 files changed

+798
-769
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ test/encrypted/nodejs-docs-samples.json
66
dump.rdb
77
logs/
88
*.iml
9-
.idea/
9+
.idea/
10+
.nyc_output

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@ before_install:
8383
- npm set progress=false
8484

8585
after_success:
86-
- npm run coveralls
86+
- npm run report

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,30 +129,29 @@ a service account file. You can download one from your Google project's
129129
"permissions" page.
130130
1. `npm test`
131131

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

136135
__Run only the tests that match a pattern:__
137136

138137

139-
npm test -- -- --grep <pattern>
138+
npm test -- -- --match="<pattern>"
140139

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

143-
npm test -- -- --grep datastore
142+
npm test -- -- --match="datastore"
144143

145144
__Skip the tests that match a pattern:__
146145

147-
npm test -- -- --grep <pattern> --invert
146+
npm test -- -- --match="!<pattern>"
148147

149148
__Run all but the `datastore` tests:__
150149

151-
npm test -- -- --grep datastore --invert
150+
npm test -- -- --match="!datastore"
152151

153152
__Skip the tests that require Redis and Memcached:__
154153

155-
npm test -- -- --grep "express-memcached-session|redis" --invert
154+
npm test -- -- --match="express-memcached-session|redis"
156155

157156
## License
158157

logging/export.js

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,70 +28,103 @@ var logging = gcloud.logging();
2828
// [END setup]
2929

3030
// [START listSinks]
31-
function listSinks(callback) {
31+
/**
32+
* @param {Function} callback Callback function.
33+
*/
34+
function listSinksExample(callback) {
3235
// list all sinks in the authenticated project
33-
logging.getSinks(callback);
36+
logging.getSinks(function (err, sinks) {
37+
if (err) {
38+
return callback(err);
39+
}
40+
41+
// Should have received all sinks
42+
console.log('Found ' + sinks.length + ' sinks');
43+
callback(null, sinks);
44+
});
3445
}
3546
// [END listSinks]
3647

3748
// [START createSink]
38-
function createSink(callback) {
39-
// Get a reference to the Cloud Storage component
40-
var gcs = gcloud.storage();
41-
49+
/**
50+
* @param {string} sinkName Name of the new sink.
51+
* @param {Object} config Configuration options for the new sink.
52+
* @param {Function} callback Callback function.
53+
*/
54+
function createSinkExample(sinkName, config, callback) {
4255
// create a new sink in the authenticated project
4356
//
4457
// This method only works if you are authenticated as yourself, e.g. using the
4558
// gcloud SDK.
46-
logging.createSink('mySink', {
47-
destination: gcs.bucket('logging-bucket')
48-
}, callback);
59+
logging.createSink(sinkName, config, function (err, sink, apiResponse) {
60+
if (err) {
61+
return callback(err);
62+
}
63+
64+
// Should have received newly created sink
65+
console.log('Created ' + sinkName, sink);
66+
callback(null, sink, apiResponse);
67+
});
4968
}
5069
// [END createSink]
5170

5271
// [START updateSink]
53-
function updateSink(callback) {
54-
// Get a reference to the Cloud Storage component
55-
var gcs = gcloud.storage();
72+
/**
73+
* @param {string} sinkName Name of the sink to update.
74+
* @param {Object} config New configuration options for the sink.
75+
* @param {Function} callback Callback function.
76+
*/
77+
function updateSinkExample(sinkName, config, callback) {
5678
// Get a reference to an existing sink
57-
var sink = logging.sink('mySink');
79+
var sink = logging.sink(sinkName);
5880

5981
// update a sink
6082
//
6183
// This method only works if you are authenticated as yourself, e.g. using the
6284
// gcloud SDK.
63-
sink.setMetadata({
64-
// change destination to something else
65-
destination: gcs.bucket('other-logging-bucket')
66-
}, callback);
85+
sink.setMetadata(config, function (err, apiResponse) {
86+
if (err) {
87+
return callback(err);
88+
}
89+
90+
console.log('Updated ' + sinkName);
91+
callback(null, apiResponse);
92+
});
6793
}
6894
// [END updateSink]
6995

7096
// [START deleteSink]
71-
function deleteSink(callback) {
97+
/**
98+
* @param {string} sinkName Name of the sink to delete.
99+
* @param {Function} callback Callback function.
100+
*/
101+
function deleteSinkExample(sinkName, callback) {
72102
// Get a reference to an existing sink
73-
var sink = logging.sink('mySink');
103+
var sink = logging.sink(sinkName);
74104

75105
// delete a sink
76106
//
77107
// This method only works if you are authenticated as yourself, e.g. using the
78108
// gcloud SDK.
79-
sink.delete(callback);
109+
sink.delete(function (err, apiResponse) {
110+
if (err) {
111+
return callback(err);
112+
}
113+
114+
console.log('Deleted ' + sinkName);
115+
callback(null, apiResponse);
116+
});
80117
}
81118
// [END deleteSink]
82119

83-
exports.runExample = function (cb) {
84-
listSinks(function (err, sinks, apiResponse) {
85-
console.log(err, 'sinks:', sinks, 'apiResponse:', apiResponse);
86-
if (typeof cb === 'function') {
87-
cb(err, sinks);
88-
}
89-
});
120+
// Run the examples
121+
exports.main = function (cb) {
122+
listSinksExample(cb || console.log);
90123
};
91-
exports.createSink = createSink;
92-
exports.updateSink = updateSink;
93-
exports.deleteSink = deleteSink;
124+
exports.createSinkExample = createSinkExample;
125+
exports.updateSinkExample = updateSinkExample;
126+
exports.deleteSinkExample = deleteSinkExample;
94127

95128
if (module === require.main) {
96-
exports.runExample();
129+
exports.main();
97130
}

logging/list.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,33 @@ var gcloud = require('gcloud')({
3030
// Get a reference to the logging component
3131
var logging = gcloud.logging();
3232

33-
function list(callback) {
34-
// Retrieve the latest 3 log entries from the authenticated project.
35-
logging.getEntries({
36-
pageSize: 3
37-
}, callback);
38-
}
39-
// [END list]
33+
/**
34+
* @param {Object} [options] Configuration options for the request.
35+
* @param {Function} callback Callback function.
36+
*/
37+
function listExample(options, callback) {
38+
if (typeof options === 'function') {
39+
callback = options;
40+
}
4041

41-
exports.runExample = function (cb) {
42-
console.log('retrieving latest 3 log entries...');
43-
list(function (err, entries, apiResponse) {
44-
console.log(err, 'entries:', entries, 'apiResponse:', apiResponse);
45-
if (typeof cb === 'function') {
46-
cb(err, entries, apiResponse);
42+
// Retrieve the latest some log entries from the authenticated project.
43+
logging.getEntries(options, function (err, entries, nextQuery, apiResponse) {
44+
if (err) {
45+
return callback(err);
4746
}
47+
48+
// Should have received some log entries
49+
console.log('Found ' + entries.length + ' entries');
50+
callback(null, entries, nextQuery, apiResponse);
4851
});
52+
}
53+
// [END list]
54+
55+
// Run the examples
56+
exports.main = function (options, cb) {
57+
listExample(options || { pageSize: 1 }, cb || console.log);
4958
};
5059

5160
if (module === require.main) {
52-
exports.runExample();
61+
exports.main();
5362
}

logging/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"export": "node export.js"
1515
},
1616
"dependencies": {
17-
"gcloud": "^0.27.0"
17+
"gcloud": "^0.29.0"
1818
}
1919
}

logging/write.js

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
/* jshint camelcase:false */
1515
'use strict';
1616

17+
var async = require('async');
18+
1719
// [START write]
1820
// [START setup]
1921
// You must set the GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT
@@ -29,9 +31,13 @@ var gcloud = require('gcloud')({
2931
var logging = gcloud.logging();
3032
// [END setup]
3133

32-
function write(callback) {
34+
/**
35+
* @param {string} logName Name of the log to write to.
36+
* @param {Function} callback Callback function.
37+
*/
38+
function writeExample(logName, callback) {
3339
// Get a reference to an existing log
34-
var log = logging.log('myLog');
40+
var log = logging.log(logName);
3541

3642
// Modify this resource type to match a resource in your project
3743
// See https://cloud.google.com/logging/docs/api/ref_v2beta1/rest \
@@ -61,50 +67,50 @@ function write(callback) {
6167
log.write([
6268
entry,
6369
secondEntry
64-
], callback);
70+
], function (err, apiResponse) {
71+
if (err) {
72+
return callback(err);
73+
}
74+
75+
console.log('Wrote to ' + logName);
76+
callback(null, apiResponse);
77+
});
6578
}
6679
// [END write]
6780

6881
// [START deleteLog]
69-
function deleteLog(callback) {
82+
/**
83+
* @param {string} logName Name of the log to delete.
84+
* @param {Function} callback Callback function.
85+
*/
86+
function deleteLogExample(logName, callback) {
7087
// Get a reference to an existing log
71-
var log = logging.log('myLog');
88+
var log = logging.log(logName);
7289

7390
// Delete the log
74-
log.delete(callback);
91+
log.delete(function (err, apiResponse) {
92+
if (err) {
93+
return callback(err);
94+
}
95+
96+
console.log('Deleted ' + logName);
97+
callback(null, apiResponse);
98+
});
7599
}
76100
// [END deleteLog]
77101

78-
exports.runExample = function (cb) {
79-
var result = [];
80-
console.log('writing 2 log entries...');
81-
write(function (err, apiResponse) {
82-
console.log(err, 'apiResponse:', apiResponse);
83-
if (err) {
84-
return typeof cb === 'function' ? cb(err) : undefined;
102+
// Run the examples
103+
exports.main = function (cb) {
104+
async.series([
105+
function (cb) {
106+
writeExample('myLog', cb);
107+
},
108+
function (cb) {
109+
deleteLogExample('myLog', cb);
85110
}
86-
result.push(apiResponse);
87-
console.log('success!');
88-
console.log('deleting the log entries...');
89-
// If you remove this code, then you can find the two log entries that
90-
// were written in the log view in the cloud console.
91-
deleteLog(function (err, apiResponse) {
92-
console.log(err, 'apiResponse:', apiResponse);
93-
if (err && err.code === 404) {
94-
err = undefined;
95-
apiResponse = {};
96-
}
97-
if (!err) {
98-
console.log('success!');
99-
}
100-
result.push(apiResponse);
101-
if (typeof cb === 'function') {
102-
cb(err, result);
103-
}
104-
});
105-
});
111+
], cb || console.log);
106112
};
107113

108114
if (module === require.main) {
109-
exports.runExample();
115+
exports.main();
110116
}

package.json

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
},
2626
"scripts": {
2727
"jshint": "jshint --exclude-path=.jshintignore .",
28-
"mocha": "mocha --timeout 10000 --recursive",
29-
"cover": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- -t 30000 --recursive",
30-
"coveralls": "cat ./coverage/lcov.info | node_modules/.bin/coveralls",
28+
"deps_appengine": "ava --match='*: dependencies should install*'",
29+
"ava": "npm run deps_appengine && ava --match='!*: dependencies should install*'",
30+
"cover": "npm run deps_appengine && nyc ava --match='!*: dependencies should install*'",
31+
"report": "nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls",
32+
"report-html": "nyc report --reporter=html",
3133
"deps_datastore": "cd datastore; npm i; cd ../",
3234
"deps_pubsub": "cd pubsub; npm i; cd ../",
3335
"deps_storage": "cd storage; npm i; cd ../",
@@ -39,13 +41,24 @@
3941
"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",
4042
"test": "npm run jshint && npm run cover"
4143
},
44+
"ava": {
45+
"files": [
46+
"test/appengine/*.test.js",
47+
"test/computeengine/*.test.js",
48+
"test/functions/*.test.js",
49+
"test/logging/*.test.js",
50+
"test/prediction/*.test.js",
51+
"test/pubsub/*.test.js",
52+
"test/storage/*.test.js"
53+
]
54+
},
4255
"devDependencies": {
4356
"async": "^1.5.2",
44-
"coveralls": "^2.11.6",
45-
"googleapis": "^2.1.7",
46-
"istanbul": "^0.4.2",
57+
"ava": "^0.13.0",
58+
"coveralls": "^2.11.9",
59+
"googleapis": "^4.0.0",
4760
"jshint": "~2.9.1",
48-
"mocha": "^2.4.5",
61+
"nyc": "^6.1.1",
4962
"proxyquire": "^1.7.4",
5063
"request": "^2.69.0",
5164
"supertest": "^1.1.0"

0 commit comments

Comments
 (0)