Skip to content

Commit 0113943

Browse files
LaszloLangoyichoi
authored andcommitted
Change error handling of timers to be more error tolerant. (#946)
IoT.js-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 5fa262c commit 0113943

File tree

4 files changed

+74
-26
lines changed

4 files changed

+74
-26
lines changed

src/js/timers.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,12 @@ exports.setTimeout = timeoutConfigurator.bind(undefined, false);
9494
exports.setInterval = timeoutConfigurator.bind(undefined, true);
9595

9696
function clearTimeoutBase(timeoutType, timeout) {
97-
if (timeout && (timeout instanceof Timeout)) {
98-
timeout.unref();
99-
} else {
100-
throw new Error(timeoutType + '() - invalid timeout');
97+
if (timeout) {
98+
if (timeout instanceof Timeout) {
99+
timeout.unref();
100+
} else {
101+
throw new Error(timeoutType + '() - invalid timeout');
102+
}
101103
}
102104
}
103105

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
// Copyright Joyent, Inc. and other Node contributors.
17+
//
18+
// Permission is hereby granted, free of charge, to any person obtaining a
19+
// copy of this software and associated documentation files (the
20+
// "Software"), to deal in the Software without restriction, including
21+
// without limitation the rights to use, copy, modify, merge, publish,
22+
// distribute, sublicense, and/or sell copies of the Software, and to permit
23+
// persons to whom the Software is furnished to do so, subject to the
24+
// following conditions:
25+
//
26+
// The above copyright notice and this permission notice shall be included
27+
// in all copies or substantial portions of the Software.
28+
//
29+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
32+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
33+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
34+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
35+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
36+
37+
'use strict';
38+
require('node/common');
39+
var assert = require('assert');
40+
41+
// This test makes sure clearing timers with
42+
// 'null' or no input does not throw error
43+
44+
assert.doesNotThrow(function() { clearInterval(null) });
45+
46+
assert.doesNotThrow(function() { clearInterval() });
47+
48+
assert.doesNotThrow(function() { clearTimeout(null) });
49+
50+
assert.doesNotThrow(function() { clearTimeout() });
51+
52+
// assert.doesNotThrow(function() { clearImmediate(null) });
53+
54+
// assert.doesNotThrow(function() { clearImmediate() });

test/run_pass/test_timers_error.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,9 @@ assert.throws(function() {
4747
}, TypeError);
4848

4949
// ClearTimeout without timeout parameter.
50-
assert.throws(function() {
50+
assert.doesNotThrow(function() {
5151
clearTimeout();
52-
}, Error);
53-
54-
// ClearTimeout with invalid timeout parameter.
55-
assert.throws(function() {
56-
clearTimeout(timeout);
57-
}, Error);
52+
});
5853

5954
assert.throws(function() {
6055
clearTimeout('timeout');
@@ -64,13 +59,13 @@ assert.throws(function() {
6459
clearTimeout(1000);
6560
}, Error);
6661

67-
assert.throws(function() {
62+
assert.doesNotThrow(function() {
6863
clearTimeout(null);
69-
}, Error);
64+
});
7065

71-
assert.throws(function() {
66+
assert.doesNotThrow(function() {
7267
clearTimeout(undefined);
73-
}, Error);
68+
});
7469

7570
assert.throws(function() {
7671
clearTimeout({timeout: 1000});
@@ -107,22 +102,18 @@ assert.throws(function() {
107102
}, TypeError);
108103

109104
// ClearInterval without interval parameter.
110-
assert.throws(function() {
105+
assert.doesNotThrow(function() {
111106
clearInterval();
112-
}, Error);
107+
});
113108

114109
// ClearInterval with invalid interval parameter.
115-
assert.throws(function() {
110+
assert.doesNotThrow(function() {
116111
clearInterval(null);
117-
}, Error);
112+
});
118113

119-
assert.throws(function() {
114+
assert.doesNotThrow(function() {
120115
clearInterval(undefined);
121-
}, Error);
122-
123-
assert.throws(function() {
124-
clearInterval(interval);
125-
}, Error);
116+
});
126117

127118
assert.throws(function() {
128119
clearInterval('interval');

test/testsets.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
{ "name": "test-http-write-head.js" },
118118
{ "name": "test-net-bind-twice.js" },
119119
{ "name": "test-net-end-without-connect.js" },
120-
{ "name": "test-net-keepalive.js"}
120+
{ "name": "test-net-keepalive.js" },
121+
{ "name": "test-timers-clear-null-does-not-throw-error.js" }
121122
]
122123
}

0 commit comments

Comments
 (0)