You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. var fdSlicer = require('../');
  2. var fs = require('fs');
  3. var crypto = require('crypto');
  4. var path = require('path');
  5. var streamEqual = require('stream-equal');
  6. var assert = require('assert');
  7. var Pend = require('pend');
  8. var StreamSink = require('streamsink');
  9. var describe = global.describe;
  10. var it = global.it;
  11. var before = global.before;
  12. var beforeEach = global.beforeEach;
  13. var after = global.after;
  14. var testBlobFile = path.join(__dirname, "test-blob.bin");
  15. var testBlobFileSize = 20 * 1024 * 1024;
  16. var testOutBlobFile = path.join(__dirname, "test-blob-out.bin");
  17. describe("FdSlicer", function() {
  18. before(function(done) {
  19. var out = fs.createWriteStream(testBlobFile);
  20. for (var i = 0; i < testBlobFileSize / 1024; i += 1) {
  21. out.write(crypto.pseudoRandomBytes(1024));
  22. }
  23. out.end();
  24. out.on('close', done);
  25. });
  26. beforeEach(function() {
  27. try {
  28. fs.unlinkSync(testOutBlobFile);
  29. } catch (err) {
  30. }
  31. });
  32. after(function() {
  33. try {
  34. fs.unlinkSync(testBlobFile);
  35. fs.unlinkSync(testOutBlobFile);
  36. } catch (err) {
  37. }
  38. });
  39. it("reads a 20MB file (autoClose on)", function(done) {
  40. fs.open(testBlobFile, 'r', function(err, fd) {
  41. if (err) return done(err);
  42. var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
  43. var actualStream = slicer.createReadStream();
  44. var expectedStream = fs.createReadStream(testBlobFile);
  45. var pend = new Pend();
  46. pend.go(function(cb) {
  47. slicer.on('close', cb);
  48. });
  49. pend.go(function(cb) {
  50. streamEqual(expectedStream, actualStream, function(err, equal) {
  51. if (err) return done(err);
  52. assert.ok(equal);
  53. cb();
  54. });
  55. });
  56. pend.wait(done);
  57. });
  58. });
  59. it("reads 4 chunks simultaneously", function(done) {
  60. fs.open(testBlobFile, 'r', function(err, fd) {
  61. if (err) return done(err);
  62. var slicer = fdSlicer.createFromFd(fd);
  63. var actualPart1 = slicer.createReadStream({start: testBlobFileSize * 0/4, end: testBlobFileSize * 1/4});
  64. var actualPart2 = slicer.createReadStream({start: testBlobFileSize * 1/4, end: testBlobFileSize * 2/4});
  65. var actualPart3 = slicer.createReadStream({start: testBlobFileSize * 2/4, end: testBlobFileSize * 3/4});
  66. var actualPart4 = slicer.createReadStream({start: testBlobFileSize * 3/4, end: testBlobFileSize * 4/4});
  67. var expectedPart1 = slicer.createReadStream({start: testBlobFileSize * 0/4, end: testBlobFileSize * 1/4});
  68. var expectedPart2 = slicer.createReadStream({start: testBlobFileSize * 1/4, end: testBlobFileSize * 2/4});
  69. var expectedPart3 = slicer.createReadStream({start: testBlobFileSize * 2/4, end: testBlobFileSize * 3/4});
  70. var expectedPart4 = slicer.createReadStream({start: testBlobFileSize * 3/4, end: testBlobFileSize * 4/4});
  71. var pend = new Pend();
  72. pend.go(function(cb) {
  73. streamEqual(expectedPart1, actualPart1, function(err, equal) {
  74. assert.ok(equal);
  75. cb(err);
  76. });
  77. });
  78. pend.go(function(cb) {
  79. streamEqual(expectedPart2, actualPart2, function(err, equal) {
  80. assert.ok(equal);
  81. cb(err);
  82. });
  83. });
  84. pend.go(function(cb) {
  85. streamEqual(expectedPart3, actualPart3, function(err, equal) {
  86. assert.ok(equal);
  87. cb(err);
  88. });
  89. });
  90. pend.go(function(cb) {
  91. streamEqual(expectedPart4, actualPart4, function(err, equal) {
  92. assert.ok(equal);
  93. cb(err);
  94. });
  95. });
  96. pend.wait(function(err) {
  97. if (err) return done(err);
  98. fs.close(fd, done);
  99. });
  100. });
  101. });
  102. it("writes a 20MB file (autoClose on)", function(done) {
  103. fs.open(testOutBlobFile, 'w', function(err, fd) {
  104. if (err) return done(err);
  105. var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
  106. var actualStream = slicer.createWriteStream();
  107. var inStream = fs.createReadStream(testBlobFile);
  108. slicer.on('close', function() {
  109. var expected = fs.createReadStream(testBlobFile);
  110. var actual = fs.createReadStream(testOutBlobFile);
  111. streamEqual(expected, actual, function(err, equal) {
  112. if (err) return done(err);
  113. assert.ok(equal);
  114. done();
  115. });
  116. });
  117. inStream.pipe(actualStream);
  118. });
  119. });
  120. it("writes 4 chunks simultaneously", function(done) {
  121. fs.open(testOutBlobFile, 'w', function(err, fd) {
  122. if (err) return done(err);
  123. var slicer = fdSlicer.createFromFd(fd);
  124. var actualPart1 = slicer.createWriteStream({start: testBlobFileSize * 0/4});
  125. var actualPart2 = slicer.createWriteStream({start: testBlobFileSize * 1/4});
  126. var actualPart3 = slicer.createWriteStream({start: testBlobFileSize * 2/4});
  127. var actualPart4 = slicer.createWriteStream({start: testBlobFileSize * 3/4});
  128. var in1 = fs.createReadStream(testBlobFile, {start: testBlobFileSize * 0/4, end: testBlobFileSize * 1/4});
  129. var in2 = fs.createReadStream(testBlobFile, {start: testBlobFileSize * 1/4, end: testBlobFileSize * 2/4});
  130. var in3 = fs.createReadStream(testBlobFile, {start: testBlobFileSize * 2/4, end: testBlobFileSize * 3/4});
  131. var in4 = fs.createReadStream(testBlobFile, {start: testBlobFileSize * 3/4, end: testBlobFileSize * 4/4});
  132. var pend = new Pend();
  133. pend.go(function(cb) {
  134. actualPart1.on('finish', cb);
  135. });
  136. pend.go(function(cb) {
  137. actualPart2.on('finish', cb);
  138. });
  139. pend.go(function(cb) {
  140. actualPart3.on('finish', cb);
  141. });
  142. pend.go(function(cb) {
  143. actualPart4.on('finish', cb);
  144. });
  145. in1.pipe(actualPart1);
  146. in2.pipe(actualPart2);
  147. in3.pipe(actualPart3);
  148. in4.pipe(actualPart4);
  149. pend.wait(function() {
  150. fs.close(fd, function(err) {
  151. if (err) return done(err);
  152. var expected = fs.createReadStream(testBlobFile);
  153. var actual = fs.createReadStream(testOutBlobFile);
  154. streamEqual(expected, actual, function(err, equal) {
  155. if (err) return done(err);
  156. assert.ok(equal);
  157. done();
  158. });
  159. });
  160. });
  161. });
  162. });
  163. it("throws on invalid ref", function(done) {
  164. fs.open(testOutBlobFile, 'w', function(err, fd) {
  165. if (err) return done(err);
  166. var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
  167. assert.throws(function() {
  168. slicer.unref();
  169. }, /invalid unref/);
  170. fs.close(fd, done);
  171. });
  172. });
  173. it("write stream emits error when max size exceeded", function(done) {
  174. fs.open(testOutBlobFile, 'w', function(err, fd) {
  175. if (err) return done(err);
  176. var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
  177. var ws = slicer.createWriteStream({start: 0, end: 1000});
  178. ws.on('error', function(err) {
  179. assert.strictEqual(err.code, 'ETOOBIG');
  180. slicer.on('close', done);
  181. });
  182. ws.end(new Buffer(1001));
  183. });
  184. });
  185. it("write stream does not emit error when max size not exceeded", function(done) {
  186. fs.open(testOutBlobFile, 'w', function(err, fd) {
  187. if (err) return done(err);
  188. var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
  189. var ws = slicer.createWriteStream({end: 1000});
  190. slicer.on('close', done);
  191. ws.end(new Buffer(1000));
  192. });
  193. });
  194. it("write stream start and end work together", function(done) {
  195. fs.open(testOutBlobFile, 'w', function(err, fd) {
  196. if (err) return done(err);
  197. var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
  198. var ws = slicer.createWriteStream({start: 1, end: 1000});
  199. ws.on('error', function(err) {
  200. assert.strictEqual(err.code, 'ETOOBIG');
  201. slicer.on('close', done);
  202. });
  203. ws.end(new Buffer(1000));
  204. });
  205. });
  206. it("write stream emits progress events", function(done) {
  207. fs.open(testOutBlobFile, 'w', function(err, fd) {
  208. if (err) return done(err);
  209. var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
  210. var ws = slicer.createWriteStream();
  211. var progressEventCount = 0;
  212. var prevBytesWritten = 0;
  213. ws.on('progress', function() {
  214. progressEventCount += 1;
  215. assert.ok(ws.bytesWritten > prevBytesWritten);
  216. prevBytesWritten = ws.bytesWritten;
  217. });
  218. slicer.on('close', function() {
  219. assert.ok(progressEventCount > 5);
  220. done();
  221. });
  222. for (var i = 0; i < 10; i += 1) {
  223. ws.write(new Buffer(16 * 1024 * 2));
  224. }
  225. ws.end();
  226. });
  227. });
  228. it("write stream unrefs when destroyed", function(done) {
  229. fs.open(testOutBlobFile, 'w', function(err, fd) {
  230. if (err) return done(err);
  231. var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
  232. var ws = slicer.createWriteStream();
  233. slicer.on('close', done);
  234. ws.write(new Buffer(1000));
  235. ws.destroy();
  236. });
  237. });
  238. it("read stream unrefs when destroyed", function(done) {
  239. fs.open(testBlobFile, 'r', function(err, fd) {
  240. if (err) return done(err);
  241. var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
  242. var rs = slicer.createReadStream();
  243. rs.on('error', function(err) {
  244. assert.strictEqual(err.message, "stream destroyed");
  245. slicer.on('close', done);
  246. });
  247. rs.destroy();
  248. });
  249. });
  250. it("fdSlicer.read", function(done) {
  251. fs.open(testBlobFile, 'r', function(err, fd) {
  252. if (err) return done(err);
  253. var slicer = fdSlicer.createFromFd(fd);
  254. var outBuf = new Buffer(1024);
  255. slicer.read(outBuf, 0, 10, 0, function(err, bytesRead, buf) {
  256. assert.strictEqual(bytesRead, 10);
  257. fs.close(fd, done);
  258. });
  259. });
  260. });
  261. it("fdSlicer.write", function(done) {
  262. fs.open(testOutBlobFile, 'w', function(err, fd) {
  263. if (err) return done(err);
  264. var slicer = fdSlicer.createFromFd(fd);
  265. slicer.write(new Buffer("blah\n"), 0, 5, 0, function() {
  266. if (err) return done(err);
  267. fs.close(fd, done);
  268. });
  269. });
  270. });
  271. });
  272. describe("BufferSlicer", function() {
  273. it("invalid ref", function() {
  274. var slicer = fdSlicer.createFromBuffer(new Buffer(16));
  275. slicer.ref();
  276. slicer.unref();
  277. assert.throws(function() {
  278. slicer.unref();
  279. }, /invalid unref/);
  280. });
  281. it("read and write", function(done) {
  282. var buf = new Buffer("through the tangled thread the needle finds its way");
  283. var slicer = fdSlicer.createFromBuffer(buf);
  284. var outBuf = new Buffer(1024);
  285. slicer.read(outBuf, 10, 11, 8, function(err) {
  286. if (err) return done(err);
  287. assert.strictEqual(outBuf.toString('utf8', 10, 21), "the tangled");
  288. slicer.write(new Buffer("derp"), 0, 4, 7, function(err) {
  289. if (err) return done(err);
  290. assert.strictEqual(buf.toString('utf8', 7, 19), "derp tangled");
  291. done();
  292. });
  293. });
  294. });
  295. it("createReadStream", function(done) {
  296. var str = "I never conquered rarely came, 16 just held such better days";
  297. var buf = new Buffer(str);
  298. var slicer = fdSlicer.createFromBuffer(buf);
  299. var inStream = slicer.createReadStream();
  300. var sink = new StreamSink();
  301. inStream.pipe(sink);
  302. sink.on('finish', function() {
  303. assert.strictEqual(sink.toString(), str);
  304. inStream.destroy();
  305. done();
  306. });
  307. });
  308. it("createWriteStream exceed buffer size", function(done) {
  309. var slicer = fdSlicer.createFromBuffer(new Buffer(4));
  310. var outStream = slicer.createWriteStream();
  311. outStream.on('error', function(err) {
  312. assert.strictEqual(err.code, 'ETOOBIG');
  313. done();
  314. });
  315. outStream.write("hi!\n");
  316. outStream.write("it warked\n");
  317. outStream.end();
  318. });
  319. it("createWriteStream ok", function(done) {
  320. var buf = new Buffer(1024);
  321. var slicer = fdSlicer.createFromBuffer(buf);
  322. var outStream = slicer.createWriteStream();
  323. outStream.on('finish', function() {
  324. assert.strictEqual(buf.toString('utf8', 0, "hi!\nit warked\n".length), "hi!\nit warked\n");
  325. outStream.destroy();
  326. done();
  327. });
  328. outStream.write("hi!\n");
  329. outStream.write("it warked\n");
  330. outStream.end();
  331. });
  332. });