Commit 3921fdac authored by Linshizhi's avatar Linshizhi

Blk based channel pass unittests

parent 04c6111e
...@@ -232,7 +232,6 @@ export class Channel { ...@@ -232,7 +232,6 @@ export class Channel {
} }
#remainBlocks() { #remainBlocks() {
console.log(this.#readPointerCache, this.#writePointerCache);
if (this.#readPointerCache == this.#writePointerCache) { if (this.#readPointerCache == this.#writePointerCache) {
// Check RBLKS and WBLKS // Check RBLKS and WBLKS
let rBlks = this.#getRBlks(), let rBlks = this.#getRBlks(),
...@@ -342,6 +341,7 @@ export class Channel { ...@@ -342,6 +341,7 @@ export class Channel {
#readDataBlks(size) { #readDataBlks(size) {
let buffer = undefined; let buffer = undefined;
let endPos = this.#endPos - 1; let endPos = this.#endPos - 1;
let rEnd = this.#readPointerCache;
this.#writePointerCache = this.#getWritePointer(); this.#writePointerCache = this.#getWritePointer();
...@@ -353,6 +353,7 @@ export class Channel { ...@@ -353,6 +353,7 @@ export class Channel {
return new Uint8Array(0); return new Uint8Array(0);
} }
let rBlks = this.#getRBlks();
buffer = new Uint8Array(blksRequired * this.#blockSize); buffer = new Uint8Array(blksRequired * this.#blockSize);
if (this.#readPointerCache >= this.#writePointerCache) { if (this.#readPointerCache >= this.#writePointerCache) {
...@@ -360,18 +361,26 @@ export class Channel { ...@@ -360,18 +361,26 @@ export class Channel {
let blks = Math.floor(spaceBetweenEnd / this.#blockSize); let blks = Math.floor(spaceBetweenEnd / this.#blockSize);
if (blks >= blksRequired) { if (blks >= blksRequired) {
buffer = new Uint8Array(blksRequired * this.#blockSize); rEnd = this.#readPointerCache + (blksRequired * this.#blockSize);
buffer.set(this.#buffer.subarray( buffer.set(this.#buffer.subarray(this.#readPointerCache, rEnd));
this.#readPointerCache, this.#readPointerCache+(blksRequired*this.#blockSize)))
return buffer;
} else { } else {
/* Two step to read datas */ // Read two times
rEnd = this.#metaSize + (blksRequired - blks) * this.#blockSize;
buffer.set(this.#buffer.subarray(
this.#readPointerCache, blks * this.#blockSize));
buffer.set(this.#buffer.subarray(this.#metaSize, rEnd),
blks * this.#blockSize);
} }
} else if (this.#readPointerCache < this.#writePointerCache) { } else if (this.#readPointerCache < this.#writePointerCache) {
rEnd = this.#readPointerCache + (blksRequired * this.#blockSize);
buffer.set(this.#buffer.subarray(
this.#readPointerCache, rEnd));
} }
this.#setRBlks((rBlks+blksRequired)%Math.pow(2, 8));
// Similar to push() must not change readPointer field in SharedMemory
// until all reads done.
this.#readPointerUpdate(rEnd);
return buffer; return buffer;
} }
...@@ -405,9 +414,6 @@ export class Channel { ...@@ -405,9 +414,6 @@ export class Channel {
let plan_1 = schedule['first']; let plan_1 = schedule['first'];
let plan_2 = schedule['second']; let plan_2 = schedule['second'];
console.log(plan_1);
console.log(plan_2);
let endPos = undefined; let endPos = undefined;
let buffers = []; let buffers = [];
...@@ -592,16 +598,14 @@ class CopyStrategyInBlock extends CopyStrategy { ...@@ -592,16 +598,14 @@ class CopyStrategyInBlock extends CopyStrategy {
retSchedule.first.pos = wPos; retSchedule.first.pos = wPos;
if (blksAbleToWrite >= blocksRequired) { if (blksAbleToWrite >= blocksRequired) {
retSchedule.first.size = blocksRequired * this.#blockSize; retSchedule.first.size = blocksRequired * this.#blockSize;
console.log(retSchedule)
return retSchedule;
} else { } else {
retSchedule.first.size = blksAbleToWrite * this.#blockSize; retSchedule.first.size = blksAbleToWrite * this.#blockSize;
blocksRequired -= blksAbleToWrite; blocksRequired -= blksAbleToWrite;
/* Second one is required */
retSchedule.second.pos = this.#shift;
retSchedule.second.size = blocksRequired * this.#blockSize;
} }
/* Second one is required */
retSchedule.second.pos = this.#shift;
retSchedule.second.size = blocksRequired * this.#blockSize;
} else if (wPos == rPos) { } else if (wPos == rPos) {
// Under the assumption of schedule() 'wPos == rPos' means // Under the assumption of schedule() 'wPos == rPos' means
// channel is empty // channel is empty
...@@ -613,9 +617,7 @@ class CopyStrategyInBlock extends CopyStrategy { ...@@ -613,9 +617,7 @@ class CopyStrategyInBlock extends CopyStrategy {
retSchedule.first.size = blocksRequired * this.#blockSize; retSchedule.first.size = blocksRequired * this.#blockSize;
} }
args.wBlks += blocksRequired; args.wBlks = (args.wBlks + blocksRequired) % Math.pow(2, 8);
console.log(retSchedule);
return retSchedule; return retSchedule;
} }
} }
import { sleep } from '../src/utils.js'; import { sleep } from '../src/utils.js';
import { H264EncWWGroup } from '../src/encGroup.js'; import { Observable } from 'rxjs';
import { Obervable, Observable } from 'rxjs';
import { Channel } from '../src/channel.js'; import { Channel } from '../src/channel.js';
const areEqual = (first, second) => const areEqual = (first, second) =>
...@@ -314,21 +313,141 @@ describe("Channel Spec", () => { ...@@ -314,21 +313,141 @@ describe("Channel Spec", () => {
expect(areEqual(rBuffer, sBuffer)).toBe(true); expect(areEqual(rBuffer, sBuffer)).toBe(true);
}, 10000); }, 10000);
});
describe("BLK-BASED Channel Spec", () => {
let blkSize = 10;
let chn = undefined;
let blkData = new Uint8Array([...Array(blkSize).keys()]);
it("BLK-BASED Channel Case 1", async () => { beforeEach(() => {
const blkSize = 10; chn = new Channel(blkSize * 2);
let chn = new Channel(blkSize * 2);
chn.enableBlockMode(blkSize); chn.enableBlockMode(blkSize);
});
it("Write Until Full", async () => {
for (let i = 0; i < 2; ++i)
chn.push(blkData);
expect(chn.dataSize()).toBe(blkSize * 2);
});
let blk = new Uint8Array([...Array(blkSize).keys()]); it("Write and Read", async () => {
chn.push(blkData);
chn.push(blkData);
expect(chn.dataSize()).toBe(blkSize * 2);
for (let i = 0; i < 10; ++i) let data = chn.readData(blkSize);
chn.push(blk); expect(areEqual(blkData, data)).toBe(true);
expect(chn.dataSize()).toBe(blkSize);
chn.push(blkData);
expect(chn.dataSize()).toBe(blkSize * 2); expect(chn.dataSize()).toBe(blkSize * 2);
data = chn.readData(blkSize);
expect(chn.dataSize()).toBe(blkSize);
expect(areEqual(data, blkData)).toBe(true);
});
it("Read Empty", async () => {
let data = chn.readData(blkSize);
expect(data.byteLength).toBe(0);
});
it("Write size smaller than bloksize", () => {
let wData = new Uint8Array([0,1,2,3,4]);
chn.push(wData);
expect(chn.dataSize()).toBe(0);
});
it("Read size smaller than blocksize", () => {
chn.push(blkData);
expect(chn.dataSize()).toBe(blkSize);
let data = chn.readData(blkSize - 1);
expect(data.byteLength).toBe(0);
expect(chn.dataSize()).toBe(blkSize);
}); });
it("BLK-BASED Channel Case 2", async () => { it("Read 0 size", () => {
chn.push(blkData);
let data = chn.readData(0);
expect(data.byteLength).toBe(0);
expect(chn.dataSize()).toBe(blkSize);
});
it("Write 0 size", () => {
let emptyData = new Uint8Array(0);
chn.push(emptyData);
expect(chn.dataSize()).toBe(0);
chn.push(blkData);
expect(chn.dataSize()).toBe(blkSize);
chn.push(emptyData);
expect(chn.dataSize()).toBe(blkSize);
})
it("Two step transfer", async () => {
let buffers = chn.writeStep1(blkSize * 2);
expect(buffers.length).toBe(1);
expect(buffers[0].byteLength).toBe(blkSize * 2);
buffers[0].set(blkData, 0);
buffers[0].set(blkData, blkData.byteLength);
chn.writeStep2();
expect(chn.dataSize()).toBe(blkSize*2);
for (let i = 0; i < 2; ++i) {
let data = chn.readData(blkSize);
expect(areEqual(data, blkData)).toBe(true);
}
expect(chn.dataSize()).toBe(0);
});
it("Two step write last block", () => {
let buffers = chn.writeStep1(blkSize * 1);
expect(buffers.length).toBe(1);
expect(buffers[0].byteLength).toBe(blkSize);
buffers[0].set(blkData, 0);
chn.writeStep2();
expect(chn.dataSize()).toBe(blkSize);
let data = chn.readData(blkSize);
expect(areEqual(data, blkData)).toBe(true);
expect(chn.dataSize()).toBe(0);
// Then write last blocks
for (let i = 0; i < 2; ++i) {
buffers = chn.writeStep1(blkSize * 1);
expect(buffers.length).toBe(1);
expect(buffers[0].byteLength).toBe(blkSize);
buffers[0].set(blkData, 0);
chn.writeStep2();
expect(chn.dataSize()).toBe(blkSize);
data = chn.readData(blkSize);
expect(areEqual(data, blkData)).toBe(true);
expect(chn.dataSize()).toBe(0);
}
});
it("Data bigger than channle", () => {
let bigData = new Uint8Array(blkSize * 3);
chn.push(bigData);
expect(chn.dataSize()).toBe(0);
// After that chn should work normally
chn.push(blkData);
expect(chn.dataSize()).toBe(10);
chn.push(blkData);
expect(chn.dataSize()).toBe(20);
let data = chn.readData(blkSize);
expect(chn.dataSize()).toBe(10);
expect(areEqual(data, blkData)).toBe(true);
data = chn.readData(blkSize);
expect(chn.dataSize()).toBe(0);
expect(areEqual(data, blkData)).toBe(true);
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment