Best JavaScript code snippet using fast-check-monorepo
appverse-performance.js
Source:appverse-performance.js
...83 */84 function init(message) {85 scope.workerid = workerid;86 scope.template = template || PERFORMANCE_CONFIG.webworker_Message_template;87 initWorker(scope.workerid, message);88 }89 /**90 * @function91 * @description92 * Gets the message from the worker.93 */94 function initWorker(workerid, message) {95 WebWorkerFactory.runTask(workerid, message);96 var messageFromWorker = WebWorkerFactory._resultMessage;97 if (messageFromWorker) {98 scope.messageFromWorker = messageFromWorker;99 }100 }101 /**102 * @function103 * @description104 * Gets the template and compile the desired layout.105 * Based on $compile, it compiles a piece of HTML string or DOM into the retrieved106 * template and produces a template function, which can then be used to link scope and107 * the template together.108 */109 function compileTemplate($http, $templateCache, $compile) {110 $http.get(scope.template, {111 //This allows you can get the template again by consuming the112 //$templateCache service directly.113 cache: $templateCache114 })115 .success(function (html) {116 element.html(html);117 $compile(element.contents())(scope);118 });119 }120 }121 };122 }]);123})();124(function() {125 'use strict';126 WebWorkerPoolFactory.$inject = ["$log", "$q", "PERFORMANCE_CONFIG"];127 angular.module('appverse.performance')128 .factory('WebWorkerPoolFactory', WebWorkerPoolFactory);129 /**130 * @ngdoc service131 * @name WebWorkerFactory132 * @module appverse.performance133 *134 * @description135 * This factory starts a pooled multithreaded execution of a webworker:136 * <pre></code> _______137 * | |-> thread 1138 * USER -> message -> task -> | pool |-> thread 2139 * |_______|-> thread N140 * </code></pre>141 *142 * @requires https://docs.angularjs.org/api/ngMock/service/$q $q143 * @requires https://docs.angularjs.org/api/ngMock/service/$log $log144 * @requires PERFORMANCE_CONFIG145 */146 function WebWorkerPoolFactory ($log, $q, PERFORMANCE_CONFIG) {147 var factory = {148 _poolSize: PERFORMANCE_CONFIG.webworker_pooled_threads,149 _authorizedWorkersOnly: PERFORMANCE_CONFIG.webworker_authorized_workers_only,150 _workersDir: PERFORMANCE_CONFIG.webworker_directory,151 _workersList: PERFORMANCE_CONFIG.webworker_authorized_workers,152 _resultMessage: ''153 };154 $log.debug("Initializated webworkers factory preconfigured values." );155 $log.debug("Default pool size: " + factory._poolSize);156 $log.debug("Are only authorized preconfigured workers? " + factory._authorizedWorkersOnly);157 $log.debug("The folder for webworkers in the app: " + factory._workersDir);158 $log.debug("Number of members in the workers list: " + factory._workersList.length);159 /**160 * @ngdoc method161 * @name WebWorkerFactory#runParallelTasksGroup162 *163 * @param {number} workerData WorkerData object with information of the task to be executed164 * @param {object} workerTasks Array with a group of WorkerTask objects for the same WorkerData165 *166 * @description167 * Runs a group of parallelized tasks168 * Run a set of workers according to the pre-defined data in configuration169 * (id, type, size in pool and worker file).170 * Pe-definition in configuration is mandatory.171 * The group of tasks are up to the caller.172 */173 factory.runParallelTasksGroup = function (workerData, workerTasks) {174 this.workerData = workerData;175 $log.debug("Started parallelized execution for worker: ");176 $log.debug(workerData.toString());177 //Initializes the pool with the indicated size for that worker group178 var pool = new factory.WorkerPool(this.workerData.poolSize);179 pool.init();180 //Create a worker task for181 if(workerTasks && workerTasks.length > 0){182 // iterate through all the parts of the image183 for (var x = 0; x < workerTasks.length; x++) {184 var workerTask = workerTasks[x];185 pool.addWorkerTask(workerTask);186 }187 }188 return factory._resultMessage;189 };190 /**191 * @ngdoc method192 * @name WebWorkerFactory#passMessage193 *194 * @param {number} id of the called worker195 * @param {object} function as callback196 * @param {string} message to be passed to the worker197 * @description198 * Execute a task in a worker.199 * The group of task is the same as the number indicated in the pool size for that pre-configured worker.200 */201 factory.runTask = function (workerId, message, callback) {202 var pool = new factory.WorkerPool(factory._poolSize);203 pool.init();204 /*205 If only workers in the configuration file are allowed.206 No fallback needed.207 */208 var workerData;209 var workerTask;210 if(factory._authorizedWorkersOnly){211 if(workerId){212 //Get data from configuration for the worker from the provided ID213 workerData = factory.getWorkerFromId(workerId);214 }else{215 //NO VALID WORKER ID ERROR216 $log.error("NO VALID WORKER ID ERROR");217 }218 }else{219 //If any provided worker is allowed the workerId arg is the complete path to the worker file220 //The ID is not important here221 if(workerId){222 workerData = new WorkerData(1001, 'dedicated', workerId);223 }else{224 //NO VALID WORKER ID ERROR225 $log.error("NO VALID WORKER ID ERROR");226 }227 }228 if(workerData) {229 pool = new factory.WorkerPool(workerData.poolSize);230 /*231 Create the worker task for the pool (only one task, passed N times):232 workerName: File of the worker233 callback: Register the supplied function as callback234 message: The last argument will be used to send a message to the worker235 */236 workerTask = new factory.WorkerTask(workerData, callback, message);237 // Pass the worker task object to the execution pool.238 // The default behavior is create one task for each thread in the pool.239 for(var i = 0; i < factory._poolSize; i++){240 pool.addWorkerTask(workerTask);241 }242 }else{243 //NO WORKER DATA ERROR244 $log.error("NO WORKER DATA ERROR");245 }246 //return _resultMessage;247 };248 factory.WorkerPool = function(poolSize) {249 var _this = this;250 if(!poolSize) {251 this.size = factory._poolSize;252 }else{253 this.size = poolSize;254 }255 //Initialize some vars with default values256 this.taskQueue = [];257 this.workerQueue = [];258 //Start the thread pool. To be used by the caller.259 this.init = function() {260 //Create the 'size' number of worker threads261 for (var i = 0 ; i < _this.size ; i++) {262 _this.workerQueue.push(new WorkerThread(_this));263 }264 };265 this.addWorkerTask = function(workerTask) {266 if (_this.workerQueue.length > 0) {267 // get the worker from the front of the queue268 var workerThread = _this.workerQueue.shift();269 workerThread.run(workerTask);270 } else {271 // If there are not free workers the task is put in queue272 _this.taskQueue.push(workerTask);273 }274 };275 //Execute the queued task. If empty, put the task to the queue.276 this.freeWorkerThread = function(workerThread) {277 if (_this.taskQueue.length > 0) {278 // It is not put back in the queue, but it is executed on the next task279 var workerTask = _this.taskQueue.shift();280 workerThread.run(workerTask);281 } else {282 _this.taskQueue.push(workerThread);283 }284 };285 };286 //Runner work tasks in the pool287 function WorkerThread(parentPool) {288 var _this = this;289 this.parentPool = parentPool;290 this.workerTask = {};291 //Execute the task292 this.run = function(workerTask) {293 _this.workerTask = workerTask;294 //Create a new web worker295 if (_this.workerTask.script != null) {296 /*297 Creation of workers.298 For both dedicated and shared workers, you can also attach to the299 message event handler event type by using the addEventListener method.300 */301 var worker;302 if(workerTask.type == PERFORMANCE_CONFIG.webworker_dedicated_literal){303 worker = new Worker(_this.workerTask.script);304 worker.addEventListener('message', _this.OnWorkerMessageHandler, false);305 worker.postMessage(_this.workerTask.startMessage);306 }else if(workerTask.type == PERFORMANCE_CONFIG.webworker_shared_literal){307 worker = new SharedWorker(_this.workerTask.script);308 worker.port.addEventListener('message', _this.OnWorkerMessageHandler, false);309 worker.port.postMessage(_this.workerTask.startMessage);310 }else{311 //NO TYPE ERROR312 $log.error("NO VALID WORKER TYPE");313 }314 }315 };316 //We assume we only get a single callback from a worker as a handler317 //It also indicates the end of this worker.318 _this.OnWorkerMessageHandler = function (evt) {319 // pass to original callback320 _this.workerTask.callback(evt);321 // We should use a separate thread to add the worker...
WorkerExecutionSupport.js
Source:WorkerExecutionSupport.js
...181 * @param {CodeBuilderInstructions} codeBuilderInstructions182 */183 buildWorker: function ( codeBuilderInstructions ) {184 let jsmSuccess = false;185 if ( codeBuilderInstructions.isSupportsJsmWorker() && codeBuilderInstructions.isPreferJsmWorker() ) {186 jsmSuccess = this._buildWorkerJsm( codeBuilderInstructions );187 }188 if ( ! jsmSuccess && codeBuilderInstructions.isSupportsStandardWorker() ) {189 this._buildWorkerStandard( codeBuilderInstructions );190 }191 },192 /**193 *194 * @param {CodeBuilderInstructions} codeBuilderInstructions195 * @return {boolean} Whether loading of jsm worker was successful196 * @private197 */198 _buildWorkerJsm: function ( codeBuilderInstructions ) {199 let jsmSuccess = true;200 let timeLabel = 'buildWorkerJsm';201 let workerAvailable = this._buildWorkerCheckPreconditions( true, timeLabel );202 if ( ! workerAvailable ) {203 try {204 let worker = new Worker( codeBuilderInstructions.jsmWorkerUrl.href, { type: "module" } );205 this._configureWorkerCommunication( worker, true, codeBuilderInstructions.defaultGeometryType, timeLabel );206 } catch ( e ) {207 jsmSuccess = false;208 // Chrome throws this exception, but Firefox currently does not complain, but can't execute the worker afterwards209 if ( e instanceof TypeError || e instanceof SyntaxError ) {210 console.error( "Modules are not supported in workers." );211 }212 }213 }214 return jsmSuccess;215 },216 /**217 * Validate the status of worker code and the derived worker and specify functions that should be build when new raw mesh data becomes available and when the parser is finished.218 *219 * @param {CodeBuilderIns} buildWorkerCode The function that is invoked to create the worker code of the parser.220 */221 /**222 *223 * @param {CodeBuilderInstructions} codeBuilderInstructions224 * @private225 */226 _buildWorkerStandard: function ( codeBuilderInstructions ) {227 let timeLabel = 'buildWorkerStandard';228 let workerAvailable = this._buildWorkerCheckPreconditions( false, timeLabel );229 if ( ! workerAvailable ) {230 let concatenateCode = '';231 codeBuilderInstructions.getImportStatements().forEach( function ( element ) {232 concatenateCode += element + '\n';233 } );234 concatenateCode += '\n';235 codeBuilderInstructions.getCodeFragments().forEach( function ( element ) {236 concatenateCode += element + '\n';237 } );238 concatenateCode += '\n';239 concatenateCode += codeBuilderInstructions.getStartCode();240 let blob = new Blob( [ concatenateCode ], { type: 'application/javascript' } );241 let worker = new Worker( window.URL.createObjectURL( blob ) );242 this._configureWorkerCommunication( worker, false, codeBuilderInstructions.defaultGeometryType, timeLabel );243 }244 },245 _buildWorkerCheckPreconditions: function ( requireJsmWorker, timeLabel ) {246 let workerAvailable = false;247 if ( this.isWorkerLoaded( requireJsmWorker ) ) {248 workerAvailable = true;249 } else {250 if ( this.logging.enabled ) {251 console.info( 'WorkerExecutionSupport: Building ' + ( requireJsmWorker ? 'jsm' : 'standard' ) + ' worker code...' );252 console.time( timeLabel );253 }254 }255 return workerAvailable;...
miner_stats.js
Source:miner_stats.js
1var workerHashrateData;2var workerHashrateChart;3var workerHistoryMax = 160;4var statData;5var totalHash;6var totalImmature;7var totalBal;8var totalPaid;9var totalShares;10function getReadableHashRateString(hashrate){11 hashrate = (hashrate * 2);12 if (hashrate < 1000000) {13 return (Math.round(hashrate / 1000) / 1000 ).toFixed(2)+' Sol/s';14 }15 var byteUnits = [ ' Sol/s', ' KSol/s', ' MSol/s', ' GSol/s', ' TSol/s', ' PSol/s' ];16 var i = Math.floor((Math.log(hashrate/1000) / Math.log(1000)) - 1);17 hashrate = (hashrate/1000) / Math.pow(1000, i + 1);18 return hashrate.toFixed(2) + byteUnits[i];19}20function timeOfDayFormat(timestamp){21 var dStr = d3.time.format('%I:%M %p')(new Date(timestamp));22 if (dStr.indexOf('0') === 0) dStr = dStr.slice(1);23 return dStr;24}25function getWorkerNameFromAddress(w) {26 var worker = w;27 if (w.split(".").length > 1) {28 worker = w.split(".")[1];29 if (worker == null || worker.length < 1) {30 worker = "noname";31 }32 } else {33 worker = "noname";34 }35 return worker;36}37function buildChartData(){38 var workers = {};39 for (var w in statData.history) {40 var worker = getWorkerNameFromAddress(w);41 var a = workers[worker] = (workers[worker] || {42 hashrate: []43 });44 for (var wh in statData.history[w]) {45 a.hashrate.push([statData.history[w][wh].time * 1000, statData.history[w][wh].hashrate]);46 }47 if (a.hashrate.length > workerHistoryMax) {48 workerHistoryMax = a.hashrate.length;49 }50 }51 52 var i=0;53 workerHashrateData = [];54 for (var worker in workers){55 workerHashrateData.push({56 key: worker,57 disabled: (i > Math.min((_workerCount-1), 3)),58 values: workers[worker].hashrate59 });60 i++;61 }62}63function updateChartData(){64 var workers = {};65 for (var w in statData.history) {66 var worker = getWorkerNameFromAddress(w);67 // get a reference to lastest workerhistory68 for (var wh in statData.history[w]) { }69 //var wh = statData.history[w][statData.history[w].length - 1];70 var foundWorker = false;71 for (var i = 0; i < workerHashrateData.length; i++) {72 if (workerHashrateData[i].key === worker) {73 foundWorker = true;74 if (workerHashrateData[i].values.length >= workerHistoryMax) {75 workerHashrateData[i].values.shift();76 }77 workerHashrateData[i].values.push([statData.history[w][wh].time * 1000, statData.history[w][wh].hashrate]);78 break;79 }80 }81 if (!foundWorker) {82 var hashrate = [];83 hashrate.push([statData.history[w][wh].time * 1000, statData.history[w][wh].hashrate]);84 workerHashrateData.push({85 key: worker,86 values: hashrate87 });88 rebuildWorkerDisplay();89 return true;90 }91 }92 triggerChartUpdates();93 return false;94}95function calculateAverageHashrate(worker) {96 var count = 0;97 var total = 1;98 var avg = 0;99 for (var i = 0; i < workerHashrateData.length; i++) {100 count = 0;101 for (var ii = 0; ii < workerHashrateData[i].values.length; ii++) {102 if (worker == null || workerHashrateData[i].key === worker) {103 count++;104 avg += parseFloat(workerHashrateData[i].values[ii][1]);105 }106 }107 if (count > total)108 total = count;109 }110 avg = avg / total;111 return avg;112}113function triggerChartUpdates(){114 workerHashrateChart.update();115}116function displayCharts() {117 nv.addGraph(function() {118 workerHashrateChart = nv.models.lineChart()119 .margin({left: 80, right: 30})120 .x(function(d){ return d[0] })121 .y(function(d){ return d[1] })122 .useInteractiveGuideline(true);123 workerHashrateChart.xAxis.tickFormat(timeOfDayFormat);124 workerHashrateChart.yAxis.tickFormat(function(d){125 return getReadableHashRateString(d);126 });127 d3.select('#workerHashrate').datum(workerHashrateData).call(workerHashrateChart);128 return workerHashrateChart;129 });130}131function updateStats() {132 totalHash = statData.totalHash;133 totalPaid = statData.paid;134 totalBal = statData.balance;135 totalImmature = statData.immature;136 totalShares = statData.totalShares;137 // do some calculations138 var _blocktime = 250;139 var _networkHashRate = parseFloat(statData.networkSols) * 1.2;140 var _myHashRate = (totalHash / 1000000) * 2;141 var luckDays = ((_networkHashRate / _myHashRate * _blocktime) / (24 * 60 * 60)).toFixed(3);142 // update miner stats143 $("#statsHashrate").text(getReadableHashRateString(totalHash));144 $("#statsHashrateAvg").text(getReadableHashRateString(calculateAverageHashrate(null)));145 $("#statsLuckDays").text(luckDays);146 $("#statsTotalImmature").text(totalImmature);147 $("#statsTotalBal").text(totalBal);148 $("#statsTotalPaid").text(totalPaid);149 $("#statsTotalShares").text(totalShares.toFixed(2));150}151function updateWorkerStats() {152 // update worker stats153 var i=0;154 for (var w in statData.workers) { i++;155 var htmlSafeWorkerName = w.split('.').join('_').replace(/[^\w\s]/gi, '');156 var saneWorkerName = getWorkerNameFromAddress(w);157 $("#statsHashrate"+htmlSafeWorkerName).text(getReadableHashRateString(statData.workers[w].hashrate));158 $("#statsHashrateAvg"+htmlSafeWorkerName).text(getReadableHashRateString(calculateAverageHashrate(saneWorkerName)));159 $("#statsLuckDays"+htmlSafeWorkerName).text(statData.workers[w].luckDays);160 $("#statsPaid"+htmlSafeWorkerName).text(statData.workers[w].paid);161 $("#statsBalance"+htmlSafeWorkerName).text(statData.workers[w].balance);162 $("#statsShares"+htmlSafeWorkerName).text(Math.round(statData.workers[w].currRoundShares * 100) / 100);163 $("#statsDiff"+htmlSafeWorkerName).text(statData.workers[w].diff);164 }165}166function addWorkerToDisplay(name, htmlSafeName, workerObj) {167 var htmlToAdd = "";168 htmlToAdd = '<div class="boxStats" id="boxStatsLeft" style="float:left; margin: 9px; min-width: 260px;"><div class="boxStatsList">';169 if (htmlSafeName.indexOf("_") >= 0) {170 htmlToAdd+= '<div class="boxLowerHeader">'+htmlSafeName.substr(htmlSafeName.indexOf("_")+1,htmlSafeName.length)+'</div>';171 } else {172 htmlToAdd+= '<div class="boxLowerHeader">noname</div>';173 }174 htmlToAdd+='<div><i class="fa fa-tachometer"></i> <span id="statsHashrate'+htmlSafeName+'">'+getReadableHashRateString(workerObj.hashrate)+'</span> (Now)</div>';175 htmlToAdd+='<div><i class="fa fa-tachometer"></i> <span id="statsHashrateAvg'+htmlSafeName+'">'+getReadableHashRateString(calculateAverageHashrate(name))+'</span> (Avg)</div>';176 htmlToAdd+='<div><i class="fa fa-shield"></i> <small>Diff:</small> <span id="statsDiff'+htmlSafeName+'">'+workerObj.diff+'</span></div>';177 htmlToAdd+='<div><i class="fa fa-cog"></i> <small>Shares:</small> <span id="statsShares'+htmlSafeName+'">'+(Math.round(workerObj.currRoundShares * 100) / 100)+'</span></div>';178 htmlToAdd+='<div><i class="fa fa-gavel"></i> <small>Luck <span id="statsLuckDays'+htmlSafeName+'">'+workerObj.luckDays+'</span> Days</small></div>';179 htmlToAdd+='<div><i class="fa fa-money"></i> <small>Bal: <span id="statsBalance'+htmlSafeName+'">'+workerObj.balance+'</span></small></div>';180 htmlToAdd+='<div><i class="fa fa-money"></i> <small>Paid: <span id="statsPaid'+htmlSafeName+'">'+workerObj.paid+'</span></small></div>';181 htmlToAdd+='</div></div></div>';182 $("#boxesWorkers").html($("#boxesWorkers").html()+htmlToAdd);183}184function rebuildWorkerDisplay() {185 $("#boxesWorkers").html("");186 var i=0;187 for (var w in statData.workers) { i++;188 var htmlSafeWorkerName = w.split('.').join('_').replace(/[^\w\s]/gi, '');189 var saneWorkerName = getWorkerNameFromAddress(w);190 addWorkerToDisplay(saneWorkerName, htmlSafeWorkerName, statData.workers[w]);191 }192}193// resize chart on window resize194nv.utils.windowResize(triggerChartUpdates);195// grab initial stats196$.getJSON('/api/worker_stats?'+_miner, function(data){197 statData = data;198 for (var w in statData.workers) { _workerCount++; }199 buildChartData();200 displayCharts();201 rebuildWorkerDisplay(); 202 updateStats();203});204// live stat updates205statsSource.addEventListener('message', function(e){206 // TODO, create miner_live_stats...207 // miner_live_stats will return the same josn except without the worker history208 // FOR NOW, use this to grab updated stats209 $.getJSON('/api/worker_stats?'+_miner, function(data){210 statData = data;211 // check for missing workers212 var wc = 0;213 var rebuilt = false;214 // update worker stats215 for (var w in statData.workers) { wc++; }216 // TODO, this isn't 100% fool proof!217 if (_workerCount != wc) {218 if (_workerCount > wc) {219 rebuildWorkerDisplay();220 rebuilt = true;221 }222 _workerCount = wc;223 }224 rebuilt = (rebuilt || updateChartData());225 updateStats();226 if (!rebuilt) {227 updateWorkerStats();228 }229 });...
WorkerManager.js
Source:WorkerManager.js
...109 },110 openWorkerInspector: function(workerId)111 {112 this._openInspectorWindow(workerId);113 WorkerAgent.connectToWorker(workerId);114 },115 _openInspectorWindow: function(workerId)116 {117 var url = window.location.href + "&dedicatedWorkerId=" + workerId;118 url = url.replace("docked=true&", "");119 // Set location=0 just to make sure the front-end will be opened in a separate window, not in new tab.120 var workerInspectorWindow = window.open(url, undefined, "location=0");121 this._workerIdToWindow[workerId] = workerInspectorWindow;122 workerInspectorWindow.addEventListener("beforeunload", this._workerInspectorClosing.bind(this, workerId), true);123 // Listen to beforeunload in detached state and to the InspectorClosing event in case of attached inspector.124 window.addEventListener("beforeunload", this._pageInspectorClosing.bind(this), true);125 WebInspector.notifications.addEventListener(WebInspector.Events.InspectorClosing, this._pageInspectorClosing, this);126 },127 closeWorkerInspector: function(workerId)128 {129 var workerInspectorWindow = this._workerIdToWindow[workerId];130 if (workerInspectorWindow)131 workerInspectorWindow.close();132 },133 reset: function()134 {135 for (var workerId in this._workerIdToWindow)136 this.closeWorkerInspector(workerId);137 this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkersCleared);138 },139 _pageInspectorClosing: function()140 {141 this._ignoreWorkerInspectorClosing = true;142 for (var workerId in this._workerIdToWindow) {143 this._workerIdToWindow[workerId].close();144 WorkerAgent.disconnectFromWorker(parseInt(workerId, 10));145 }146 },147 _workerInspectorClosing: function(workerId, event)148 {149 if (this._ignoreWorkerInspectorClosing)150 return;151 delete this._workerIdToWindow[workerId];152 WorkerAgent.disconnectFromWorker(workerId);153 this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkerInspectorClosed, workerId);154 }155}156WebInspector.WorkerManager.prototype.__proto__ = WebInspector.Object.prototype;157/**158 * @constructor159 * @implements {WorkerAgent.Dispatcher}160 */161WebInspector.DedicatedWorkerMessageForwarder = function(workerManager)162{163 this._workerManager = workerManager;164 window.addEventListener("message", this._receiveMessage.bind(this), true);165}166WebInspector.DedicatedWorkerMessageForwarder.prototype = {167 _receiveMessage: function(event)168 {169 var workerId = event.data["workerId"];170 workerId = parseInt(workerId, 10);171 var command = event.data.command;172 var message = event.data.message;173 if (command == "sendMessageToBackend")174 WorkerAgent.sendMessageToWorker(workerId, message);175 },176 workerCreated: function(workerId, url, inspectorConnected)177 {178 this._workerManager._workerCreated(workerId, url, inspectorConnected);179 },180 workerTerminated: function(workerId)181 {182 this._workerManager._workerTerminated(workerId);183 },184 dispatchMessageFromWorker: function(workerId, message)185 {186 this._workerManager._sendMessageToWorkerInspector(workerId, message);187 }188}
WorkersSidebarPane.js
Source:WorkersSidebarPane.js
...51 addWorker: function(id, url, isShared)52 {53 if (id in this._workers)54 return;55 var worker = new WebInspector.Worker(id, url, isShared);56 this._workers[id] = worker;57 var title = WebInspector.linkifyURL(url, WebInspector.displayNameForURL(url), "worker-item", true, url);58 var treeElement = new TreeElement(null, worker, false);59 treeElement.titleHTML = title;60 this._treeOutline.appendChild(treeElement);61 },62 removeWorker: function(id)63 {64 if (id in this._workers) {65 this._treeOutline.removeChild(this._treeOutline.getCachedTreeElement(this._workers[id]));66 delete this._workers[id];67 }68 },69 setInstrumentation: function(enabled)70 {71 PageAgent.removeAllScriptsToEvaluateOnLoad();72 if (enabled)73 PageAgent.addScriptToEvaluateOnLoad("(" + InjectedFakeWorker + ")");74 },75 reset: function()76 {77 this.setInstrumentation(this._enableWorkersCheckbox.checked);78 this._treeOutline.removeChildren();79 this._workers = {};80 },81 _onTriggerInstrument: function(event)82 {83 this.setInstrumentation(this._enableWorkersCheckbox.checked);84 }85};86WebInspector.WorkersSidebarPane.prototype.__proto__ = WebInspector.SidebarPane.prototype;87/**88 * @constructor89 */90WebInspector.Worker = function(id, url, shared)91{92 this.id = id;93 this.url = url;94 this.shared = shared;95}96/**97 * @constructor98 * @extends {WebInspector.SidebarPane}99 */100WebInspector.WorkerListSidebarPane = function(workerManager)101{102 WebInspector.SidebarPane.call(this, WebInspector.UIString("Worker inspectors"));103 this._enableWorkersCheckbox = new WebInspector.Checkbox(104 WebInspector.UIString("Debug"),105 "sidebar-pane-subtitle",106 WebInspector.UIString("Automatically attach to new workers. Enabling this option will force opening inspector for all new workers."));107 this.titleElement.insertBefore(this._enableWorkersCheckbox.element, this.titleElement.firstChild);108 this._enableWorkersCheckbox.addEventListener(this._autoattachToWorkersClicked.bind(this));109 this._enableWorkersCheckbox.checked = false;110 this._workerListElement = document.createElement("ol");111 this._workerListElement.tabIndex = 0;112 this._workerListElement.addStyleClass("properties-tree");113 this._workerListTreeOutline = new TreeOutline(this._workerListElement);114 this.bodyElement.appendChild(this._workerListElement);115 this._idToWorkerItem = {};116 this._workerManager = workerManager;117 workerManager.addEventListener(WebInspector.WorkerManager.Events.WorkerAdded, this._workerAdded, this);118 workerManager.addEventListener(WebInspector.WorkerManager.Events.WorkerRemoved, this._workerRemoved, this);119 workerManager.addEventListener(WebInspector.WorkerManager.Events.WorkersCleared, this._workersCleared, this);120 workerManager.addEventListener(WebInspector.WorkerManager.Events.WorkerInspectorClosed, this._workerInspectorClosed, this);121}122WebInspector.WorkerListSidebarPane.prototype = {123 _workerAdded: function(event)124 {125 this._addWorker(event.data.workerId, event.data.url, event.data.inspectorConnected);126 },127 _workerRemoved: function(event)128 {129 var workerItem = this._idToWorkerItem[event.data];130 delete this._idToWorkerItem[event.data];131 workerItem.element.parent.removeChild(workerItem.element);132 },133 _workerInspectorClosed: function(event)134 {135 var workerItem = this._idToWorkerItem[event.data];136 workerItem.checkbox.checked = false;137 },138 _workersCleared: function(event)139 {...
NodeThreadsWorker.test.js
Source:NodeThreadsWorker.test.js
...40});41it('passes fork options down to child_process.fork, adding the defaults', () => {42 const child = require.resolve('../threadChild');43 process.execArgv = ['--inspect', '-p'];44 new Worker({45 forkOptions: {46 cwd: '/tmp',47 execPath: 'hello',48 },49 maxRetries: 3,50 workerId: process.env.JEST_WORKER_ID,51 workerPath: '/tmp/foo/bar/baz.js',52 });53 expect(childProcess.mock.calls[0][0]).toBe(child);54 expect(childProcess.mock.calls[0][1]).toEqual({55 eval: false,56 stderr: true,57 stdout: true,58 workerData: {59 cwd: '/tmp', // Overridden default option.60 env: process.env, // Default option.61 execArgv: ['-p'], // Filtered option.62 execPath: 'hello', // Added option.63 silent: true, // Default option.64 },65 });66});67it('passes workerId to the child process and assign it to env.JEST_WORKER_ID', () => {68 new Worker({69 forkOptions: {},70 maxRetries: 3,71 workerId: 2,72 workerPath: '/tmp/foo',73 });74 expect(childProcess.mock.calls[0][1].workerData.env.JEST_WORKER_ID).toEqual(75 2,76 );77});78it('initializes the child process with the given workerPath', () => {79 const worker = new Worker({80 forkOptions: {},81 maxRetries: 3,82 setupArgs: ['foo', 'bar'],83 workerPath: '/tmp/foo/bar/baz.js',84 });85 expect(worker._worker.postMessage.mock.calls[0][0]).toEqual([86 CHILD_MESSAGE_INITIALIZE,87 false,88 '/tmp/foo/bar/baz.js',89 ['foo', 'bar'],90 ]);91});92it('stops initializing the worker after the amount of retries is exceeded', () => {93 const worker = new Worker({94 forkOptions: {},95 maxRetries: 3,96 workerPath: '/tmp/foo/bar/baz.js',97 });98 const request = [CHILD_MESSAGE_CALL, false, 'foo', []];99 const onProcessStart = jest.fn();100 const onProcessEnd = jest.fn();101 worker.send(request, onProcessStart, onProcessEnd);102 // We fail four times (initial + three retries).103 worker._worker.emit('exit');104 worker._worker.emit('exit');105 worker._worker.emit('exit');106 worker._worker.emit('exit');107 expect(childProcess).toHaveBeenCalledTimes(5);108 expect(onProcessStart).toBeCalledWith(worker);109 expect(onProcessEnd).toHaveBeenCalledTimes(1);110 expect(onProcessEnd.mock.calls[0][0]).toBeInstanceOf(Error);111 expect(onProcessEnd.mock.calls[0][0].type).toBe('WorkerError');112 expect(onProcessEnd.mock.calls[0][1]).toBe(null);113});114it('provides stdout and stderr fields from the child process', () => {115 const worker = new Worker({116 forkOptions: {},117 maxRetries: 3,118 workerPath: '/tmp/foo',119 });120 expect(worker.getStdout()).toBe('stdout');121 expect(worker.getStderr()).toBe('stderr');122});123it('sends the task to the child process', () => {124 const worker = new Worker({125 forkOptions: {},126 maxRetries: 3,127 workerPath: '/tmp/foo',128 });129 const request = [CHILD_MESSAGE_CALL, false, 'foo', []];130 worker.send(request, () => {}, () => {});131 // Skipping call "0" because it corresponds to the "initialize" one.132 expect(worker._worker.postMessage.mock.calls[1][0]).toEqual(request);133});134it('calls the onProcessStart method synchronously if the queue is empty', () => {135 const worker = new Worker({136 forkOptions: {},137 maxRetries: 3,138 workerPath: '/tmp/foo',139 });140 const onProcessStart = jest.fn();141 const onProcessEnd = jest.fn();142 worker.send(143 [CHILD_MESSAGE_CALL, false, 'foo', []],144 onProcessStart,145 onProcessEnd,146 );147 // Only onProcessStart has been called148 expect(onProcessStart).toHaveBeenCalledTimes(1);149 expect(onProcessEnd).not.toHaveBeenCalled();150 // then first call replies...151 worker._worker.emit('message', [PARENT_MESSAGE_OK]);152 expect(onProcessEnd).toHaveBeenCalledTimes(1);153});154it('creates error instances for known errors', () => {155 const worker = new Worker({156 forkOptions: {},157 maxRetries: 3,158 workerPath: '/tmp/foo',159 });160 const callback1 = jest.fn();161 const callback2 = jest.fn();162 const callback3 = jest.fn();163 // Testing a generic ECMAScript error.164 worker.send([CHILD_MESSAGE_CALL, false, 'method', []], () => {}, callback1);165 worker._worker.emit('message', [166 PARENT_MESSAGE_CLIENT_ERROR,167 'TypeError',168 'bar',169 'TypeError: bar',170 {},171 ]);172 expect(callback1.mock.calls[0][0]).toBeInstanceOf(TypeError);173 expect(callback1.mock.calls[0][0].message).toBe('bar');174 expect(callback1.mock.calls[0][0].type).toBe('TypeError');175 expect(callback1.mock.calls[0][0].stack).toBe('TypeError: bar');176 // Testing a custom error.177 worker.send([CHILD_MESSAGE_CALL, false, 'method', []], () => {}, callback2);178 worker._worker.emit('message', [179 PARENT_MESSAGE_CLIENT_ERROR,180 'RandomCustomError',181 'bar',182 'RandomCustomError: bar',183 {qux: 'extra property'},184 ]);185 expect(callback2.mock.calls[0][0]).toBeInstanceOf(Error);186 expect(callback2.mock.calls[0][0].message).toBe('bar');187 expect(callback2.mock.calls[0][0].type).toBe('RandomCustomError');188 expect(callback2.mock.calls[0][0].stack).toBe('RandomCustomError: bar');189 expect(callback2.mock.calls[0][0].qux).toBe('extra property');190 // Testing a non-object throw.191 worker.send([CHILD_MESSAGE_CALL, false, 'method', []], () => {}, callback3);192 worker._worker.emit('message', [193 PARENT_MESSAGE_CLIENT_ERROR,194 'Number',195 null,196 null,197 412,198 ]);199 expect(callback3.mock.calls[0][0]).toBe(412);200});201it('throws when the child process returns a strange message', () => {202 const worker = new Worker({203 forkOptions: {},204 maxRetries: 3,205 workerPath: '/tmp/foo',206 });207 worker.send([CHILD_MESSAGE_CALL, false, 'method', []], () => {}, () => {});208 // Type 27 does not exist.209 expect(() => {210 worker._worker.emit('message', [27]);211 }).toThrow(TypeError);212});213it('does not restart the child if it cleanly exited', () => {214 const worker = new Worker({215 forkOptions: {},216 maxRetries: 3,217 workerPath: '/tmp/foo',218 });219 expect(childProcess).toHaveBeenCalledTimes(1);220 worker._worker.emit('exit', 0);221 expect(childProcess).toHaveBeenCalledTimes(1);222});223it('restarts the child when the child process dies', () => {224 const worker = new Worker({225 workerPath: '/tmp/foo',226 });227 expect(childProcess).toHaveBeenCalledTimes(1);228 worker._worker.emit('exit', 1);229 expect(childProcess).toHaveBeenCalledTimes(2);...
test-Worker.js
Source:test-Worker.js
...8expect.extend({ toBeType });9let worker;10beforeEach(() => worker && !worker.closed && worker.close());11afterEach(() => worker && !worker.closed && worker.close());12test('createWorker() succeeds', async () =>13{14 const onObserverNewWorker = jest.fn();15 observer.once('newworker', onObserverNewWorker);16 worker = await createWorker();17 expect(onObserverNewWorker).toHaveBeenCalledTimes(1);18 expect(onObserverNewWorker).toHaveBeenCalledWith(worker);19 expect(worker).toBeType('object');20 expect(worker.pid).toBeType('number');21 expect(worker.closed).toBe(false);22 expect(worker.died).toBe(false);23 worker.close();24 expect(worker.closed).toBe(true);25 expect(worker.died).toBe(false);26 // eslint-disable-next-line require-atomic-updates27 worker = await createWorker(28 {29 logLevel : 'debug',30 logTags : [ 'info' ],31 rtcMinPort : 0,32 rtcMaxPort : 9999,33 dtlsCertificateFile : path.join(__dirname, 'data', 'dtls-cert.pem'),34 dtlsPrivateKeyFile : path.join(__dirname, 'data', 'dtls-key.pem'),35 appData : { bar: 456 }36 });37 expect(worker).toBeType('object');38 expect(worker.pid).toBeType('number');39 expect(worker.closed).toBe(false);40 expect(worker.died).toBe(false);41 expect(worker.appData).toEqual({ bar: 456 });42 worker.close();43 expect(worker.closed).toBe(true);44 expect(worker.died).toBe(false);45}, 2000);46test('createWorker() with wrong settings rejects with TypeError', async () =>47{48 await expect(createWorker({ logLevel: 'chicken' }))49 .rejects50 .toThrow(TypeError);51 await expect(createWorker({ rtcMinPort: 1000, rtcMaxPort: 999 }))52 .rejects53 .toThrow(TypeError);54 // Port is from 0 to 65535.55 await expect(createWorker({ rtcMinPort: 1000, rtcMaxPort: 65536 }))56 .rejects57 .toThrow(TypeError);58 await expect(createWorker({ dtlsCertificateFile: '/notfound/cert.pem' }))59 .rejects60 .toThrow(TypeError);61 await expect(createWorker({ dtlsPrivateKeyFile: '/notfound/priv.pem' }))62 .rejects63 .toThrow(TypeError);64 await expect(createWorker({ appData: 'NOT-AN-OBJECT' }))65 .rejects66 .toThrow(TypeError);67}, 2000);68test('worker.updateSettings() succeeds', async () =>69{70 worker = await createWorker();71 await expect(worker.updateSettings({ logLevel: 'debug', logTags: [ 'ice' ] }))72 .resolves73 .toBeUndefined();74 worker.close();75}, 2000);76test('worker.updateSettings() with wrong settings rejects with TypeError', async () =>77{78 worker = await createWorker();79 await expect(worker.updateSettings({ logLevel: 'chicken' }))80 .rejects81 .toThrow(TypeError);82 worker.close();83}, 2000);84test('worker.updateSettings() rejects with InvalidStateError if closed', async () =>85{86 worker = await createWorker();87 worker.close();88 await expect(worker.updateSettings({ logLevel: 'error' }))89 .rejects90 .toThrow(InvalidStateError);91 worker.close();92}, 2000);93test('worker.dump() succeeds', async () =>94{95 worker = await createWorker();96 await expect(worker.dump())97 .resolves98 .toEqual({ pid: worker.pid, routerIds: [] });99 worker.close();100}, 2000);101test('worker.dump() rejects with InvalidStateError if closed', async () =>102{103 worker = await createWorker();104 worker.close();105 await expect(worker.dump())106 .rejects107 .toThrow(InvalidStateError);108 worker.close();109}, 2000);110test('worker.getResourceUsage() succeeds', async () =>111{112 worker = await createWorker();113 await expect(worker.getResourceUsage())114 .resolves115 .toMatchObject({});116 worker.close();117}, 2000);118test('worker.close() succeeds', async () =>119{120 worker = await createWorker({ logLevel: 'warn' });121 const onObserverClose = jest.fn();122 worker.observer.once('close', onObserverClose);123 worker.close();124 expect(onObserverClose).toHaveBeenCalledTimes(1);125 expect(worker.closed).toBe(true);126 expect(worker.died).toBe(false);127}, 2000);128test('Worker emits "died" if worker process died unexpectedly', async () =>129{130 let onDied;131 let onObserverClose;132 worker = await createWorker({ logLevel: 'warn' });133 onDied = jest.fn();134 onObserverClose = jest.fn();135 worker.observer.once('close', onObserverClose);136 await new Promise((resolve, reject) =>137 {138 worker.on('died', () =>139 {140 onDied();141 if (onObserverClose.mock.calls.length > 0)142 {143 reject(144 new Error('observer "close" event emitted before worker "died" event'));145 }146 else if (worker.closed)147 {148 resolve();149 }150 else151 {152 reject(new Error('worker.closed is false'));153 }154 });155 process.kill(worker.pid, 'SIGINT');156 });157 expect(onDied).toHaveBeenCalledTimes(1);158 expect(onObserverClose).toHaveBeenCalledTimes(1);159 expect(worker.closed).toBe(true);160 expect(worker.died).toBe(true);161 // eslint-disable-next-line require-atomic-updates162 worker = await createWorker({ logLevel: 'warn' });163 onDied = jest.fn();164 onObserverClose = jest.fn();165 worker.observer.once('close', onObserverClose);166 await new Promise((resolve, reject) =>167 {168 worker.on('died', () =>169 {170 onDied();171 if (onObserverClose.mock.calls.length > 0)172 {173 reject(174 new Error('observer "close" event emitted before worker "died" event'));175 }176 else if (worker.closed)177 {178 resolve();179 }180 else181 {182 reject(new Error('worker.closed is false'));183 }184 });185 process.kill(worker.pid, 'SIGTERM');186 });187 expect(onDied).toHaveBeenCalledTimes(1);188 expect(onObserverClose).toHaveBeenCalledTimes(1);189 expect(worker.closed).toBe(true);190 expect(worker.died).toBe(true);191 // eslint-disable-next-line require-atomic-updates192 worker = await createWorker({ logLevel: 'warn' });193 onDied = jest.fn();194 onObserverClose = jest.fn();195 worker.observer.once('close', onObserverClose);196 await new Promise((resolve, reject) =>197 {198 worker.on('died', () =>199 {200 onDied();201 if (onObserverClose.mock.calls.length > 0)202 {203 reject(204 new Error('observer "close" event emitted before worker "died" event'));205 }206 else if (worker.closed)207 {208 resolve();209 }210 else211 {212 reject(new Error('worker.closed is false'));213 }214 });215 process.kill(worker.pid, 'SIGKILL');216 });217 expect(onDied).toHaveBeenCalledTimes(1);218 expect(onObserverClose).toHaveBeenCalledTimes(1);219 expect(worker.closed).toBe(true);220 expect(worker.died).toBe(true);221}, 5000);222test('worker process ignores PIPE, HUP, ALRM, USR1 and USR2 signals', async () =>223{224 // Windows doesn't have some signals such as SIGPIPE, SIGALRM, SIGUSR1, SIGUSR2225 // so we just skip this test in Windows.226 if (os.platform() === 'win32')227 return;228 worker = await createWorker({ logLevel: 'warn' });229 await new Promise((resolve, reject) =>230 {231 worker.on('died', reject);232 process.kill(worker.pid, 'SIGPIPE');233 process.kill(worker.pid, 'SIGHUP');234 process.kill(worker.pid, 'SIGALRM');235 process.kill(worker.pid, 'SIGUSR1');236 process.kill(worker.pid, 'SIGUSR2');237 setTimeout(() =>238 {239 expect(worker.closed).toBe(false);240 worker.close();241 resolve();242 }, 2000);...
workerpool.js
Source:workerpool.js
...89 * @param {string} code The code to execute inside the worker.90 * @return {goog.gears.Worker} The worker that was just created.91 */92goog.gears.WorkerPool.prototype.createWorker = function(code) {93 var workerId = this.workerPool_.createWorker(code);94 var worker = new goog.gears.Worker(this, workerId);95 this.registerWorker(worker);96 return worker;97};98/**99 * Creates a new worker from a URL.100 * @param {string} url URL from which to get the code to execute inside the101 * worker.102 * @return {goog.gears.Worker} The worker that was just created.103 */104goog.gears.WorkerPool.prototype.createWorkerFromUrl = function(url) {105 var workerId = this.workerPool_.createWorkerFromUrl(url);106 var worker = new goog.gears.Worker(this, workerId);107 this.registerWorker(worker);108 return worker;109};110/**111 * Allows the worker who calls this to be used cross origin.112 */113goog.gears.WorkerPool.prototype.allowCrossOrigin = function() {114 this.workerPool_.allowCrossOrigin();115};116/**117 * Sends a message to a given worker.118 * @param {*} message The message to send to the worker.119 * @param {goog.gears.Worker} worker The worker to send the message to.120 */121goog.gears.WorkerPool.prototype.sendMessage = function(message, worker) {...
Using AI Code Generation
1const { Worker } = require('worker_threads');2const worker = new Worker('./worker.js');3worker.on('message', (message) => {4 console.log(message);5});6worker.on('error', (error) => {7 console.error(error);8});9worker.on('exit', (code) => {10 if (code !== 0) {11 console.error(new Error(`Worker stopped with exit code ${code}`));12 }13});14worker.postMessage('hello');15const { parentPort } = require('worker_threads');16parentPort.on('message', (message) => {17 console.log(message);18 parentPort.postMessage('world');19});20parentPort.on('error', (error) => {21 console.error(error);22});23parentPort.on('exit', (code) => {24 if (code !== 0) {25 console.error(new Error(`Worker stopped with exit code ${code}`));26 }27});28const { Worker } = require('worker_threads');29const worker = new Worker('./worker.js');30worker.on('message', (message) => {31 console.log(message);32});33worker.on('error', (error) => {34 console.error(error);35});36worker.on('exit', (code) => {37 if (code !== 0) {38 console.error(new Error(`Worker stopped with exit code ${code}`));39 }40});41worker.postMessage('hello');42const { parentPort } = require('worker_threads');43parentPort.on('message', (message) => {44 console.log(message);45 parentPort.postMessage('world');46});47parentPort.on('error', (error) => {48 console.error(error);49});50parentPort.on('exit', (code) => {51 if (code !== 0) {52 console.error(new Error(`Worker stopped with exit code ${code}`));53 }54});55const { Worker } = require('worker_threads');56const worker = new Worker('./worker.js');57worker.on('message', (message) => {58 console.log(message);59});60worker.on('error', (error) => {61 console.error(error);62});63worker.on('exit',
Using AI Code Generation
1const { Worker } = require('worker_threads');2const worker = new Worker('./worker.js');3worker.on('message', (message) => {4 console.log(message);5});6worker.on('error', (error) => {7 console.error(error);8});9worker.on('exit', (code) => {10 if (code !== 0) {11 console.error(new Error(`Worker stopped with exit code ${code}`));12 }13});14worker.postMessage('Hello World');15const { parentPort } = require('worker_threads');16parentPort.on('message', (message) => {17 parentPort.postMessage(message);18});19parentPort.on('error', (error) => {20 parentPort.postMessage(error);21});22parentPort.on('exit', (code) => {23 if (code !== 0) {24 parentPort.postMessage(new Error(`Worker stopped with exit code ${code}`));25 }26});27parentPort.postMessage('Hello World');28jest.mock('worker_threads', () => ({29 Worker: class Worker {30 constructor() {31 this.onMessage = null;32 this.onError = null;33 this.onExit = null;34 }35 postMessage(message) {36 this.onMessage(message);37 }38 on(message, callback) {39 if (message === 'message') {40 this.onMessage = callback;41 } else if (message === 'error') {42 this.onError = callback;43 } else if (message === 'exit') {44 this.onExit = callback;45 }46 }47 },48}));49const { Worker } = require('worker_threads');50const worker = new Worker('./worker.js');51worker.on('message', (message) => {52 console.log(message);53});54worker.on('error', (error) => {55 console.error(error);56});57worker.on('
Using AI Code Generation
1const { Worker } = require('worker_threads');2const worker = new Worker('./worker.js');3worker.on('message', (msg) => {4 console.log(msg);5});6worker.on('error', (err) => {7 console.error(err);8});9worker.on('exit', (code) => {10 if (code !== 0)11 console.error(new Error(`Worker stopped with exit code ${code}`));12});13const { workerData } = require('worker_threads');14console.log(workerData);
Using AI Code Generation
1const { Worker } = require("worker_threads");2const worker = new Worker("./worker.js");3worker.on("message", (message) => {4 console.log(message);5});6worker.on("error", (error) => {7 console.log(error);8});9worker.on("exit", (code) => {10 if (code !== 0) {11 console.log(`Worker stopped with exit code ${code}`);12 }13});14const { Worker } = require("worker_threads");15const worker = new Worker("./worker.js");16worker.on("message", (message) => {17 console.log(message);18});19worker.on("error", (error) => {20 console.log(error);21});22worker.on("exit", (code) => {23 if (code !== 0) {24 console.log(`Worker stopped with exit code ${code}`);25 }26});27const { parentPort } = require("worker_threads");28parentPort.on("message", (message) => {29 console.log(message);30});31parentPort.postMessage("hello from worker");
Using AI Code Generation
1const fc = require("fast-check");2const { Worker } = require("worker_threads");3const worker = new Worker("./worker.js");4worker.on("message", (message) => {5 console.log(message);6});7worker.on("error", (error) => {8 console.error(error);9});10worker.on("exit", (code) => {11 if (code !== 0)12 console.error(13 new Error(`Worker stopped with exit code ${code}`)14 );15});16worker.postMessage("start");17const { parentPort } = require("worker_threads");18const fc = require("fast-check");19parentPort.on("message", (message) => {20 if (message === "start") {21 fc.assert(22 fc.property(fc.integer(), (x) => {23 return x + 1 > x;24 })25 );26 }27});28{29 "scripts": {30 },31 "dependencies": {32 }33}34at Object.<anonymous> (test.js:17:5)35at Module._compile (internal/modules/cjs/loader.js:1138:30)36at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)37at Module.load (internal/modules/cjs/loader.js:986:32)38at Function.Module._load (internal/modules/cjs/loader.js:879:14)39at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
Using AI Code Generation
1const { Worker } = require('worker_threads');2const fs = require('fs');3const path = require('path');4const workerPath = path.join(__dirname, 'worker.js');5const worker = new Worker(workerPath);6const data = fs.readFileSync(path.join(__dirname, 'data.json'));7worker.postMessage(data);8const { parentPort } = require('worker_threads');9const fc = require('fast-check');10parentPort.on('message', data => {11 fc.assert(12 fc.property(fc.integer(), x => {13 return x * 2 === 2 * x;14 })15 );16});
Using AI Code Generation
1const { Worker } = require('worker_threads');2const worker = new Worker('./worker.js');3worker.on('message', (message) => {4 if (message.type === 'fast-check') {5 console.log('fast-check is ready');6 worker.postMessage({ type: 'run', params: { numRuns: 1000 } });7 }8 if (message.type === 'run') {9 console.log('run result', message.result);10 worker.terminate();11 }12});13worker.on('error', (error) => {14 console.log('error', error);15 worker.terminate();16});17worker.on('exit', (code) => {18 if (code !== 0) {19 console.log('exit with error code', code);20 }21});22const { parentPort } = require('worker_threads');23const { run } = require('fast-check-monorepo');24parentPort.on('message', (message) => {25 if (message.type === 'run') {26 run(message.params).then((result) => {27 parentPort.postMessage({ type: 'run', result });28 });29 }30});31{32 "dependencies": {33 }34}35const { run } = require('fast-check-monorepo');36run({ numRuns: 1000 }).then((result) => {37 console.log('run result', result);38});39{40 "dependencies": {41 }42}
Using AI Code Generation
1const fc = require('fast-check/lib/Worker');2const { check } = fc;3const isEven = (n) => n % 2 === 0;4const isOdd = (n) => n % 2 !== 0;5const isInteger = (n) => Number.isInteger(n);6const isPositive = (n) => n > 0;7const isNegative = (n) => n < 0;8const isZero = (n) => n === 0;9const isEvenInteger = (n) => isEven(n) && isInteger(n);10const isOddInteger = (n) => isOdd(n) && isInteger(n);11const isPositiveInteger = (n) => isPositive(n) && isInteger(n);12const isNegativeInteger = (n) => isNegative(n) && isInteger(n);13const isPositiveOrZero = (n) => isPositive(n) || isZero(n);14const isNegativeOrZero = (n) => isNegative(n) || isZero(n);15const isPositiveOddInteger = (n) => isPositiveInteger(n) && isOddInteger(n);16const isPositiveEvenInteger = (n) => isPositiveInteger(n) && isEvenInteger(n);17const isNegativeOddInteger = (n) => isNegativeInteger(n) && isOddInteger(n);18const isNegativeEvenInteger = (n) => isNegativeInteger(n) && isEvenInteger(n);19const isPositiveOrZeroOddInteger = (n) => isPositiveOrZero(n) && isOddInteger(n);20const isPositiveOrZeroEvenInteger = (n) => isPositiveOrZero(n) && isEvenInteger(n);21const isNegativeOrZeroOddInteger = (n) => isNegativeOrZero(n) && isOddInteger(n);22const isNegativeOrZeroEvenInteger = (n) => isNegativeOrZero(n) && isEvenInteger(n);23const isPositiveOrNegativeOddInteger = (n) => isPositiveOrZeroOddInteger(n) || isNegativeOrZeroOddInteger(n);24const isPositiveOrNegativeEvenInteger = (n) => isPositiveOrZeroEvenInteger(n) || isNegativeOrZeroEvenInteger(n);25const isPositiveOrNegativeInteger = (n) => isPositiveOrZero(n) || isNegativeOrZero(n);
Using AI Code Generation
1import * as fc from 'fast-check';2import { Worker } from 'worker_threads';3const worker = new Worker(new URL('./worker.js', import.meta.url));4const workerFunction = (x) => {5 return new Promise((resolve) => {6 worker.postMessage({ x });7 worker.on('message', resolve);8 });9};10const mainFunction = (x) => {11 return Promise.resolve(x + 1);12};13const mainFunction2 = (x) => {14 return Promise.resolve(x + 2);15};16const mainFunction3 = (x) => {17 return Promise.resolve(x + 3);18};19const mainFunction4 = (x) => {20 return Promise.resolve(x + 4);21};22fc.assert(23 fc.asyncProperty(fc.integer(), async (x) => {24 const result = await workerFunction(x);25 const result2 = await mainFunction(x);26 const result3 = await mainFunction2(x);27 const result4 = await mainFunction3(x);28 const result5 = await mainFunction4(x);29 return result === x + 1 && result2 === x + 1 && result3 === x + 2 && result4 === x + 3 && result5 === x + 4;30 })31);32worker.terminate();33const { parentPort } = require('worker_threads');34parentPort.on('message', ({ x }) => {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!