CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
flush-sync.test.js141 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('tap')4const fs = require('fs')5const proxyquire = require('proxyquire')6const SonicBoom = require('../')7const { file, runTests } = require('./helper')8 9runTests(buildTests)10 11function buildTests (test, sync) {12  // Reset the umask for testing13  process.umask(0o000)14 15  test('flushSync', (t) => {16    t.plan(4)17 18    const dest = file()19    const fd = fs.openSync(dest, 'w')20    const stream = new SonicBoom({ fd, minLength: 4096, sync })21 22    t.ok(stream.write('hello world\n'))23    t.ok(stream.write('something else\n'))24 25    stream.flushSync()26 27    // let the file system settle down things28    setImmediate(function () {29      stream.end()30      const data = fs.readFileSync(dest, 'utf8')31      t.equal(data, 'hello world\nsomething else\n')32 33      stream.on('close', () => {34        t.pass('close emitted')35      })36    })37  })38}39 40test('retry in flushSync on EAGAIN', (t) => {41  t.plan(7)42 43  const fakeFs = Object.create(fs)44  const SonicBoom = proxyquire('../', {45    fs: fakeFs46  })47 48  const dest = file()49  const fd = fs.openSync(dest, 'w')50  const stream = new SonicBoom({ fd, sync: false, minLength: 0 })51 52  stream.on('ready', () => {53    t.pass('ready emitted')54  })55 56  t.ok(stream.write('hello world\n'))57 58  fakeFs.writeSync = function (fd, buf, enc) {59    t.pass('fake fs.write called')60    fakeFs.writeSync = fs.writeSync61    const err = new Error('EAGAIN')62    err.code = 'EAGAIN'63    throw err64  }65 66  t.ok(stream.write('something else\n'))67 68  stream.flushSync()69  stream.end()70 71  stream.on('finish', () => {72    fs.readFile(dest, 'utf8', (err, data) => {73      t.error(err)74      t.equal(data, 'hello world\nsomething else\n')75    })76  })77  stream.on('close', () => {78    t.pass('close emitted')79  })80})81 82test('throw error in flushSync on EAGAIN', (t) => {83  t.plan(12)84 85  const fakeFs = Object.create(fs)86  const SonicBoom = proxyquire('../', {87    fs: fakeFs88  })89 90  const dest = file()91  const fd = fs.openSync(dest, 'w')92  const stream = new SonicBoom({93    fd,94    sync: false,95    minLength: 1000,96    retryEAGAIN: (err, writeBufferLen, remainingBufferLen) => {97      t.equal(err.code, 'EAGAIN')98      t.equal(writeBufferLen, 12)99      t.equal(remainingBufferLen, 0)100      return false101    }102  })103 104  stream.on('ready', () => {105    t.pass('ready emitted')106  })107 108  const err = new Error('EAGAIN')109  err.code = 'EAGAIN'110  fakeFs.writeSync = function (fd, buf, enc) {111    Error.captureStackTrace(err)112    t.pass('fake fs.write called')113    fakeFs.writeSync = fs.writeSync114    throw err115  }116 117  fakeFs.fsyncSync = function (...args) {118    t.pass('fake fs.fsyncSync called')119    fakeFs.fsyncSync = fs.fsyncSync120    return fs.fsyncSync.apply(null, args)121  }122 123  t.ok(stream.write('hello world\n'))124  t.throws(stream.flushSync.bind(stream), err, 'EAGAIN')125 126  t.ok(stream.write('something else\n'))127  stream.flushSync()128 129  stream.end()130 131  stream.on('finish', () => {132    fs.readFile(dest, 'utf8', (err, data) => {133      t.error(err)134      t.equal(data, 'hello world\nsomething else\n')135    })136  })137  stream.on('close', () => {138    t.pass('close emitted')139  })140})141