-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7240ed
commit 17a4dad
Showing
5 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { chunkFromBuffer } from "../../src/data-chunk/buffer-helper"; | ||
import { DataPart } from "../../src/data-chunk/yield-chunk"; | ||
|
||
describe(chunkFromBuffer.name, () => { | ||
const INPUT_STRING = "Farmer jack realized that big yellow quilts were expensive"; | ||
|
||
describe("String chunking", () => { | ||
it("should come back with small sub buffers", async (done) => { | ||
const length = 10; | ||
const chunker = chunkFromBuffer(Buffer.from(INPUT_STRING), length); | ||
const result = await chunker.next(); | ||
const dataChunk = result.value as DataPart; | ||
expect(dataChunk.PartNumber).toEqual(1); | ||
expect(dataChunk.Body.toString()).toEqual(INPUT_STRING.substring(0, length)); | ||
|
||
done(); | ||
}); | ||
|
||
it("should come back with many chunks on subsequent calls", async (done) => { | ||
const length = 10; | ||
const chunker = chunkFromBuffer(Buffer.from(INPUT_STRING), length); | ||
|
||
let result = await chunker.next(); | ||
let dataChunk = result.value as DataPart; | ||
expect(dataChunk.PartNumber).toEqual(1); | ||
expect(dataChunk.Body.toString()).toEqual(INPUT_STRING.substring(0, length)); | ||
|
||
result = await chunker.next(); | ||
dataChunk = result.value as DataPart; | ||
expect(dataChunk.PartNumber).toEqual(2); | ||
expect(dataChunk.Body.toString()).toEqual(INPUT_STRING.substring(length, length * 2)); | ||
|
||
done(); | ||
}); | ||
|
||
it("should have the last sub buffer be the remainder length", async (done) => { | ||
const length = 30; | ||
const chunker = chunkFromBuffer(Buffer.from(INPUT_STRING), length); | ||
let result = await chunker.next(); | ||
result = await chunker.next(); | ||
const dataChunk = result.value as DataPart; | ||
expect(dataChunk.Body.toString()).toEqual(INPUT_STRING.substring(length)); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { Readable } from "stream"; | ||
import { chunkFromReadable } from "../../src/data-chunk/readable-helper"; | ||
import { DataPart } from "../../src/data-chunk/yield-chunk"; | ||
|
||
describe(chunkFromReadable.name, () => { | ||
const fs = require("fs"); | ||
const fileStream = fs.createReadStream("./test/data-chunk/sample.file"); | ||
const INPUT_STRING = "Farmer jack realized that big yellow quilts were expensive"; | ||
|
||
describe("Proper chunk creation", () => { | ||
let chunks: IteratorResult<DataPart>[] = []; | ||
const CHUNK_SIZE = 30; | ||
|
||
const setup = async () => { | ||
const chunk: IteratorResult<DataPart>[] = []; | ||
const readableStream = Readable.from(INPUT_STRING); | ||
const interation = chunkFromReadable(readableStream, CHUNK_SIZE); | ||
chunk.push(await interation.next()); | ||
chunk.push(await interation.next()); | ||
chunk.push(await interation.next()); | ||
return chunk; | ||
}; | ||
|
||
beforeAll(async (done) => { | ||
chunks = await setup(); | ||
done(); | ||
}); | ||
|
||
it("should properly give the first chunk", async (done) => { | ||
expect(chunks[0].done).toEqual(false); | ||
// expect(chunks[0].value.Body.toString()).toEqual("Farmer jack realized that big "); | ||
expect(chunks[0].value.Body.length).toEqual(30); | ||
expect(chunks[0].value.PartNumber).toEqual(1); | ||
done(); | ||
}); | ||
|
||
it("should properly give the second (smaller) chunk", async (done) => { | ||
expect(chunks[1].done).toEqual(false); | ||
expect(chunks[1].value.Body.toString()).toEqual("yellow quilts were expensive"); | ||
expect(chunks[1].value.Body.length).toEqual(28); | ||
expect(chunks[1].value.PartNumber).toEqual(2); | ||
done(); | ||
}); | ||
|
||
it("should properly end the interation", async (done) => { | ||
expect(chunks[2].done).toEqual(true); | ||
expect(chunks[2].value).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe("Proper single chunk", () => { | ||
let chunks: IteratorResult<DataPart>[] = []; | ||
const CHUNK_SIZE = 1000; | ||
|
||
const setup = async () => { | ||
const chunks: IteratorResult<DataPart>[] = []; | ||
const readableStream = Readable.from(INPUT_STRING); | ||
const interation = chunkFromReadable(readableStream, CHUNK_SIZE); | ||
chunks.push(await interation.next()); | ||
chunks.push(await interation.next()); | ||
return chunks; | ||
}; | ||
|
||
beforeAll(async (done) => { | ||
chunks = await setup(); | ||
done(); | ||
}); | ||
|
||
it("should properly give the first chunk as all the data", async (done) => { | ||
expect(CHUNK_SIZE).toBeGreaterThan(INPUT_STRING.length); | ||
expect(chunks[0].done).toEqual(false); | ||
expect(chunks[0].value.Body.toString()).toEqual(INPUT_STRING); | ||
expect(chunks[0].value.Body.length).toEqual(INPUT_STRING.length); | ||
expect(chunks[0].value.PartNumber).toEqual(1); | ||
done(); | ||
}); | ||
|
||
it("should properly end the interation", async (done) => { | ||
expect(chunks[1].done).toEqual(true); | ||
expect(chunks[1].value).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe("Proper no data chunk", () => { | ||
let chunks: IteratorResult<DataPart>[] = []; | ||
const CHUNK_SIZE = 10; | ||
|
||
const setup = async () => { | ||
const chunks: IteratorResult<DataPart>[] = []; | ||
const readableStream = Readable.from([]); | ||
const interation = chunkFromReadable(readableStream, CHUNK_SIZE); | ||
chunks.push(await interation.next()); | ||
chunks.push(await interation.next()); | ||
return chunks; | ||
}; | ||
|
||
beforeAll(async (done) => { | ||
chunks = await setup(); | ||
done(); | ||
}); | ||
|
||
it("should return an empty object", async (done) => { | ||
expect(chunks[0].done).toEqual(false); | ||
expect(chunks[0].value.Body.toString()).toEqual(""); | ||
done(); | ||
}); | ||
|
||
it("should properly end the interation", async (done) => { | ||
expect(chunks[1].done).toEqual(true); | ||
expect(chunks[1].value).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe("Properly chunks files", () => { | ||
let chunks: IteratorResult<DataPart>[] = []; | ||
const CHUNK_SIZE = 30; | ||
|
||
const setup = async () => { | ||
const chunk: IteratorResult<DataPart>[] = []; | ||
const interation = chunkFromReadable(fileStream, CHUNK_SIZE); | ||
chunk.push(await interation.next()); | ||
chunk.push(await interation.next()); | ||
chunk.push(await interation.next()); | ||
return chunk; | ||
}; | ||
|
||
beforeAll(async (done) => { | ||
chunks = await setup(); | ||
done(); | ||
}); | ||
|
||
it("should properly give the first chunk", async (done) => { | ||
expect(chunks[0].done).toEqual(false); | ||
// expect(chunks[0].value.Body.toString()).toEqual("Farmer jack realized that big "); | ||
expect(chunks[0].value.Body.length).toEqual(30); | ||
expect(chunks[0].value.PartNumber).toEqual(1); | ||
done(); | ||
}); | ||
|
||
it("should properly give the second (smaller) chunk", async (done) => { | ||
expect(chunks[1].done).toEqual(false); | ||
expect(chunks[1].value.Body.toString()).toEqual("yellow quilts were expensive"); | ||
expect(chunks[1].value.Body.length).toEqual(28); | ||
expect(chunks[1].value.PartNumber).toEqual(2); | ||
done(); | ||
}); | ||
|
||
it("should properly end the interation", async (done) => { | ||
expect(chunks[2].done).toEqual(true); | ||
expect(chunks[2].value).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
lib/storage/test/data-chunk/readable-stream-helper.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ReadableStream } from "web-streams-polyfill"; | ||
import { chunkFromReadableStream } from "../../src/data-chunk/readable-stream-helper"; | ||
import { DataPart } from "../../src/data-chunk/yield-chunk"; | ||
|
||
describe(chunkFromReadableStream.name, () => { | ||
const INPUT_STRING = "Farmer jack realized that big yellow quilts were expensive"; | ||
|
||
describe("Proper chunk creation", () => { | ||
let chunks: IteratorResult<DataPart>[] = []; | ||
const CHUNK_SIZE = 30; | ||
|
||
const setup = async () => { | ||
const customStringStream = new ReadableStream({ | ||
start(controller) { | ||
let string = INPUT_STRING; | ||
controller.enqueue(string); | ||
}, | ||
pull(controller) { | ||
controller.close(); | ||
}, | ||
cancel() {}, | ||
}); | ||
|
||
let chunk: IteratorResult<DataPart>[] = []; | ||
let interation = chunkFromReadableStream(customStringStream, CHUNK_SIZE); | ||
chunk.push(await interation.next()); | ||
chunk.push(await interation.next()); | ||
chunk.push(await interation.next()); | ||
return chunk; | ||
}; | ||
|
||
beforeAll(async (done) => { | ||
chunks = await setup(); | ||
done(); | ||
}); | ||
|
||
it("should properly give the first chunk", async (done) => { | ||
expect(chunks[0].done).toEqual(false); | ||
// expect(chunks[0].value.Body.toString()).toEqual("Farmer jack realized that big "); | ||
expect(chunks[0].value.Body.length).toEqual(30); | ||
expect(chunks[0].value.PartNumber).toEqual(1); | ||
done(); | ||
}); | ||
|
||
it("should properly give the second (smaller) chunk", async (done) => { | ||
expect(chunks[1].done).toEqual(false); | ||
expect(chunks[1].value.Body.toString()).toEqual("yellow quilts were expensive"); | ||
expect(chunks[1].value.Body.length).toEqual(28); | ||
expect(chunks[1].value.PartNumber).toEqual(2); | ||
done(); | ||
}); | ||
|
||
it("should properly end the interation", async (done) => { | ||
expect(chunks[2].done).toEqual(true); | ||
expect(chunks[2].value).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Buffer } from "buffer"; | ||
import { DataPart, yieldChunk } from "../../src/data-chunk/yield-chunk"; | ||
import { Readable } from "stream"; | ||
import { ReadableStream } from "web-streams-polyfill"; | ||
|
||
describe(yieldChunk.name, () => { | ||
const fs = require("fs"); | ||
const fileStream = fs.createReadStream("./test/data-chunk/sample.file"); | ||
const INPUT_STRING = "Farmer jack realized that big yellow quilts were expensive"; | ||
|
||
const standardChunkTest = async (input: any) => { | ||
const chunker = yieldChunk(input, 30); | ||
let chunks: IteratorResult<DataPart>[] = []; | ||
chunks.push(await chunker.next()); | ||
chunks.push(await chunker.next()); | ||
chunks.push(await chunker.next()); | ||
|
||
// test full, first chunk | ||
expect(chunks[0].done).toEqual(false); | ||
expect(chunks[0].value.Body.toString()).toEqual("Farmer jack realized that big "); | ||
expect(chunks[0].value.Body.length).toEqual(30); | ||
expect(chunks[0].value.PartNumber).toEqual(1); | ||
|
||
// test small, final chunk | ||
expect(chunks[1].done).toEqual(false); | ||
expect(chunks[1].value.Body.toString()).toEqual("yellow quilts were expensive"); | ||
expect(chunks[1].value.Body.length).toEqual(28); | ||
expect(chunks[1].value.PartNumber).toEqual(2); | ||
|
||
// test chunk ends | ||
expect(chunks[2].done).toEqual(true); | ||
expect(chunks[2].value).toEqual(undefined); | ||
}; | ||
|
||
it(`Readable: ${yieldChunk.name}`, async (done) => { | ||
standardChunkTest(Readable.from(INPUT_STRING)); | ||
done(); | ||
}); | ||
|
||
it(`File Stream: ${yieldChunk.name}`, async (done) => { | ||
standardChunkTest(fileStream); | ||
done(); | ||
}); | ||
|
||
it(`ReadableStream: ${yieldChunk.name}`, async (done) => { | ||
const customStringStream = new ReadableStream({ | ||
start(controller) { | ||
let string = INPUT_STRING; | ||
controller.enqueue(string); | ||
}, | ||
pull(controller) { | ||
controller.close(); | ||
}, | ||
cancel() {}, | ||
}); | ||
|
||
standardChunkTest(customStringStream); | ||
done(); | ||
}); | ||
|
||
it(`Buffer: ${yieldChunk.name}`, async (done) => { | ||
standardChunkTest(Buffer.from(INPUT_STRING)); | ||
done(); | ||
}); | ||
|
||
it(`String: ${yieldChunk.name}`, async (done) => { | ||
standardChunkTest(INPUT_STRING); | ||
done(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as Storage from "../src/index"; | ||
|
||
describe("Storage Packages", () => { | ||
it("has Upload", () => { | ||
expect(Storage.Upload).toBeDefined(); | ||
}); | ||
}); |