This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
chunker-rabin.spec.js
135 lines (112 loc) · 3.33 KB
/
chunker-rabin.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* eslint-env mocha */
'use strict'
const chunker = require('../src/chunker/rabin')
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const loadFixture = require('aegir/fixtures')
const isNode = require('detect-node')
const all = require('it-all')
const rawFile = loadFixture((isNode ? __dirname : 'test') + '/fixtures/1MiB.txt')
describe('chunker: rabin', function () {
this.timeout(30000)
const defaultOptions = {
avgChunkSize: 262144,
window: 64,
polynomial: 17437180132763653
}
it('chunks non flat buffers', async () => {
const b1 = Buffer.alloc(2 * 256)
const b2 = Buffer.alloc(1 * 256)
const b3 = Buffer.alloc(5 * 256)
b1.fill('a')
b2.fill('b')
b3.fill('c')
const chunks = await all(chunker([b1, b2, b3], {
...defaultOptions,
minChunkSize: 48,
avgChunkSize: 96,
maxChunkSize: 192
}))
const size = chunks.reduce((acc, curr) => acc + curr.length, 0)
expect(size).to.equal(b1.length + b2.length + b3.length)
chunks.forEach((chunk, index) => {
if (index === chunks.length - 1) {
expect(chunk.length).to.equal(128)
} else {
expect(chunk.length).to.equal(192)
}
})
})
it('uses default min and max chunk size when only avgChunkSize is specified', async () => {
const b1 = Buffer.alloc(10 * 256)
b1.fill('a')
const chunks = await all(chunker([b1], {
...defaultOptions,
maxChunkSize: 262144,
minChunkSize: 18,
avgChunkSize: 256
}))
chunks.forEach((chunk) => {
expect(chunk).to.have.length.gte(256 / 3)
expect(chunk).to.have.length.lte(256 * (256 / 2))
})
})
it('256 KiB avg chunks of non scalar filesize', async () => {
const KiB256 = 262144
const file = Buffer.concat([rawFile, Buffer.from('hello')])
const opts = {
...defaultOptions,
minChunkSize: KiB256 / 3,
avgChunkSize: KiB256,
maxChunkSize: KiB256 + (KiB256 / 2)
}
const chunks = await all(chunker([file], opts))
chunks.forEach((chunk) => {
expect(chunk).to.have.length.gte(opts.minChunkSize)
expect(chunk).to.have.length.lte(opts.maxChunkSize)
})
})
it('throws when min chunk size is too small', async () => {
const opts = {
...defaultOptions,
minChunkSize: 1,
maxChunkSize: 100
}
try {
await all(chunker([], opts))
throw new Error('Should have thrown')
} catch (err) {
expect(err.code).to.equal('ERR_INVALID_MIN_CHUNK_SIZE')
}
})
it('throws when avg chunk size is not specified', async () => {
const opts = {
...defaultOptions,
avgChunkSize: undefined
}
try {
await all(chunker([], opts))
throw new Error('Should have thrown')
} catch (err) {
expect(err.code).to.equal('ERR_INVALID_AVG_CHUNK_SIZE')
}
})
it('uses the min chunk size when max and avg are too small', async () => {
const file = Buffer.concat([rawFile, Buffer.from('hello')])
const opts = {
...defaultOptions,
minChunkSize: 100,
maxChunkSize: 5,
avgChunkSize: 5
}
const chunks = await all(chunker([file], opts))
chunks.forEach((chunk, index) => {
if (index === chunks.length - 1) {
expect(chunk.length).to.equal(81)
} else {
expect(chunk.length).to.equal(100)
}
})
})
})