Skip to content

Commit af64c94

Browse files
committed
Protection against my own habit to forget to pass a callback.
1 parent 719a2d9 commit af64c94

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

lib/ntp-client.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
exports.defaultNtpPort = 123;
1515
exports.defaultNtpServer = "pool.ntp.org";
16-
16+
1717
/**
1818
* Amount of acceptable time to await for a response from the remote server.
1919
* Configured default to 10 seconds.
@@ -24,10 +24,17 @@
2424
* Fetches the current NTP Time from the given server and port.
2525
* @param {string} server IP/Hostname of the remote NTP Server
2626
* @param {number} port Remote NTP Server port number
27-
* @param {function(Object, Date)} callback(err, date) Async callback for
27+
* @param {function(Object, Date)} callback(err, date) Async callback for
2828
* the result date or eventually error.
2929
*/
3030
exports.getNetworkTime = function (server, port, callback) {
31+
if (callback === null || typeof callback !== "function") {
32+
return;
33+
}
34+
35+
server = server || exports.defaultNtpServer;
36+
port = port || exports.defaultNtpPort;
37+
3138
var client = dgram.createSocket("udp4"),
3239
ntpData = new Buffer(48);
3340

@@ -37,7 +44,7 @@
3744
ntpData[i] = 0;
3845
}
3946

40-
var timeout = setTimeout(function() {
47+
var timeout = setTimeout(function () {
4148
client.close();
4249
callback("Timeout waiting for NTP response.", null);
4350
}, exports.ntpReplyTimeout);
@@ -52,7 +59,7 @@
5259
client.once('message', function (msg) {
5360
clearTimeout(timeout);
5461
client.close();
55-
62+
5663
// Offset to get to the "Transmit Timestamp" field (time at which the reply
5764
// departed the server for the client, in 64-bit timestamp format."
5865
var offsetTransmitTime = 40,

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ntp-client",
33
"description": "Pure Javascript implementation of the NTP Client Protocol",
4-
"version": "0.5.1",
4+
"version": "0.5.2",
55
"homepage": "https://github.com/moonpyk/node-ntp-client",
66
"author": {
77
"name": "Clément Bourgeois",
@@ -26,10 +26,11 @@
2626
"test": "grunt jshint nodeunit"
2727
},
2828
"devDependencies": {
29+
"grunt": "~0.4.1",
2930
"grunt-contrib-jshint": "~0.6.0",
3031
"grunt-contrib-nodeunit": "~0.2.0",
3132
"grunt-contrib-watch": "~0.4.0",
32-
"grunt": "~0.4.1"
33+
"nodeunit": "~0.8.1"
3334
},
3435
"bin": {
3536
"node-ntp-client": "bin/node-ntp-client"
@@ -38,4 +39,4 @@
3839
"ntp",
3940
"date"
4041
]
41-
}
42+
}

test/ntp-client_test.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,29 @@
2525

2626
exports.setUp = function (done) {
2727
// setup here
28+
ntpClient.ntpReplyTimeout = 5000; // Reducing timeout to avoid warnings.
2829
done();
2930
};
3031

31-
exports.validNTPServer = function (test) {
32-
ntpClient.ntpReplyTimeout = 5000; // Reducing timeout to avoid warnings.
32+
exports.invalidUsage = function (test) {
33+
test.doesNotThrow(function () {
34+
ntpClient.getNetworkTime(ntpClient.defaultNtpServer, ntpClient.defaultNtpPort);
35+
});
3336

34-
ntpClient.getNetworkTime(ntpClient.defaultNtpServer, ntpClient.defaultNtpPort, function (err, date) {
35-
var now = new Date();
37+
test.done();
38+
};
3639

40+
exports.notBitchyAboutSomeParameters = function (test) {
41+
ntpClient.getNetworkTime(null, null, function (err, date) {
42+
test.ok(date !== null);
43+
test.done();
44+
});
45+
};
46+
47+
exports.validNTPServer = function (test) {
48+
ntpClient.getNetworkTime(ntpClient.defaultNtpServer, ntpClient.defaultNtpPort, function (err, date) {
3749
console.log();
38-
console.log("System reported : %s", now);
50+
console.log("System reported : %s", new Date());
3951

4052
test.ok(err === null);
4153
test.ok(date !== null);

0 commit comments

Comments
 (0)