Skip to content

Instantly share code, notes, and snippets.

@othiym23
Last active January 29, 2016 21:56
Show Gist options
  • Save othiym23/de0fc88dd204ae368dc5 to your computer and use it in GitHub Desktop.
Save othiym23/de0fc88dd204ae368dc5 to your computer and use it in GitHub Desktop.
can't get this test to work with newer versions of tap
'use strict';
var createNamespace = require('continuation-local-storage').createNamespace
, fs = require('fs')
, tap = require('tap')
, test = tap.test
;
// CONSTANTS
var FILENAME = '__testfile';
function createFile(assert) {
var contents = new Buffer("UHOH")
, file = fs.openSync(FILENAME, 'w')
, written = fs.writeSync(file, contents, 0, contents.length, 0)
;
assert.equals(written, contents.length, "whole buffer was written");
var rc = fs.closeSync(file);
// need this here to avoid dealing with umask complications
fs.chmodSync(FILENAME, '0666');
return rc;
}
function deleteFile() { return fs.unlinkSync(FILENAME); }
test("continuation-local state with MakeCallback and fs module", function (t) {
t.plan(1);
var namespace = createNamespace('fs');
namespace.run(function () {
namespace.set('test', 0xabad1dea);
t.test("fs.watchFile", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'watchFile');
t.equal(namespace.get('test'), 'watchFile', "state has been mutated");
fs.watchFile(FILENAME,
{persistent : false, interval : 20},
function (before, after) {
t.equal(namespace.get('test'), 'watchFile',
"mutated state has persisted to fs.watchFile's callback");
t.ok(before.ino, "file has an entry");
t.equal(before.ino, after.ino, "file is at the same location");
fs.unwatchFile(FILENAME);
process.nextTick(function () {
deleteFile();
t.end();
});
});
setTimeout(function poke() {
fs.appendFileSync(FILENAME, 'still a test');
}, 20);
});
});
});
});
{
"name": "test-cls-watchfile",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Forrest L Norvell <ogd@aoaioxxysz.net>",
"license": "ISC",
"dependencies": {
"continuation-local-storage": "^3.1.4"
},
"devDependencies": {
"tap": "^5.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment