Best JavaScript code snippet using stryker-parent
pool.spec.ts
Source:pool.spec.ts
1import { expect } from 'chai';2import { toArray } from 'rxjs/operators';3import sinon from 'sinon';4import { factory, tick } from '@stryker-mutator/test-helpers';5import { Task, ExpirableTask } from '@stryker-mutator/util';6import { lastValueFrom, range, ReplaySubject } from 'rxjs';7import { Pool, Resource } from '../../../src/concurrent';8describe(Pool.name, () => {9 let worker1: sinon.SinonStubbedInstance<Required<Resource>>;10 let worker2: sinon.SinonStubbedInstance<Required<Resource>>;11 let genericWorkerForAllSubsequentCreates: sinon.SinonStubbedInstance<Required<Resource>>;12 let createWorkerStub: sinon.SinonStub;13 let concurrencyToken$: ReplaySubject<number>;14 let sut: Pool<Required<Resource>>;15 beforeEach(() => {16 concurrencyToken$ = new ReplaySubject();17 worker1 = factory.testRunner();18 worker2 = factory.testRunner();19 genericWorkerForAllSubsequentCreates = factory.testRunner();20 createWorkerStub = sinon.stub();21 });22 afterEach(() => {23 concurrencyToken$.complete();24 sut.dispose();25 });26 function arrangeWorkers() {27 createWorkerStub.returns(genericWorkerForAllSubsequentCreates).onCall(0).returns(worker1).onCall(1).returns(worker2);28 }29 function createSut() {30 return new Pool<Required<Resource>>(createWorkerStub, concurrencyToken$);31 }32 function setConcurrency(n: number) {33 Array.from({ length: n }).forEach((_, i) => concurrencyToken$.next(i));34 }35 describe('schedule', () => {36 it("should create 2 workers at a time (for performance reasons, we don't to overwhelm the device)", async () => {37 // Arrange38 arrangeWorkers();39 setConcurrency(3);40 const initWorker1Task = new Task<void>();41 const initWorker2Task = new Task<void>();42 const initWorker3Task = new Task<void>();43 worker1.init.returns(initWorker1Task.promise);44 worker2.init.returns(initWorker2Task.promise);45 genericWorkerForAllSubsequentCreates.init.returns(initWorker3Task.promise);46 sut = createSut();47 const actualWorkers: Array<Required<Resource>> = [];48 // Act49 const onGoingTask = lastValueFrom(sut.schedule(range(0, 3), async (worker) => actualWorkers.push(worker)));50 // Assert51 expect(actualWorkers).lengthOf(0);52 expect(createWorkerStub).callCount(2);53 initWorker1Task.resolve();54 initWorker2Task.resolve();55 initWorker3Task.resolve();56 await tick(2);57 await sut.dispose();58 expect(actualWorkers).lengthOf(3);59 await onGoingTask;60 });61 it('should eventually create all workers', async () => {62 arrangeWorkers();63 setConcurrency(8);64 sut = createSut();65 const result = await captureWorkers(sut, 8);66 expect(result).lengthOf(8);67 expect(result).deep.eq([68 worker1,69 worker2,70 genericWorkerForAllSubsequentCreates,71 genericWorkerForAllSubsequentCreates,72 genericWorkerForAllSubsequentCreates,73 genericWorkerForAllSubsequentCreates,74 genericWorkerForAllSubsequentCreates,75 genericWorkerForAllSubsequentCreates,76 ]);77 });78 it("should reject when a worker couldn't be created", async () => {79 setConcurrency(1);80 const expectedError = new Error('foo error');81 createWorkerStub.throws(expectedError);82 sut = createSut();83 await expect(captureWorkers(sut, 1)).rejectedWith(expectedError);84 });85 it('should share workers across subscribers (for sharing between dry runner and mutation test runner)', async () => {86 // Arrange87 setConcurrency(2);88 arrangeWorkers();89 sut = createSut();90 // Act91 const firstResult = await lastValueFrom(sut.schedule(range(0, 2), (worker) => worker).pipe(toArray()));92 const secondResult = await lastValueFrom(sut.schedule(range(0, 2), (worker) => worker).pipe(toArray()));93 // Assert94 await sut.dispose();95 expect(firstResult).lengthOf(2);96 expect(secondResult).lengthOf(2);97 expect(firstResult[0]).eq(secondResult[0]);98 expect(firstResult[1]).eq(secondResult[1]);99 });100 it('should re-emit the presented worker in the stream', async () => {101 // Arrange102 arrangeWorkers();103 setConcurrency(2);104 const actualScheduledWork: Array<[number, Required<Resource>]> = [];105 sut = createSut();106 const onGoingWork = lastValueFrom(107 sut108 .schedule(range(0, 3), async (worker, input) => {109 await tick();110 actualScheduledWork.push([input, worker]);111 })112 .pipe(toArray())113 );114 await tick(3);115 // Act116 await sut.dispose();117 // Assert118 expect(actualScheduledWork).lengthOf(3);119 expect(actualScheduledWork).deep.eq([120 [0, worker1],121 [1, worker2],122 [2, worker1],123 ]);124 await onGoingWork;125 });126 });127 describe('init', () => {128 it('should await the init() of all workers', async () => {129 // Arrange130 arrangeWorkers();131 setConcurrency(2);132 const initWorker2Task = new Task<void>();133 worker2.init.returns(initWorker2Task.promise);134 worker1.init.resolves();135 sut = createSut();136 // Act137 const timeoutResult = await ExpirableTask.timeout(sut.init(), 20);138 initWorker2Task.resolve();139 concurrencyToken$.complete();140 await sut.init();141 // Assert142 expect(timeoutResult).eq(ExpirableTask.TimeoutExpired);143 expect(worker1.init).called;144 expect(worker2.init).called;145 });146 it('should cache the workers for later use', async () => {147 // Arrange148 arrangeWorkers();149 setConcurrency(1);150 sut = createSut();151 concurrencyToken$.complete();152 // Act153 await sut.init();154 await sut.init();155 const allWorkers = await captureWorkers(sut, 1);156 // Assert157 expect(createWorkerStub).calledOnce;158 expect(allWorkers[0]).eq(worker1);159 });160 it('should await for all concurrency tokens to be delivered', async () => {161 // Arrange162 arrangeWorkers();163 setConcurrency(2);164 const actualWorkers: Array<Required<Resource>> = [];165 sut = createSut();166 const onGoingScheduledWork = lastValueFrom(sut.schedule(range(0, 2), (worker) => actualWorkers.push(worker)));167 // Act168 const timeoutResult = await ExpirableTask.timeout(sut.init(), 20);169 concurrencyToken$.complete();170 // Assert171 expect(timeoutResult).eq(ExpirableTask.TimeoutExpired);172 expect(actualWorkers).lengthOf(2);173 await sut.init();174 await onGoingScheduledWork;175 });176 it('should reject when a worker rejects', async () => {177 // Arrange178 arrangeWorkers();179 setConcurrency(2);180 const expectedError = new Error('expected error');181 worker2.init.rejects(expectedError);182 sut = createSut();183 // Act & Assert184 await expect(sut.init()).rejectedWith(expectedError);185 concurrencyToken$.complete();186 });187 });188 describe('dispose', () => {189 it('should have disposed all testRunners', async () => {190 setConcurrency(8);191 arrangeWorkers();192 sut = createSut();193 await captureWorkers(sut, 9);194 await sut.dispose();195 expect(worker1.dispose).called;196 expect(worker2.dispose).called;197 expect(genericWorkerForAllSubsequentCreates.dispose).called;198 });199 it('should not do anything if no workers were created', async () => {200 sut = createSut();201 await sut.dispose();202 expect(worker1.dispose).not.called;203 expect(worker2.dispose).not.called;204 });205 it('should not resolve when there are still workers being initialized (issue #713)', async () => {206 // Arrange207 arrangeWorkers();208 setConcurrency(2);209 const task = new Task<void>();210 const task2 = new Task<void>();211 worker1.init.returns(task.promise);212 worker2.init.returns(task2.promise);213 sut = createSut();214 // Act215 const resultPromise = lastValueFrom(sut.schedule(range(0, 2), (worker) => worker).pipe(toArray()));216 task.resolve();217 await sut.dispose();218 task2.resolve();219 concurrencyToken$.complete();220 await resultPromise;221 expect(worker1.dispose).called;222 expect(worker2.dispose).called;223 });224 it('should halt creating of new sandboxes', async () => {225 // Arrange226 arrangeWorkers();227 setConcurrency(3);228 const task = new Task<void>();229 const task2 = new Task<void>();230 worker1.init.returns(task.promise);231 worker2.init.returns(task2.promise);232 sut = createSut();233 // Act234 const actualTestRunnersPromise = lastValueFrom(sut.schedule(range(0, 3), (worker) => worker).pipe(toArray()));235 const disposePromise = sut.dispose();236 task.resolve();237 task2.resolve();238 await disposePromise;239 concurrencyToken$.complete();240 await actualTestRunnersPromise;241 // Assert242 expect(createWorkerStub).calledTwice;243 });244 });245 async function captureWorkers(suite: Pool<Required<Resource>>, inputCount: number) {246 // Eagerly get all test runners247 const createAllPromise = lastValueFrom(248 suite249 .schedule(range(0, inputCount), async (worker) => {250 await tick();251 return worker;252 })253 .pipe(toArray())254 );255 // But don't await yet, until after dispose.256 // Allow processes to be created257 await tick(inputCount);258 // Dispose completes the internal recycle bin subject, which in turn will complete.259 await suite.dispose();260 concurrencyToken$.complete();261 return createAllPromise;262 }...
Using AI Code Generation
1const initWorker3Task = require('stryker-parent').initWorker3Task;2initWorker3Task();3const initWorker3Task = require('stryker-parent').initWorker3Task;4initWorker3Task();5const initWorker3Task = require('stryker-parent').initWorker3Task;6initWorker3Task();7const initWorker3Task = require('stryker-parent').initWorker3Task;8initWorker3Task();9const initWorker3Task = require('stryker-parent').initWorker3Task;10initWorker3Task();11const initWorker3Task = require('stryker-parent').initWorker3Task;12initWorker3Task();13const initWorker3Task = require('stryker-parent').initWorker3Task;14initWorker3Task();15const initWorker3Task = require('stryker-parent').initWorker3Task;16initWorker3Task();17const initWorker3Task = require('stryker-parent').initWorker3Task;18initWorker3Task();19const initWorker3Task = require('stryker-parent').initWorker3Task;20initWorker3Task();21const initWorker3Task = require('stryker-parent').initWorker3Task;22initWorker3Task();23const initWorker3Task = require('stryker-parent').initWorker3Task;24initWorker3Task();
Using AI Code Generation
1var worker3 = require('stryker-parent').initWorker3Task;2worker3();3var worker3 = require('stryker-parent').initWorker3Task;4worker3();5var worker3 = require('stryker-parent').initWorker3Task;6worker3();7var worker3 = require('stryker-parent').initWorker3Task;8worker3();9var worker3 = require('stryker-parent').initWorker3Task;10worker3();11var worker3 = require('stryker-parent').initWorker3Task;12worker3();13var worker3 = require('stryker-parent').initWorker3Task;14worker3();15var worker3 = require('stryker-parent').initWorker3Task;16worker3();17var worker3 = require('stryker-parent').initWorker3Task;18worker3();19var worker3 = require('stryker-parent').initWorker3Task;20worker3();21var worker3 = require('stryker-parent').initWorker3Task;22worker3();23var worker3 = require('stryker-parent').initWorker3Task;24worker3();25var worker3 = require('stryker-parent').initWorker3Task;26worker3();
Using AI Code Generation
1const { initWorker3Task } = require('stryker-parent');2initWorker3Task();3const { initWorker3Task } = require('stryker-parent');4initWorker3Task();5const { initWorker3Task } = require('stryker-parent');6initWorker3Task();7const { initWorker3Task } = require('stryker-parent');8initWorker3Task();9const { initWorker3Task } = require('stryker-parent');10initWorker3Task();11const { initWorker3Task } = require('stryker-parent');12initWorker3Task();13const { initWorker3Task } = require('stryker-parent');14initWorker3Task();15const { initWorker3Task } = require('stryker-parent');16initWorker3Task();17const { initWorker3Task } = require('stryker-parent');18initWorker3Task();
Using AI Code Generation
1var strykerParent = require('stryker-parent');2var strykerWorker = require('stryker-worker');3var stryker = require('stryker');4var path = require('path');5var parent = new strykerParent.Parent(path.resolve(__dirname, 'stryker.conf.js'));6var worker = new strykerWorker.Worker(parent);7strykerParent.Parent.prototype.initWorker3Task = function (worker) {8 var self = this;9 var worker3 = new strykerWorker.Worker(self);10 self.initWorker(worker3);11 return worker3;12};13var worker3 = parent.initWorker3Task(worker);14strykerWorker.Worker.prototype.runTask = function (task) {15 var self = this;16 return self.runTask(task);17};18var task = new stryker.Task('test', 'test');19worker3.runTask(task).then(function () {20 console.log('done');21});22strykerWorker.Worker.prototype.runTask = function (task) {23 var self = this;24 return self.runTask(task);25};26var task = new stryker.Task('test', 'test');27worker3.runTask(task).then(function () {28 console.log('done');29});30strykerParent.Parent.prototype.initWorker3Task = function (worker) {31 var self = this;32 var worker3 = new strykerWorker.Worker(self);33 self.initWorker(worker3);34 return worker3;35};36var worker3 = parent.initWorker3Task(worker);37strykerWorker.Worker.prototype.runTask = function (task) {38 var self = this;39 return self.runTask(task);40};41var task = new stryker.Task('test', 'test');42worker3.runTask(task).then(function () {43 console.log('done');44});45strykerWorker.Worker.prototype.runTask = function (task) {46 var self = this;47 return self.runTask(task);48};49var task = new stryker.Task('test', 'test');50worker3.runTask(task).then(function ()
Using AI Code Generation
1var strykerParent = require('stryker-parent');2var stryker = strykerParent.initWorker3Task();3 at process (C:\Users\johan\Documents\GitHub\stryker\packages\stryker-mocha-runner\node_modules\resolve\lib\async.js:173:43)4 at ondir (C:\Users\johan\Documents\GitHub\stryker\packages\stryker-mocha-runner\node_modules\resolve\lib\async.js:188:17)5 at load (C:\Users\johan\Documents\GitHub\stryker\packages\stryker-mocha-runner\node_modules\resolve\lib\async.js:69:43)6 at onex (C:\Users\johan\Documents\GitHub\stryker\packages\stryker-mocha-runner\node_modules\resolve\lib\async.js:92:31)7 at FSReqWrap.oncomplete (fs.js:82:15)
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!!