Server IP : 127.0.0.2 / Your IP : 18.217.119.115 Web Server : Apache/2.4.18 (Ubuntu) System : User : www-data ( ) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : disk_free_space,disk_total_space,diskfreespace,dl,exec,fpaththru,getmyuid,getmypid,highlight_file,ignore_user_abord,leak,listen,link,opcache_get_configuration,opcache_get_status,passthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,php_uname,phpinfo,posix_ctermid,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix,_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_times,posix_ttyname,posix_uname,pclose,popen,proc_open,proc_close,proc_get_status,proc_nice,proc_terminate,shell_exec,source,show_source,system,virtual MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/share/npm/node_modules/lockfile/test/ |
Upload File : |
var fs = require('fs') var lockFile = require('../') var test = require('tap').test var path = require('path') var lock = path.resolve(__dirname, 'stale.lock') var touch = require('touch') var spawn = require('child_process').spawn var node = process.execPath // We're using a lockfile with an artificially old date, // so make it use that instead of ctime. // Probably you should never do this in production! lockFile.filetime = 'mtime' if (process.argv[2] === 'child') { return child() } function child () { // Make fs.stat take 100ms to return its data // This is important because, in a test scenario where // we're statting the same exact file rapid-fire like this, // it'll end up being cached by the FS, and never trigger // the race condition we're trying to expose. fs.stat = function (stat) { return function () { var args = [].slice.call(arguments) var cb = args.pop() stat.apply(fs, args.concat(function(er, st) { setTimeout(function () { cb(er, st) }, 100) })) }}(fs.stat) lockFile.lock(lock, { stale: 100000 }, function (er) { if (er && er.code !== 'EEXIST') throw er else if (er) process.exit(17) else setTimeout(function(){}, 500) }) } test('create stale file', function (t) { try { fs.unlinkSync(lock) } catch (er) {} touch.sync(lock, { time: '1979-07-01T19:10:00.000Z' }) t.end() }) test('contenders', function (t) { var n = 10 var fails = 0 var wins = 0 var args = [ __filename, 'child' ] var opt = { stdio: [0, "pipe", 2] } for (var i = 0; i < n; i++) { spawn(node, args, opt).on('close', then) } function then (code) { if (code === 17) { fails ++ } else if (code) { t.fail("unexpected failure", code) fails ++ } else { wins ++ } if (fails + wins === n) { done() } } function done () { t.equal(wins, 1, "should have 1 lock winner") t.equal(fails, n - 1, "all others should lose") t.end() } }) test('remove stale file', function (t) { try { fs.unlinkSync(lock) } catch (er) {} t.end() })