Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into release/v6.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-palmer committed May 15, 2019
2 parents d0a24bb + d22dff7 commit 365a990
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
18 changes: 11 additions & 7 deletions lib/server/command_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class CommandProcessor extends Duplex {
return this._clientAddress;
}

get _sendFileQueueLength() {
const q = this[kSendFileQueue];
return q ? q.length - this._sendFileQueueIndex : 0;
}

_registerEventListeners() {
this.once('finish', this._printReadStats);
this.on('pipe', src => {
Expand All @@ -92,7 +97,7 @@ class CommandProcessor extends Duplex {

this.on('unpipe', () => {
this[kSource] = null;
this[kSendFileQueue] = [];
this[kSendFileQueue] = null;
this._writeHandler = this._writeHandlers.none;

if(this[kReadStream]) {
Expand Down Expand Up @@ -166,15 +171,14 @@ class CommandProcessor extends Duplex {
}

// No more files to send
const qLen = this[kSendFileQueue].length;
if(qLen === 0 || this._sendFileQueueIndex >= qLen) {
if(this._sendFileQueueLength === 0) {
return;
}

// De-queue the next file
const file = this[kSendFileQueue][this._sendFileQueueIndex];
delete this[kSendFileQueue][this._sendFileQueueIndex];
this._sendFileQueueIndex++;
const i = this._sendFileQueueIndex++;
const file = this[kSendFileQueue][i];
delete this[kSendFileQueue][i];

// Respond with file-not-found header and early out, wait for next _read
if(!file.exists) {
Expand Down Expand Up @@ -346,7 +350,7 @@ class CommandProcessor extends Duplex {
}
finally {
this[kSendFileQueue].push(item);
if(this[kSendFileQueue].length === 1) {
if(this._sendFileQueueLength === 1) {
this._read();
}
}
Expand Down
38 changes: 37 additions & 1 deletion test/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ describe("Protocol", () => {
// of a stuck async loop
});

it('should retrieve stored versions in the order they were are requested', function(done) {
it('should retrieve stored versions in the order they were are requested (queued)', function(done) {
const resp = new CacheServerResponseTransform();
client.pipe(resp);

Expand All @@ -303,9 +303,45 @@ describe("Protocol", () => {
encodeCommand(cmd.getInfo, self.data.guid, self.data.hash) +
encodeCommand(cmd.getAsset, self.data.guid, self.data.hash), 'ascii');


// execute all commands in the same frame to simulate filling the send queue in CommandProcessor
clientWrite(client, buf, LARGE_PACKET_SIZE).catch(err => done(err));
});

it('should retrieve stored versions in the order they were are requested (async series)', function(done) {
const resp = new CacheServerResponseTransform();
client.pipe(resp);

const cmds = ['+a', '+i', '-r', '+i', '+a'];

resp.on('data', () => {});
resp.on('dataEnd', () => {
if(cmds.length === 0) {
done();
}
});

resp.on('header', header => {
const nextCmd = cmds.shift();
assert.strictEqual(header.cmd, nextCmd);
});

const cmdData = [
encodeCommand(cmd.getAsset, self.data.guid, self.data.hash),
encodeCommand(cmd.getInfo, self.data.guid, self.data.hash),
encodeCommand(cmd.getResource, self.data.guid, Buffer.alloc(consts.HASH_SIZE, 'ascii')),
encodeCommand(cmd.getInfo, self.data.guid, self.data.hash),
encodeCommand(cmd.getAsset, self.data.guid, self.data.hash)
];

// execute each command in series, asynchronously, to better simulate a real world server connection
let next = Promise.resolve();
cmdData.forEach(b => {
next = next.then(() => clientWrite(client, b, LARGE_PACKET_SIZE))
.catch(err => done(err));
});
});

it('should respond with not found (-) for a file that exists but throws an error when accessed', function (done) {
const resp = new CacheServerResponseTransform();
client.pipe(resp);
Expand Down

0 comments on commit 365a990

Please sign in to comment.