Skip to content

Instantly share code, notes, and snippets.

@vorble
Created December 11, 2021 06:50
Show Gist options
  • Save vorble/e3331abd6617fa8c9ae1dd177fbfcffa to your computer and use it in GitHub Desktop.
Save vorble/e3331abd6617fa8c9ae1dd177fbfcffa to your computer and use it in GitHub Desktop.
setup('WriteStream (write failure)', mustCall((client, server) => {
let writes = 0;
const path_ = '/foo/bar/baz';
const handle_ = Buffer.from('hi mom!');
const data_ = Buffer.from('hello world');
const pflags_ = OPEN_MODE.TRUNC | OPEN_MODE.CREAT | OPEN_MODE.WRITE;
server.on('OPEN', mustCall((id, path, pflags, attrs) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
assert(pflags === pflags_, `Wrong flags: ${flagsToHuman(pflags)}`);
server.handle(id, handle_);
})).on('FSETSTAT', mustCall((id, handle, attrs) => {
assert(id === 1, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
assert.strictEqual(attrs.mode, 0o666, 'Wrong file mode');
server.status(id, STATUS_CODE.OK);
})).on('WRITE', mustCall((id, handle, offset, data) => {
assert(id === ++writes + 1, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
assert(offset === ((writes - 1) * data_.length),
`Wrong write offset: ${offset}`);
assert.deepStrictEqual(data, data_, 'Wrong data');
server.status(id, STATUS_CODE.FAILURE);
}, 1)).on('CLOSE', mustCall((id, handle) => {
assert(id === 3, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
server.status(id, STATUS_CODE.OK);
server.end();
}));
const writer = client.createWriteStream(path_);
writer.on('error', mustCall((err) => {
writer.destroy();
}));
writer.cork && writer.cork();
writer.write(data_);
writer.uncork && writer.uncork();
writer.end();
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment