Best JavaScript code snippet using stryker-parent
rate_limit.test.ts
Source:rate_limit.test.ts
1import * as assert from 'assert';2import {rateLimitCtor, rateLimitEmitLastCtor} from './rate_limit';3import {resolvedUndefined} from './resolved';4describe('rateLimitCtor', () => {5 const setup = () => {6 const getTimeStub = () => getTimeStub.returnValue;7 getTimeStub.returnValue = 1000;8 const setTimeoutStub = (callback, delay) => {9 setTimeoutStub.callback = callback;10 setTimeoutStub.delay = delay;11 setTimeoutStub.callCount++;12 return setTimeoutStub.handle++;13 }14 setTimeoutStub.callback = () => undefined;15 setTimeoutStub.delay = null;16 setTimeoutStub.callCount = 0;17 setTimeoutStub.handle = 1;18 // rateLimiterCtor returns a rate limiter function.19 const rateLimit = rateLimitCtor(getTimeStub, setTimeoutStub);20 // The rate limiter gives us a rate-limited wrapper around the the function we pass it.21 const fStub = () => { fStub.callCount += 1 };22 fStub.callCount = 023 let rateLimitedF = rateLimit(100, fStub);24 return {getTimeStub, setTimeoutStub, fStub, rateLimitedF};25 };26 it('Happy path', async () => {27 const {getTimeStub, setTimeoutStub, fStub, rateLimitedF} = setup();28 // If we call the rate-limited wrapper once, the original function is called right away29 rateLimitedF();30 assert.strictEqual(setTimeoutStub.callCount, 0);31 assert.strictEqual(fStub.callCount, 1);32 // If we make a second call before the rate is up, the call will be scheduled for later33 getTimeStub.returnValue = 1010;34 rateLimitedF();35 assert.strictEqual(setTimeoutStub.callCount, 1);36 assert.strictEqual(setTimeoutStub.delay, 90);37 assert.strictEqual(fStub.callCount, 1);38 // If we call the rate-limited wrapper a third time before the rate is up, the call will be dropped39 // (oldest of the rate-limited calls wins)40 getTimeStub.returnValue = 1020;41 rateLimitedF();42 assert.strictEqual(setTimeoutStub.callCount, 1);43 assert.strictEqual(fStub.callCount, 1);44 // 100 ms from the first deferred call, the timed-out callback runs45 getTimeStub.returnValue = 1110;46 setTimeoutStub.callback();47 assert.strictEqual(fStub.callCount, 2);48 // After a bit more time has elapsed we should again be able to get a call through,49 // this is essentially identical to the first call we made50 getTimeStub.returnValue = 2000;51 rateLimitedF();52 assert.strictEqual(fStub.callCount, 3);53 assert.strictEqual(setTimeoutStub.callCount, 1);54 });55});56describe('rateLimitEmitLastCtor', () => {57 const setup = () => {58 const getTimeStub = () => getTimeStub.returnValue;59 getTimeStub.returnValue = 1000;60 const setTimeoutStub = (callback, delay) => {61 setTimeoutStub.callback = callback;62 setTimeoutStub.delay = delay;63 setTimeoutStub.callCount++;64 return setTimeoutStub.handle++;65 }66 setTimeoutStub.callback = () => undefined;67 setTimeoutStub.delay = null;68 setTimeoutStub.callCount = 0;69 setTimeoutStub.handle = 1;70 // rateLimitEmitLastCtor returns a rate limiter function71 const rateLimitEmitLast = rateLimitEmitLastCtor(getTimeStub, setTimeoutStub);72 // The rate limiter gives us a rate-limited wrapper around the the function we pass it.73 const delayBetweenCallsMsMock = 10074 const fStub = (arg) => {75 fStub.lastCalledWith = arg;76 fStub.callCount += 1;77 return resolvedUndefined;78 };79 fStub.lastCalledWith = null;80 fStub.callCount = 0;81 const callbackStub:()=>void = () => {};82 let rateLimitedF = rateLimitEmitLast(delayBetweenCallsMsMock, fStub, callbackStub)83 return {getTimeStub, setTimeoutStub, fStub, rateLimitedF};84 };85 it('Happy path', async () => {86 const {getTimeStub, setTimeoutStub, fStub, rateLimitedF} = setup();87 // If we call the rate-limited wrapper once, the original function is called right away88 await rateLimitedF('first call');89 assert.strictEqual(setTimeoutStub.callCount, 0);90 assert.strictEqual(fStub.callCount, 1);91 assert.strictEqual(fStub.lastCalledWith, 'first call')92 // If we call the rate-limited wrapper again before the rate is up, the call is deferred93 getTimeStub.returnValue = 1010;94 await rateLimitedF('second call');95 assert.strictEqual(setTimeoutStub.callCount, 1);96 assert.strictEqual(setTimeoutStub.delay, 90);97 assert.strictEqual(fStub.callCount, 1);98 99 // If we call the rate-limited wrapper a third time before the rate is up, the deferred call should be set to use this argument instead100 getTimeStub.returnValue = 1020;101 await rateLimitedF('third call');102 assert.strictEqual(setTimeoutStub.callCount, 1);103 assert.strictEqual(fStub.callCount, 1);104 // 100ms from the first deferred call, the timed-out callback runs with the latest arguments105 getTimeStub.returnValue = 1110;106 await setTimeoutStub.callback();107 assert.strictEqual(fStub.callCount, 2);108 assert.strictEqual(fStub.lastCalledWith, 'third call');109 // After a bit more time has elapsed we should again be able to get a call through,110 // this is essentially identical to the first call we made111 getTimeStub.returnValue = 2000;112 await rateLimitedF('fourth call');113 assert.strictEqual(fStub.callCount, 3);114 assert.strictEqual(setTimeoutStub.callCount, 1);115 });116 it('Edge case - lastParams was updated while we waited on f to complete', async () => {117 const {getTimeStub, setTimeoutStub, fStub, rateLimitedF} = setup();118 119 await rateLimitedF('first call');120 getTimeStub.returnValue = 1010;121 await rateLimitedF('second call'); // will be deferred, just like the above test122 assert.strictEqual(setTimeoutStub.callCount, 1); // that's normal123 assert.strictEqual(setTimeoutStub.delay, 90);124 getTimeStub.returnValue = 2000; // well past the rate limit125 setTimeoutStub.callback(); // simulating a long-running callback to set up the problemastic race condition126 await rateLimitedF('third call'); // call while waiting for the previous call's callback127 // since we've past the rate limit, we would normally expect 'third call' to go straight through,128 // but in this case setTimeout has been called again because of the long-running callback129 assert.strictEqual(setTimeoutStub.callCount, 2);130 });...
scheduler.spec.js
Source:scheduler.spec.js
1import { describe, it, beforeEach, afterEach } from 'mocha';2import { expect } from 'chai';3import { spy, stub } from 'sinon';4import moment from 'moment';5import { scheduleAfter, scheduleAt, __Rewire__, __ResetDependency__, stopSchedule } from '../lib/scheduler.js';6import { DEFAULT_WAKEUP_TIMEOUT_MS } from '../lib/constants.js';7const func = /** @type {function} */ spy();8const setTimeoutStub = stub();9setTimeoutStub.returns(1);10const clearTimeoutSpy = spy();11describe('scheduler spec', () => {12 beforeEach(() => {13 __Rewire__(setTimeoutStub, clearTimeoutSpy);14 });15 afterEach(() => {16 stopSchedule();17 __ResetDependency__();18 setTimeoutStub.resetHistory();19 clearTimeoutSpy.resetHistory();20 });21 describe(`${scheduleAt.name} spec`, () => {22 it('stops previous timeout before making a new one', () => {23 const currentTime = new Date();24 scheduleAt({ func, currentTime });25 expect(setTimeoutStub.callCount).to.be.eq(1);26 expect(setTimeoutStub.args[0][1]).to.be.eq(DEFAULT_WAKEUP_TIMEOUT_MS);27 expect(clearTimeoutSpy.callCount).to.be.eq(0);28 scheduleAt({ func, currentTime });29 expect(setTimeoutStub.callCount).to.be.eq(2);30 expect(setTimeoutStub.args[1][1]).to.be.eq(DEFAULT_WAKEUP_TIMEOUT_MS);31 expect(clearTimeoutSpy.callCount).to.be.eq(1);32 expect(clearTimeoutSpy.args[0][0]).to.be.eq(setTimeoutStub.returnValues[0]);33 });34 it('should schedule func at given date', () => {35 const currentTime = new Date();36 const date = moment(currentTime).add(1, 'minute').toDate();37 scheduleAt({ func, currentTime, date });38 expect(setTimeoutStub.callCount).to.be.eq(1);39 expect(setTimeoutStub.args).to.be.ofSize(1);40 expect(setTimeoutStub.args[0]).to.be.ofSize(2);41 expect(setTimeoutStub.args[0][1]).to.be.eq(date.getTime() - currentTime.getTime());42 });43 it('defaults to predefined timeout when given date is not valid', () => {44 const currentTime = new Date();45 const date = moment(currentTime).subtract(1, 'second').toDate();46 scheduleAt({ func, currentTime, date });47 expect(setTimeoutStub.callCount).to.be.eq(1);48 expect(setTimeoutStub.args).to.be.ofSize(1);49 expect(setTimeoutStub.args[0]).to.be.ofSize(2);50 expect(setTimeoutStub.args[0][1]).to.be.eq(DEFAULT_WAKEUP_TIMEOUT_MS);51 });52 it('defaults to predefined timeout when date is not given', () => {53 const currentTime = new Date();54 scheduleAt({ func, currentTime });55 expect(setTimeoutStub.callCount).to.be.eq(1);56 expect(setTimeoutStub.args).to.be.ofSize(1);57 expect(setTimeoutStub.args[0]).to.be.ofSize(2);58 expect(setTimeoutStub.args[0][1]).to.be.eq(DEFAULT_WAKEUP_TIMEOUT_MS);59 });60 });61 describe(`${scheduleAfter.name} spec`, () => {62 it('stops previous timeout before making a new one', () => {63 const currentTime = new Date();64 scheduleAfter(func, currentTime);65 expect(setTimeoutStub.callCount).to.be.eq(1);66 expect(setTimeoutStub.args[0][1]).to.be.eq(DEFAULT_WAKEUP_TIMEOUT_MS);67 expect(clearTimeoutSpy.callCount).to.be.eq(0);68 scheduleAfter(func, currentTime);69 expect(setTimeoutStub.callCount).to.be.eq(2);70 expect(setTimeoutStub.args[1][1]).to.be.eq(DEFAULT_WAKEUP_TIMEOUT_MS);71 expect(clearTimeoutSpy.callCount).to.be.eq(1);72 expect(clearTimeoutSpy.args[0][0]).to.be.eq(setTimeoutStub.returnValues[0]);73 });74 it('should schedule func at given timeout', () => {75 const currentTime = new Date();76 const timeout = 100;77 scheduleAfter(func, currentTime, timeout);78 expect(setTimeoutStub.callCount).to.be.eq(1);79 expect(setTimeoutStub.args).to.be.ofSize(1);80 expect(setTimeoutStub.args[0]).to.be.ofSize(2);81 expect(setTimeoutStub.args[0][1]).to.be.eq(timeout);82 });83 it('defaults to predefined timeout when given timeout is not valid', () => {84 const currentTime = new Date();85 scheduleAfter(func, currentTime, 0);86 expect(setTimeoutStub.callCount).to.be.eq(1);87 expect(setTimeoutStub.args).to.be.ofSize(1);88 expect(setTimeoutStub.args[0]).to.be.ofSize(2);89 expect(setTimeoutStub.args[0][1]).to.be.eq(DEFAULT_WAKEUP_TIMEOUT_MS);90 scheduleAfter(func, currentTime, 86400001);91 expect(setTimeoutStub.callCount).to.be.eq(2);92 expect(setTimeoutStub.args).to.be.ofSize(2);93 expect(setTimeoutStub.args[0]).to.be.ofSize(2);94 expect(setTimeoutStub.args[1][1]).to.be.eq(DEFAULT_WAKEUP_TIMEOUT_MS);95 });96 it('defaults to predefined timeout when timeout is not given', () => {97 const currentTime = new Date();98 scheduleAfter(func, currentTime);99 expect(setTimeoutStub.callCount).to.be.eq(1);100 expect(setTimeoutStub.args).to.be.ofSize(1);101 expect(setTimeoutStub.args[0]).to.be.ofSize(2);102 expect(setTimeoutStub.args[0][1]).to.be.eq(DEFAULT_WAKEUP_TIMEOUT_MS);103 });104 });...
wait.js
Source:wait.js
1'use strict';2const {test, stub} = require('supertape');3const wait = require('..');4test('wait: setTimeout', async (t) => {5 const {setTimeout} = global;6 const setTimeoutStub = stub((f) => f());7 8 global.setTimeout = setTimeoutStub;9 10 const fn = stub();11 await wait(fn);12 13 global.setTimeout = setTimeout;14 15 t.calledWith(setTimeoutStub, [fn, 0]);16 t.end();17});18test('wait: setTimeout: number', async (t) => {19 const {setTimeout} = global;20 const setTimeoutStub = stub((f) => f());21 22 global.setTimeout = setTimeoutStub;23 24 await wait(1);25 26 global.setTimeout = setTimeout;27 28 t.calledWith(setTimeoutStub, [stub(), 1]);29 t.end();30});31test('wait: setTimeout: timeout', async (t) => {32 const {setTimeout} = global;33 const setTimeoutStub = stub((f) => f());34 35 global.setTimeout = setTimeoutStub;36 37 const fn = stub();38 await wait(1, fn);39 40 global.setTimeout = setTimeout;41 42 t.calledWith(setTimeoutStub, [fn, 1]);43 t.end();44});45test('wait: setTimeout: args', async (t) => {46 const fn = stub();47 await wait(1, fn, 'hello');48 49 t.calledWith(fn, ['hello']);50 t.end();51});52test('wait: setTimeout: args: no timeout', async (t) => {53 const fn = stub();54 await wait(fn, 'hello');55 56 t.calledWith(fn, ['hello']);57 t.end();...
Using AI Code Generation
1const setTimeoutStub = require('stryker-parent').setTimeoutStub;2setTimeoutStub(() => {3 console.log('hello world');4}, 1000);5const setTimeoutStub = require('stryker-parent').setTimeoutStub;6setTimeoutStub(() => {7 console.log('hello world');8}, 1000);9const setTimeoutStub = require('stryker-parent').setTimeoutStub;10setTimeoutStub(() => {11 console.log('hello world');12}, 1000);13const setTimeoutStub = require('stryker-parent').setTimeoutStub;14setTimeoutStub(() => {15 console.log('hello world');16}, 1000);17const setTimeoutStub = require('stryker-parent').setTimeoutStub;18setTimeoutStub(() => {19 console.log('hello world');20}, 1000);21const setTimeoutStub = require('stryker-parent').setTimeoutStub;22setTimeoutStub(() => {23 console.log('hello world');24}, 1000);25const setTimeoutStub = require('stryker-parent').setTimeoutStub;26setTimeoutStub(() => {27 console.log('hello world');28}, 1000);29const setTimeoutStub = require('stryker-parent').setTimeoutStub;30setTimeoutStub(() => {31 console.log('hello world');32}, 1000);33const setTimeoutStub = require('stryker-parent').setTimeoutStub;34setTimeoutStub(() => {35 console.log('hello world');36}, 1000);37const setTimeoutStub = require('stryker-parent').setTimeoutStub;38setTimeoutStub(() => {39 console.log('hello world');40}, 1000);41const setTimeoutStub = require('stryker-parent
Using AI Code Generation
1var setTimeoutStub = require('stryker-parent').setTimeoutStub;2setTimeoutStub(function() {3 console.log('Hello World');4}, 1000);5var setTimeoutStub = require('stryker-parent').setTimeoutStub;6setTimeoutStub(function() {7 console.log('Hello World');8}, 1000);
Using AI Code Generation
1const setTimeoutStub = require('stryker-parent').setTimeoutStub;2setTimeoutStub(() => {3 console.log('Hello World!');4}, 5000);5const setTimeoutStub = require('stryker-parent').setTimeoutStub;6setTimeoutStub(() => {7 console.log('Hello World!');8}, 5000);9const setTimeoutStub = require('stryker-parent').setTimeoutStub;10setTimeoutStub(() => {11 console.log('Hello World!');12}, 5000);13const setTimeoutStub = require('stryker-parent').setTimeoutStub;14setTimeoutStub(() => {15 console.log('Hello World!');16}, 5000);17const setTimeoutStub = require('stryker-parent').setTimeoutStub;18setTimeoutStub(() => {19 console.log('Hello World!');20}, 5000);21const setTimeoutStub = require('stryker-parent').setTimeoutStub;22setTimeoutStub(() => {23 console.log('Hello World!');24}, 5000);25const setTimeoutStub = require('stryker-parent').setTimeoutStub;26setTimeoutStub(() => {27 console.log('Hello World!');28}, 5000);29const setTimeoutStub = require('stryker-parent').setTimeoutStub;30setTimeoutStub(() => {31 console.log('Hello World!');32}, 5000);33const setTimeoutStub = require('stryker-parent').setTimeoutStub;34setTimeoutStub(() => {35 console.log('Hello World!');36}, 5000);37const setTimeoutStub = require('stryker-parent').setTimeoutStub;38setTimeoutStub(() => {39 console.log('Hello World!');40}, 5000);41const setTimeoutStub = require('stryker-parent
Using AI Code Generation
1const { setTimeoutStub } = require('stryker-parent');2setTimeoutStub(() => {3 console.log('Hello world');4}, 1000);5const { setTimeoutStub } = require('stryker-parent');6describe('test', () => {7 it('should log hello world', () => {8 setTimeoutStub(() => {9 console.log('Hello world');10 }, 1000);11 });12});13module.exports = {14};
Using AI Code Generation
1var setTimeoutStub = require('stryker-parent').setTimeoutStub;2setTimeoutStub(function () {3 console.log('Hello world!');4}, 1000);5module.exports = {6 setTimeoutStub: function (callback, delay) {7 setTimeout(callback, delay);8 }9};10{11}12var setTimeoutStub = require('stryker-parent').setTimeoutStub;13setTimeoutStub(function () {14 console.log('Hello world!');15}, 1000);16module.exports = {17 setTimeoutStub: function (callback, delay) {18 setTimeout(callback, delay);19 }20};21{22}23var setTimeoutStub = require('stryker-parent').setTimeoutStub;24setTimeoutStub(function () {25 console.log('Hello world!');26}, 1000);27module.exports = {28 setTimeoutStub: function (callback, delay) {29 setTimeout(callback, delay);30 }31};32{33}34var setTimeoutStub = require('stryker-parent').setTimeoutStub;35setTimeoutStub(function () {36 console.log('Hello world!');37}, 1000);38module.exports = {39 setTimeoutStub: function (callback, delay) {40 setTimeout(callback, delay);41 }42};43{44}45var setTimeoutStub = require('stry
Using AI Code Generation
1setTimeoutStub = require('stryker-parent').setTimeoutStub;2setTimeoutStub(function() {3 console.log('Hello world');4}, 5000);5var parent = require('stryker-parent');6setTimeoutStub = parent.setTimeoutStub;7setTimeoutStub(function() {8 console.log('Hello world');9}, 5000);10var parent = require('stryker-parent');11setTimeoutStub = parent.setTimeoutStub;12setTimeoutStub(function() {13 console.log('Hello world');14}, 5000);15var parent = require('stryker-parent');16setTimeoutStub = parent.setTimeoutStub;17setTimeoutStub(function() {18 console.log('Hello world');19}, 5000);20var parent = require('stryker-parent');21setTimeoutStub = parent.setTimeoutStub;22setTimeoutStub(function() {23 console.log('Hello world');24}, 5000);25var parent = require('stryker-parent');26setTimeoutStub = parent.setTimeoutStub;27setTimeoutStub(function() {28 console.log('Hello world');29}, 5000);30var parent = require('stryker-parent');31setTimeoutStub = parent.setTimeoutStub;32setTimeoutStub(function() {33 console.log('Hello world');34}, 5000);35var parent = require('stryker-parent');36setTimeoutStub = parent.setTimeoutStub;37setTimeoutStub(function() {38 console.log('Hello world');39}, 5000);40var parent = require('stryker-parent');41setTimeoutStub = parent.setTimeoutStub;42setTimeoutStub(function() {43 console.log('Hello world');44}, 5000);45var parent = require('stryker-parent');46setTimeoutStub = parent.setTimeoutStub;47setTimeoutStub(function() {48 console.log('Hello world');49}, 5000);
Using AI Code Generation
1const setTimeoutStub = require('stryker-parent').setTimeoutStub;2const setTimeout = setTimeoutStub();3setTimeout(function() {4 console.log('hello');5}, 1000);6const setTimeoutStub = require('stryker').setTimeoutStub;7const setTimeout = setTimeoutStub();8setTimeout(function() {9 console.log('hello');10}, 1000);11const setTimeoutStub = require('stryker-api').setTimeoutStub;12const setTimeout = setTimeoutStub();13setTimeout(function() {14 console.log('hello');15}, 1000);16const setTimeoutStub = require('stryker-parent').setTimeoutStub;17const setTimeout = setTimeoutStub();18setTimeout(function() {19 console.log('hello');20}, 1000);21const setTimeoutStub = require('stryker').setTimeoutStub;22const setTimeout = setTimeoutStub();23setTimeout(function() {24 console.log('hello');25}, 1000);26const setTimeoutStub = require('stryker-api').setTimeoutStub;27const setTimeout = setTimeoutStub();28setTimeout(function() {29 console.log('hello');30}, 1000);31const setTimeoutStub = require('stryker-parent').setTimeoutStub;32const setTimeout = setTimeoutStub();33setTimeout(function() {34 console.log('hello');35}, 1000);36const setTimeoutStub = require('stryker').setTimeoutStub;37const setTimeout = setTimeoutStub();38setTimeout(function() {39 console.log('hello');40}, 1000);41const setTimeoutStub = require('stryker-api').setTimeoutStub;42const setTimeout = setTimeoutStub();43setTimeout(function() {44 console.log('hello');45}, 1000);46const setTimeoutStub = require('stryker-parent').setTimeoutStub;47const setTimeout = setTimeoutStub();48setTimeout(function() {49 console.log('hello');50}, 1000);51const setTimeoutStub = require('stryker').setTimeoutStub;52const setTimeout = setTimeoutStub();
Using AI Code Generation
1const myModule = require('stryker-parent');2myModule.setTimeoutStub(1000);3const stryker = require('stryker');4module.exports.setTimeoutStub = stryker.setTimeoutStub;5const sinon = require('sinon');6exports.setTimeoutStub = (time) => {7 const clock = sinon.useFakeTimers();8 clock.tick(time);9};
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!!