From e8f5b54d631df88fb8b8ce4e92d2f88a4395cb27 Mon Sep 17 00:00:00 2001 From: Valeriy S Date: Thu, 14 Mar 2019 12:00:18 +0300 Subject: [PATCH] fix(client): pass logs incrementally from test worker --- client/src/client/workers/test-evaluator.js | 44 ++++++++++++++----- .../src/templates/Challenges/redux/index.js | 10 ++--- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/client/src/client/workers/test-evaluator.js b/client/src/client/workers/test-evaluator.js index 228d2e93ef..8f9d0f4e84 100644 --- a/client/src/client/workers/test-evaluator.js +++ b/client/src/client/workers/test-evaluator.js @@ -2,14 +2,38 @@ import chai from 'chai'; import '@babel/polyfill'; import __toString from 'lodash/toString'; -const oldLog = self.console.log.bind(self.console); -self.console.log = function proxyConsole(...args) { - self.postMessage({ - type: 'LOG', - data: args.map(arg => JSON.stringify(arg)).join(' ') - }); - return oldLog(...args); -}; +const __utils = (() => { + const MAX_LOGS_SIZE = 64 * 1024; + + let logs = []; + function flushLogs() { + if (logs.length) { + self.postMessage({ + type: 'LOG', + data: logs.join('\n') + }); + logs = []; + } + } + + const oldLog = self.console.log.bind(self.console); + self.console.log = function proxyConsole(...args) { + logs.push(args.map(arg => JSON.stringify(arg)).join(' ')); + if (logs.join('\n').length > MAX_LOGS_SIZE) { + flushLogs(); + } + return oldLog(...args); + }; + + function postResult(data) { + flushLogs(); + self.postMessage(data); + } + + return { + postResult + }; +})(); self.onmessage = async e => { /* eslint-disable no-unused-vars */ @@ -38,11 +62,11 @@ self.onmessage = async e => { if (typeof testResult === 'function') { await testResult(fileName => __toString(e.data.sources[fileName])); } - self.postMessage({ + __utils.postResult({ pass: true }); } catch (err) { - self.postMessage({ + __utils.postResult({ err: { message: err.message, stack: err.stack diff --git a/client/src/templates/Challenges/redux/index.js b/client/src/templates/Challenges/redux/index.js index e48db417ac..59308775a0 100644 --- a/client/src/templates/Challenges/redux/index.js +++ b/client/src/templates/Challenges/redux/index.js @@ -215,6 +215,8 @@ export const challengeDataSelector = state => { return challengeData; }; +const MAX_LOGS_SIZE = 64 * 1024; + export const reducer = handleActions( { [types.fetchIdToNameMapComplete]: (state, { payload }) => ({ @@ -259,19 +261,17 @@ export const reducer = handleActions( }), [types.initLogs]: state => ({ ...state, - logsOut: [] + logsOut: '' }), [types.updateLogs]: (state, { payload }) => ({ ...state, - logsOut: [...state.logsOut, payload] + logsOut: (state.logsOut + '\n' + payload).slice(-MAX_LOGS_SIZE) }), [types.logsToConsole]: (state, { payload }) => ({ ...state, consoleOut: state.consoleOut + - (state.logsOut.length - ? '\n' + payload + '\n' + state.logsOut.join('\n') - : '') + (state.logsOut ? '\n' + payload + '\n' + state.logsOut : '') }), [types.updateChallengeMeta]: (state, { payload }) => ({ ...state,