diff options
author | Marcos Casagrande <marcoscvp90@gmail.com> | 2020-07-10 17:49:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-10 11:49:35 -0400 |
commit | 39dba12a061e464fb06bc6a763c84b36b5d1a915 (patch) | |
tree | b6eed98dafce1643225232bf87236958add8e808 /cli/tests/unit/buffer_test.ts | |
parent | 1bcc35b84a78fb052b8092b7ed57c2ce763f5d4b (diff) |
fix(cli/buffer): allow Buffer to store MAX_SIZE bytes (#6570)
Diffstat (limited to 'cli/tests/unit/buffer_test.ts')
-rw-r--r-- | cli/tests/unit/buffer_test.ts | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/cli/tests/unit/buffer_test.ts b/cli/tests/unit/buffer_test.ts index dc80c9c6d..440dd5495 100644 --- a/cli/tests/unit/buffer_test.ts +++ b/cli/tests/unit/buffer_test.ts @@ -11,11 +11,19 @@ import { unitTest, } from "./test_util.ts"; +const MAX_SIZE = 2 ** 32 - 2; // N controls how many iterations of certain checks are performed. const N = 100; let testBytes: Uint8Array | null; let testString: string | null; +let ignoreMaxSizeTests = false; +try { + new ArrayBuffer(MAX_SIZE); +} catch (e) { + ignoreMaxSizeTests = true; +} + function init(): void { if (testBytes == null) { testBytes = new Uint8Array(N); @@ -167,6 +175,100 @@ unitTest(async function bufferTooLargeByteWrites(): Promise<void> { ); }); +unitTest( + { ignore: ignoreMaxSizeTests }, + function bufferGrowWriteMaxBuffer(): void { + const bufSize = 16 * 1024; + const capacities = [MAX_SIZE, MAX_SIZE - 1]; + for (const capacity of capacities) { + let written = 0; + const buf = new Deno.Buffer(); + const writes = Math.floor(capacity / bufSize); + for (let i = 0; i < writes; i++) + written += buf.writeSync(repeat("x", bufSize)); + + if (written < capacity) { + written += buf.writeSync(repeat("x", capacity - written)); + } + + assertEquals(written, capacity); + } + } +); + +unitTest( + { ignore: ignoreMaxSizeTests }, + async function bufferGrowReadCloseMaxBufferPlus1(): Promise<void> { + const reader = new Deno.Buffer(new ArrayBuffer(MAX_SIZE + 1)); + const buf = new Deno.Buffer(); + + await assertThrowsAsync( + async () => { + await buf.readFrom(reader); + }, + Error, + "grown beyond the maximum size" + ); + } +); + +unitTest( + { ignore: ignoreMaxSizeTests }, + function bufferGrowReadSyncCloseMaxBufferPlus1(): void { + const reader = new Deno.Buffer(new ArrayBuffer(MAX_SIZE + 1)); + const buf = new Deno.Buffer(); + + assertThrows( + () => { + buf.readFromSync(reader); + }, + Error, + "grown beyond the maximum size" + ); + } +); + +unitTest( + { ignore: ignoreMaxSizeTests }, + function bufferGrowReadSyncCloseToMaxBuffer(): void { + const capacities = [MAX_SIZE, MAX_SIZE - 1]; + for (const capacity of capacities) { + const reader = new Deno.Buffer(new ArrayBuffer(capacity)); + const buf = new Deno.Buffer(); + buf.readFromSync(reader); + + assertEquals(buf.length, capacity); + } + } +); + +unitTest( + { ignore: ignoreMaxSizeTests }, + async function bufferGrowReadCloseToMaxBuffer(): Promise<void> { + const capacities = [MAX_SIZE, MAX_SIZE - 1]; + for (const capacity of capacities) { + const reader = new Deno.Buffer(new ArrayBuffer(capacity)); + const buf = new Deno.Buffer(); + await buf.readFrom(reader); + assertEquals(buf.length, capacity); + } + } +); + +unitTest( + { ignore: ignoreMaxSizeTests }, + async function bufferReadCloseToMaxBufferWithInitialGrow(): Promise<void> { + const capacities = [MAX_SIZE, MAX_SIZE - 1, MAX_SIZE - 512]; + for (const capacity of capacities) { + const reader = new Deno.Buffer(new ArrayBuffer(capacity)); + const buf = new Deno.Buffer(); + buf.grow(MAX_SIZE); + await buf.readFrom(reader); + assertEquals(buf.length, capacity); + } + } +); + unitTest(async function bufferLargeByteReads(): Promise<void> { init(); assert(testBytes); |