How to use TimeoutDecorator method in stryker-parent

Best JavaScript code snippet using stryker-parent

k8s.js

Source:k8s.js Github

copy

Full Screen

1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3const k8s = require('@kubernetes/client-node');4const logger = require('./logger');5const config = require('./config');6const { timeoutDecorator } = require('./util');7const kc = new k8s.KubeConfig();8if (config.rbacEnabled) {9 // If RBAC is enabled, we can use kc.loadFromDefault() to load k8s config in containers.10 // For local debugging purpose, one can set CUSTOM_K8S_API_SERVER_URL, CUSTOM_K8S_CA_FILE and CUSTOM_K8S_TOKEN_FILE,11 // to connect to RBAC k8s cluster. Ca and Token file should be from a valid service account.12 if (config.customK8sApiServerURL) {13 const cluster = {14 name: 'inCluster',15 caFile: config.customK8sCaFile,16 server: config.customK8sApiServerURL,17 skipTLSVerify: false,18 };19 const user = {20 name: 'inClusterUser',21 authProvider: {22 name: 'tokenFile',23 config: {24 tokenFile: config.customK8sTokenFile,25 },26 },27 };28 kc.loadFromClusterAndUser(cluster, user);29 } else {30 kc.loadFromDefault();31 }32} else {33 // If RBAC is not enabled, use CUSTOM_K8S_API_SERVER_URL to connect to API server.34 const cluster = { name: 'cluster', server: config.customK8sApiServerURL };35 const user = { name: 'user' };36 kc.loadFromClusterAndUser(cluster, user);37}38// If API server reports a non-200 status code, the client will39// throw an error. In such case, error.name will be "HttpError",40// and error.response.statusCode is the actual status code.41// If network is disconnected, the error will be a different one,42// and you cannot get error.name and error.statusCode.43const customObjectsClient = kc.makeApiClient(k8s.CustomObjectsApi);44async function getFramework(name, namespace = 'default') {45 const res = await customObjectsClient.getNamespacedCustomObject(46 'frameworkcontroller.microsoft.com',47 'v1',48 namespace,49 'frameworks',50 name,51 );52 return res.response;53}54async function listFramework(name, namespace = 'default') {55 const res = await customObjectsClient.listNamespacedCustomObject(56 'frameworkcontroller.microsoft.com',57 'v1',58 namespace,59 'frameworks',60 );61 return res.response;62}63async function createFramework(frameworkDescription, namespace = 'default') {64 const res = await customObjectsClient.createNamespacedCustomObject(65 'frameworkcontroller.microsoft.com',66 'v1',67 namespace,68 'frameworks',69 frameworkDescription,70 );71 return res.response;72}73async function patchFramework(name, data, namespace = 'default') {74 if (data.status) {75 logger.warn(76 'Modifying status field in framework is not allowed! Will delete it.',77 );78 delete data.status;79 }80 const res = await customObjectsClient.patchNamespacedCustomObject(81 'frameworkcontroller.microsoft.com',82 'v1',83 namespace,84 'frameworks',85 name,86 data,87 { headers: { 'Content-Type': 'application/merge-patch+json' } },88 );89 return res.response;90}91async function deleteFramework(name, namespace = 'default') {92 const res = await customObjectsClient.deleteNamespacedCustomObject(93 'frameworkcontroller.microsoft.com',94 'v1',95 namespace,96 'frameworks',97 name,98 ...Array(4), // skip some parameters99 { headers: { propagationPolicy: 'Foreground' } },100 );101 return res.response;102}103function getFrameworkInformer(104 timeoutSeconds = 365 * 86400,105 namespace = 'default',106) {107 /*108 Usage:109 const informer = getFrameworkInformer()110 informer.on('add', (obj) => { console.log(`Added: ${obj.metadata.name}`); });111 informer.on('update', (obj) => { console.log(`Updated: ${obj.metadata.name}`); });112 informer.on('delete', (obj) => { console.log(`Deleted: ${obj.metadata.name}`); });113 informer.on('error', (err) => { console.error(err);});114 informer.start();115 If the informer disconnects normally from API server, it will re-connect automatically.116 But during the reconnection, listFn will be called again, which is inefficient.117 According to https://github.com/kubernetes-client/javascript/blob/932c2fbc34db954c6ed397b3cd9ead08b2ff1d10/src/cache.ts#L82-L85,118 this behavior will be fixed in the future. For this version, we set a large timeout to mitigate this issue.119 TO DO: If @kubernetes/client-node fixes this issue, we should upgrade our code to use the new code.120 If the informer encounters any error, it will stop watching, and won't re-connect.121 One can restart it by informer.on('error', (err) => { informer.start(); }).122 */123 const listFn = () => {124 logger.info('Frameworks are listed.');125 return customObjectsClient.listNamespacedCustomObject(126 'frameworkcontroller.microsoft.com',127 'v1',128 namespace,129 'frameworks',130 );131 };132 const informer = k8s.makeInformer(133 kc,134 `/apis/frameworkcontroller.microsoft.com/v1/namespaces/${namespace}/frameworks?timeoutSeconds=${timeoutSeconds}`,135 listFn,136 );137 return informer;138}139const coreV1Client = kc.makeApiClient(k8s.CoreV1Api);140function getEventInformer(timeoutSeconds = 365 * 86400, namespace = 'default') {141 /*142 The usage is very like `getFrameworkInformer`. Please see the comments of `getFrameworkInformer` for reference.143 */144 const listFn = () => {145 logger.info('Cluster events are listed.');146 return coreV1Client.listNamespacedEvent(namespace);147 };148 const informer = k8s.makeInformer(149 kc,150 `/api/v1/namespaces/${namespace}/events?timeoutSeconds=${timeoutSeconds}`,151 listFn,152 );153 return informer;154}155const priorityClassClient = kc.makeApiClient(k8s.SchedulingV1Api);156async function createPriorityClass(priorityClassDef) {157 const res = await priorityClassClient.createPriorityClass(priorityClassDef);158 return res.response;159}160async function deletePriorityClass(name) {161 const res = await priorityClassClient.deletePriorityClass(name);162 return res.response;163}164async function createSecret(secretDef) {165 const res = await coreV1Client.createNamespacedSecret(166 secretDef.metadata.namespace,167 secretDef,168 );169 return res;170}171async function deleteSecret(name, namespace = 'default') {172 const res = await coreV1Client.deleteNamespacedSecret(name, namespace);173 return res.response;174}175async function patchSecretOwnerToFramework(secret, frameworkResponse) {176 const metadata = {177 ownerReferences: [178 {179 apiVersion: 'frameworkcontroller.microsoft.com',180 kind: 'Framework',181 name: frameworkResponse.metadata.name,182 uid: frameworkResponse.metadata.uid,183 controller: true,184 blockOwnerDeletion: true,185 },186 ],187 };188 const res = await coreV1Client.patchNamespacedSecret(189 secret.metadata.name,190 secret.metadata.namespace,191 { metadata: metadata },192 ...Array(4), // skip some parameters193 { headers: { 'Content-Type': 'application/merge-patch+json' } },194 );195 return res.response;196}197const timeoutMs = config.k8sConnectionTimeoutSecond * 1000;198// We give every method a timeout.199module.exports = {200 getFramework: timeoutDecorator(201 getFramework,202 'Kubernetes getFramework',203 timeoutMs,204 ),205 listFramework: timeoutDecorator(206 listFramework,207 'Kubernetes getFramework',208 timeoutMs,209 ),210 createFramework: timeoutDecorator(211 createFramework,212 'Kubernetes createFramework',213 timeoutMs,214 ),215 patchFramework: timeoutDecorator(216 patchFramework,217 'Kubernetes patchFramework',218 timeoutMs,219 ),220 deleteFramework: timeoutDecorator(221 deleteFramework,222 'Kubernetes deleteFramework',223 timeoutMs,224 ),225 createPriorityClass: timeoutDecorator(226 createPriorityClass,227 'Kubernetes createPriorityClass',228 timeoutMs,229 ),230 deletePriorityClass: timeoutDecorator(231 deletePriorityClass,232 'Kubernetes deletePriorityClass',233 timeoutMs,234 ),235 createSecret: timeoutDecorator(236 createSecret,237 'Kubernetes createSecret',238 timeoutMs,239 ),240 deleteSecret: timeoutDecorator(241 deleteSecret,242 'Kubernetes deleteSecret',243 timeoutMs,244 ),245 patchSecretOwnerToFramework: timeoutDecorator(246 patchSecretOwnerToFramework,247 'Kubernetes patchSecretOwnerToFramework',248 timeoutMs,249 ),250 getFrameworkInformer: getFrameworkInformer,251 getEventInformer: getEventInformer,...

Full Screen

Full Screen

time.test.js

Source:time.test.js Github

copy

Full Screen

1const time = require("../lib/time");2const expect = require("chai").expect;3function print() {4 return new Promise((resolve, reject) => {5 setTimeout(() => {6 resolve(true);7 }, 10);8 });9}10describe("time.test.js", () => {11 it("function timeoutDecorator and expect not timeout", async () => {12 let printNoTimeout = time.timeoutDecorator(print, 11);13 expect(await printNoTimeout()).to.be.true;14 });15 it("function timeoutDecorator and expect timeout", async () => {16 let printWithTimeout = time.timeoutDecorator(print, 5);17 // expect(printWithTimeout).to.throw();18 try {19 await printWithTimeout();20 } catch (error) {21 expect(error).to.be.an("error");22 }23 });24 it("test delay(sleep) function without default value", async () => {25 let start = Date.now();26 await time.delay(10);27 expect(Date.now() - start).to.lt(20).and.gte(8);28 });...

Full Screen

Full Screen

timeout.spec.ts

Source:timeout.spec.ts Github

copy

Full Screen

...10 @TestCase(1)11 @TestCase(2)12 @TestCase(42)13 public testTimeoutMetaDataAdded(timeout: number) {14 const timeoutDecorator = TimeoutDecorator(timeout);15 const testFixture = {};16 timeoutDecorator(testFixture, "test", undefined);17 Expect(18 Reflect.getMetadata(METADATA_KEYS.TIMEOUT, testFixture, "test")19 ).toBe(timeout);20 }21 @TestCase(0)22 @TestCase(-1)23 @TestCase(-42)24 public incorrectTestTimeoutThrowsError(timeout: number) {25 Expect(() => TimeoutDecorator(timeout)).toThrowError(26 RangeError,27 "Timeout period must be greater than 0."28 );29 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');2const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');3const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');4const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');5const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');6const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');7const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');8const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');9const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');10const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');11const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');12const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');13const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');14const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');15const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');16const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');17const TimeoutDecorator = require('stryker-parent/TimeoutDecorator');18const TimeoutDecorator = require('stryker-parent

Full Screen

Using AI Code Generation

copy

Full Screen

1require('stryker').TimeoutDecorator.configure(1000);2require('stryker').TimeoutDecorator.configure(1000);3require('stryker').TimeoutDecorator.configure(1000);4require('stryker').TimeoutDecorator.configure(1000);5require('stryker').TimeoutDecorator.configure(1000);6require('stryker').TimeoutDecorator.configure(1000);7require('stryker').TimeoutDecorator.configure(1000);8require('stryker').TimeoutDecorator.configure(1000);9require('stryker').TimeoutDecorator.configure(1000);

Full Screen

Using AI Code Generation

copy

Full Screen

1const { TimeoutDecorator } = require('stryker-parent');2const { expect } = require('chai');3describe('TimeoutDecorator', () => {4 it('should return the result of the original function', () => {5 const originalFunction = () => 'result';6 const decoratedFunction = TimeoutDecorator.create(originalFunction, 1000);7 expect(decoratedFunction()).to.equal('result');8 });9});

Full Screen

Using AI Code Generation

copy

Full Screen

1var TimeoutDecorator = require('stryker-parent').TimeoutDecorator;2var timeoutDecorator = new TimeoutDecorator();3var myFunction = function(){4}5var myFunctionWithTimeout = timeoutDecorator.decorate(myFunction, 1000, "myFunction");6myFunctionWithTimeout();7var TimeoutDecorator = require('stryker-parent').TimeoutDecorator;8var timeoutDecorator = new TimeoutDecorator();9var myFunction = function(){10}11var myFunctionWithTimeout = timeoutDecorator.decorate(myFunction, 1000, "myFunction");12myFunctionWithTimeout();13var TimeoutDecorator = require('stryker-parent').TimeoutDecorator;14var timeoutDecorator = new TimeoutDecorator();15var myFunction = function(){16}17var myFunctionWithTimeout = timeoutDecorator.decorate(myFunction, 1000, "myFunction");18myFunctionWithTimeout();19var TimeoutDecorator = require('stryker-parent').TimeoutDecorator;20var timeoutDecorator = new TimeoutDecorator();21var myFunction = function(){22}23var myFunctionWithTimeout = timeoutDecorator.decorate(myFunction, 1000, "myFunction");24myFunctionWithTimeout();25var TimeoutDecorator = require('stryker-parent').TimeoutDecorator;26var timeoutDecorator = new TimeoutDecorator();27var myFunction = function(){28}29var myFunctionWithTimeout = timeoutDecorator.decorate(myFunction, 1000, "myFunction");30myFunctionWithTimeout();31var TimeoutDecorator = require('stryker-parent').TimeoutDecorator;32var timeoutDecorator = new TimeoutDecorator();33var myFunction = function(){34}35var myFunctionWithTimeout = timeoutDecorator.decorate(myFunction, 1000, "myFunction");36myFunctionWithTimeout();37var TimeoutDecorator = require('stryker-parent').TimeoutDecorator;38var timeoutDecorator = new TimeoutDecorator();39var myFunction = function(){40}

Full Screen

Using AI Code Generation

copy

Full Screen

1var TimeoutDecorator = require('stryker-parent').TimeoutDecorator;2var timeoutDecorator = new TimeoutDecorator();3var async = require('async');4var test = function() {5 async.parallel([6 function(done) {7 setTimeout(function() {8 console.log('test1');9 done();10 }, 1000);11 },12 function(done) {13 setTimeout(function() {14 console.log('test2');15 done();16 }, 1000);17 },18 function(done) {19 setTimeout(function() {20 console.log('test3');21 done();22 }, 1000);23 }24 ], function() {25 console.log('done');26 });27};28timeoutDecorator.timeout(test, 1000);

Full Screen

Using AI Code Generation

copy

Full Screen

1var TimeoutDecorator = require("stryker-parent").TimeoutDecorator;2var timeoutDecorator = new TimeoutDecorator();3var myFunction = function () {4 console.log("I will timeout in 3 seconds.");5};6timeoutDecorator.timeout(myFunction, 3000);7var TimeoutDecorator = require("stryker-api").TimeoutDecorator;8var timeoutDecorator = new TimeoutDecorator();9var myFunction = function () {10 console.log("I will timeout in 3 seconds.");11};12timeoutDecorator.timeout(myFunction, 3000);13var TimeoutDecorator = require("stryker").TimeoutDecorator;14var timeoutDecorator = new TimeoutDecorator();15var myFunction = function () {16 console.log("I will timeout in 3 seconds.");17};18timeoutDecorator.timeout(myFunction, 3000);19var TimeoutDecorator = require("stryker-javascript-mutator").TimeoutDecorator;20var timeoutDecorator = new TimeoutDecorator();21var myFunction = function () {22 console.log("I will timeout in 3 seconds.");23};24timeoutDecorator.timeout(myFunction, 3000);25var TimeoutDecorator = require("stryker-javascript-mutator").TimeoutDecorator;

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run stryker-parent automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful