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

Commit

Permalink
Merge bc7695e into a596c0f
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-palmer committed Jun 14, 2019
2 parents a596c0f + bc7695e commit f211274
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/server/client_stream_recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class ClientStreamRecorder extends Writable {
}

async _flush_buffer() {
if(this._bufferPos === 0) return;

await fs.ensureDir(this._saveDir);

// Normalize the version size so it will be correctly parsed when streamed to a server.
Expand Down
21 changes: 20 additions & 1 deletion test/client_stream_recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('ClientStreamRecorder', () => {
it('should use options passed into constructor', () => {
const opts = {
sessionId: uuid.v4(),
saveDir: 'temp',
saveDir: this.tmpDir.name,
bufferSize: Math.floor(Math.random() * 100000)
};

Expand All @@ -41,6 +41,25 @@ describe('ClientStreamRecorder', () => {
assert.strictEqual(csr._bufferSize, opts.bufferSize);
});

it('should not write diagnostic file if client did not send any data', async () => {
const opts = {
saveDir: this.tmpDir.name,
bufferSize: 1024
};

const csr = new ClientStreamRecorder(opts);
const csrWrite = promisify(csr.write).bind(csr);
const dataPath = csr.dataPath;

await csrWrite(Buffer.alloc(0));
assert(!await fs.pathExists(dataPath));
csr.emit('unpipe'); // triggers a buffer flush
await csrWrite(Buffer.alloc(1025));
csr.emit('unpipe');
assert(await fs.pathExists(dataPath));
});


it('should not write to disk until the internal buffer is full', async () => {
const opts = {
saveDir: this.tmpDir.name,
Expand Down
22 changes: 11 additions & 11 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const CacheServer = require('../lib/server/server');
const tmp = require('tmp');
const ClientStreamRecorder = require('../lib/server/client_stream_recorder');
const CacheBase = require('../lib/cache/cache_base').CacheBase;
const { generateCommandData, encodeCommand, clientWrite, sleep, cmd } = require('./test_utils');
const { generateCommandData, encodeCommand, clientWrite, sleep, cmd, purgeConfig } = require('./test_utils');
const sinon = require('sinon');

const cache = new CacheBase();
Expand Down Expand Up @@ -223,16 +223,17 @@ describe("Server common", function() {
before(async () => {
this.tmpDir = tmp.dirSync({unsafeCleanup: true});

const serverOpts = {
port: 0,
clientRecorder: {
enabled: true,
saveDir: this.tmpDir.name,
bufferSize: 1024
purgeConfig();
process.env.NODE_CONFIG = JSON.stringify({
Diagnostics: {
clientRecorderOptions: {
saveDir: this.tmpDir.name,
bufferSize: 1024
}
}
};
});

this.csrServer = new CacheServer(cache, serverOpts);
this.csrServer = new CacheServer(cache, {clientRecorder: true});
return this.csrServer.start(err => assert(!err, `Cache Server reported error! ${err}`));
});

Expand All @@ -247,13 +248,12 @@ describe("Server common", function() {
this.csrServer.server.on('connection', socket => {
assert.ok(Array.isArray(socket._readableState.pipes));
assert.ok(socket._readableState.pipes.find(x => x instanceof ClientStreamRecorder));
done();
});

client = net.connect({port: this.csrServer.port}, () => {
client.write(helpers.encodeInt32(consts.PROTOCOL_VERSION));
client.end(cmd.quit);
});
}).on('data', () => {}).on('close', () => done());
});
});

Expand Down
3 changes: 2 additions & 1 deletion test/unity_cache_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ describe("Unity Cache Server bootstrap", () => {
}
},
Diagnostics: {
clientRecorder: true
clientRecorder: true,
saveDir: tmpPath
}
});

Expand Down

0 comments on commit f211274

Please sign in to comment.