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 { initWorker2Task } = require('stryker-parent');2initWorker2Task();3const { initWorker2Task } = require('stryker-parent');4initWorker2Task();5const { initWorker2Task } = require('stryker-parent');6initWorker2Task();7const { initWorker2Task } = require('stryker-parent');8initWorker2Task();9const { initWorker2Task } = require('stryker-parent');10initWorker2Task();11const { initWorker2Task } = require('stryker-parent');12initWorker2Task();13const { initWorker2Task } = require('stryker-parent');14initWorker2Task();15const { initWorker2Task } = require('stryker-parent');16initWorker2Task();17const { initWorker2Task } = require('stryker-parent');18initWorker2Task();19const { initWorker2Task } = require('stryker-parent');20initWorker2Task();21const { initWorker2Task } = require('stryker-parent');22initWorker2Task();23const { initWorker2Task } = require('stryker-parent');24initWorker2Task();25const { initWorker2Task } = require('stryker-parent');26initWorker2Task();27const { initWorker2Task } = require('stryker-parent');28initWorker2Task();29const { initWorker2Task } = require('stryker-parent');30initWorker2Task();
Using AI Code Generation
1const { initWorker2Task } = require('stryker-parent');2const worker = initWorker2Task();3const { initMaster2Task } = require('stryker-parent');4const master = initMaster2Task();5const { initWorker2Task } = require('stryker-parent');6const worker = initWorker2Task();7const { initMaster2Task } = require('stryker-parent');8const master = initMaster2Task();9const { initWorker2Task } = require('stryker-parent');10const worker = initWorker2Task();11const { initMaster2Task } = require('stryker-parent');12const master = initMaster2Task();13const { initWorker2Task } = require('stryker-parent');14const worker = initWorker2Task();15const { initMaster2Task } = require('stryker-parent');16const master = initMaster2Task();17const { initWorker2Task } = require('stryker-parent');18const worker = initWorker2Task();19const { initMaster2Task } = require('stryker-parent');20const master = initMaster2Task();21const { initWorker2Task } = require('stryker-parent');22const worker = initWorker2Task();23const { initMaster2Task } = require('stryker-parent');24const master = initMaster2Task();25const { initWorker2Task } = require('stryker-parent');
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!!