Skip to content

Commit

Permalink
Fix async mode of fs.readdir (jerryscript-project#619)
Browse files Browse the repository at this point in the history
IoT.js-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]
  • Loading branch information
dbatyai authored and yichoi committed Feb 10, 2017
1 parent 63ba2af commit 41e7c25
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/module/iotjs_module_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,11 @@ JHANDLER_FUNCTION(Rename) {
JHANDLER_FUNCTION(ReadDir) {
JHANDLER_CHECK_THIS(object);
JHANDLER_CHECK_ARGS(1, string);
JHANDLER_CHECK_ARG_IF_EXIST(2, function);
JHANDLER_CHECK_ARG_IF_EXIST(1, function);

const iotjs_environment_t* env = iotjs_environment_get();
iotjs_string_t path = JHANDLER_GET_ARG(0, string);
const iotjs_jval_t* jcallback = JHANDLER_GET_ARG_IF_EXIST(2, function);
const iotjs_jval_t* jcallback = JHANDLER_GET_ARG_IF_EXIST(1, function);

if (jcallback) {
FS_ASYNC(env, scandir, jcallback, iotjs_string_data(&path), 0);
Expand Down
40 changes: 40 additions & 0 deletions test/run_fail/fs_callbacks_called.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


var assert = require('assert')
var invocation_count = 0;
var callback_count = 13;

var callback = function () {
if (++invocation_count == callback_count) {
assert.fail("pass") // All the callbacks were called
}
}

var fs = require('fs');
fs.open("", "r", callback);
fs.close(0, callback);
fs.read(0, Buffer(1), 0, 0, 0, callback);
fs.write(0, Buffer(1), 0, 0, 0, callback);
fs.readFile("", callback);
fs.writeFile("", Buffer(1), callback);
fs.rename("", "", callback);
fs.stat("", callback);
fs.exists("", callback);
fs.unlink("", callback);
fs.mkdir("", callback);
fs.rmdir("", callback);
fs.readdir("", callback);
1 change: 1 addition & 0 deletions test/testsets.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function testsets() {
'assert_fail1.js',
'assert_fail2.js',
'assert_fail3.js',
'fs_callbacks_called.js',
'process_exit1.js',
'process_exit2.js',
'process_exitcode1.js',
Expand Down
9 changes: 7 additions & 2 deletions tools/check_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ Driver.prototype.finish = function() {
if (this.results.fail > 0 || this.results.timeout > 0) {
originalExit(1);
}

originalExit(0);
};

var driver = new Driver();
Expand All @@ -227,14 +229,15 @@ process.exit = function(code) {
// this function is called when the following happens.
// 1. the test case is finished normally.
// 2. assertion inside the callback function is failed.
var should_fail = driver.runner.attr.fail;
try {
process.emitExit(code);
} catch(e) {
// when assertion inside the process.on('exit', function { ... }) is failed,
// this procedure is executed.
process.removeAllListeners('exit');

if (driver.runner.attr.fail) {
if (should_fail) {
driver.runner.finish('pass');
} else {
console.error(e);
Expand All @@ -243,7 +246,9 @@ process.exit = function(code) {
} finally {
process.removeAllListeners('exit');

if (code != 0) {
if (code != 0 && !should_fail) {
driver.runner.finish('fail');
} else if (code == 0 && should_fail) {
driver.runner.finish('fail');
} else {
driver.runner.finish('pass');
Expand Down

0 comments on commit 41e7c25

Please sign in to comment.