|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const chunker = require('./../src/chunker/rabin') |
| 5 | +const chai = require('chai') |
| 6 | +chai.use(require('dirty-chai')) |
| 7 | +const expect = chai.expect |
| 8 | +const pull = require('pull-stream') |
| 9 | +const loadFixture = require('aegir/fixtures') |
| 10 | + |
| 11 | +const rawFile = loadFixture('test/fixtures/1MiB.txt') |
| 12 | + |
| 13 | +describe('chunker: rabin', function () { |
| 14 | + this.timeout(30000) |
| 15 | + |
| 16 | + it('chunks non flat buffers', (done) => { |
| 17 | + const b1 = Buffer.alloc(2 * 256) |
| 18 | + const b2 = Buffer.alloc(1 * 256) |
| 19 | + const b3 = Buffer.alloc(5 * 256) |
| 20 | + |
| 21 | + b1.fill('a') |
| 22 | + b2.fill('b') |
| 23 | + b3.fill('c') |
| 24 | + |
| 25 | + pull( |
| 26 | + pull.values([b1, b2, b3]), |
| 27 | + chunker({minChunkSize: 48, avgChunkSize: 96, maxChunkSize: 192}), |
| 28 | + pull.collect((err, chunks) => { |
| 29 | + expect(err).to.not.exist() |
| 30 | + chunks.forEach((chunk) => { |
| 31 | + expect(chunk).to.have.length.gte(48) |
| 32 | + expect(chunk).to.have.length.lte(192) |
| 33 | + }) |
| 34 | + done() |
| 35 | + }) |
| 36 | + ) |
| 37 | + }) |
| 38 | + |
| 39 | + it('256 KiB avg chunks of non scalar filesize', (done) => { |
| 40 | + const KiB256 = 262144 |
| 41 | + let file = Buffer.concat([rawFile, Buffer.from('hello')]) |
| 42 | + const opts = { |
| 43 | + minChunkSize: KiB256 / 3, |
| 44 | + avgChunkSize: KiB256, |
| 45 | + maxChunkSize: KiB256 + (KiB256 / 2) |
| 46 | + } |
| 47 | + pull( |
| 48 | + pull.values([file]), |
| 49 | + chunker(opts), |
| 50 | + pull.collect((err, chunks) => { |
| 51 | + expect(err).to.not.exist() |
| 52 | + |
| 53 | + chunks.forEach((chunk) => { |
| 54 | + expect(chunk).to.have.length.gte(opts.minChunkSize) |
| 55 | + expect(chunk).to.have.length.lte(opts.maxChunkSize) |
| 56 | + }) |
| 57 | + |
| 58 | + done() |
| 59 | + }) |
| 60 | + ) |
| 61 | + }) |
| 62 | +}) |
0 commit comments