Best JavaScript code snippet using best
agent.ts
Source:agent.ts
...39 this.state = AgentState.IDLE;40 this.loadRunnerSpecs();41 if (this.remoteHubConfig.uri) {42 this.once('ready', () => {43 const remoteHub = this.setupNewHub(this.remoteHubConfig, this.specs, this.agentConfig);44 remoteHub.connectToHub();45 });46 }47 }48 async loadRunnerSpecs() {49 this.specs = await getBrowserSpecs(this.agentConfig.runner);50 console.log(`[AGENT] Available specs: ${JSON.stringify(this.specs)}`);51 this.emit('ready');52 }53 onClientConnect(socketClient: ServerSocket) {54 const query = socketClient.handshake.query;55 const config = normalizeClientConfig(query);56 const invalidConfig = validateConfig(config, this.agentConfig, this.specs, socketClient.id);57 if (invalidConfig) {58 socketClient.emit(BEST_RPC.AGENT_REJECTION, invalidConfig);59 return socketClient.disconnect(true);60 }61 const remoteClient = this.setupNewClient(socketClient, config);62 console.log(`[AGENT] Connected clients: ${this.connectedClients.size} | state: ${this.state} | activePending: ${this.activeClient && this.activeClient.getPendingBenchmarks()}`);63 if (this.idleState) {64 this.runBenchmark(remoteClient);65 } else {66 remoteClient.log(`Client enqueued. Waiting for the agent to be free...`);67 this.emit(BEST_RPC.AGENT_QUEUED_CLIENT, { clientId: remoteClient.getId(), jobs: config.jobs, specs: config.specs });68 }69 }70 async runBenchmark(remoteClient: RemoteClient) {71 if (this.idleState) {72 this.state = AgentState.BUSY;73 this.activeClient = remoteClient;74 try {75 console.log(`[AGENT] Requesting benchmark from RemoteClient ${remoteClient.getId()}`);76 const benchmarkBuild = await remoteClient.requestJob();77 const bundleConfig = createBundleConfig(benchmarkBuild, this.agentConfig);78 console.log(`[AGENT] Running benchmark ${benchmarkBuild.benchmarkSignature} from RemoteClient ${remoteClient.getId()}`);79 this.interruption = new RunnerInterruption(benchmarkBuild.benchmarkSignature);80 const results = await runBenchmarks(bundleConfig, remoteClient, this.interruption);81 console.log(`[AGENT] Completed benchmark ${benchmarkBuild.benchmarkSignature} from RemoteClient ${remoteClient.getId()}`);82 remoteClient.sendResults(results);83 } catch(err) {84 console.log(`[AGENT] Error running benchmark for remote client ${remoteClient.getId()}`);85 console.log(err);86 remoteClient.disconnectClient(`Error running benchmark ${err}`); // make sure we disconnect the agent87 } finally {88 this.state = AgentState.IDLE;89 this.interruption = undefined;90 queueMicrotask(() => this.runQueuedBenchmarks());91 }92 } else {93 console.log('[AGENT] Benchmark already running...');94 }95 }96 runQueuedBenchmarks() {97 if (this.idleState) {98 console.log('[AGENT] Checking for queued agents and tasks...');99 if (this.activeClient && this.activeClient.getPendingBenchmarks()) {100 console.log(`[AGENT] Active Client "${this.activeClient.getId()}" has still ${this.activeClient.getPendingBenchmarks()} pending`);101 this.runBenchmark(this.activeClient);102 } else {103 // Note that there might be some clients with no jobs still connected (we give them some time to disconnect)104 // So to avoid race conditions we check for remaining jobs, rather that just check for an arbitrary client on the queue105 const remoteClient = Array.from(this.connectedClients).find(client => client.getPendingBenchmarks() > 0);106 if (remoteClient) {107 console.log(`[AGENT] Client "${remoteClient.getId()}" has ${remoteClient.getPendingBenchmarks()} to run`);108 this.runBenchmark(remoteClient);109 } else {110 console.log('[AGENT] No more jobs to run at the moment');111 }112 }113 } else {114 console.log(`[AGENT] Busy, running ${this.activeClient!.getId()}`);115 }116 }117 get idleState() { return this.state === AgentState.IDLE; }118 getStateInfo() {119 return `120 |> state: ${this.idleState}121 |> clients: ${this.connectedClients.size}122 |> activeClient: ${this.activeClient && this.activeClient.getId()}123 `;124 }125 setupNewHub(remoteHubConfig: RemoteHubConfig, specs: BrowserSpec[], agentConfig: AgentConfig): RemoteHub {126 const remoteHub = new RemoteHub(remoteHubConfig, specs, agentConfig);127 remoteHub.on(BEST_RPC.AGENT_CONNECTED_HUB, (hubUri) => this.emit(BEST_RPC.AGENT_CONNECTED_HUB, hubUri));128 remoteHub.on(BEST_RPC.AGENT_DISCONNECTED_HUB, (hubUri) => this.emit(BEST_RPC.AGENT_DISCONNECTED_HUB, hubUri));129 return remoteHub;130 }131 setupNewClient(socketClient: ServerSocket, clientConfig: RemoteClientConfig): RemoteClient {132 // Create and new RemoteClient and add it to the pool133 const remoteClient = new RemoteClient(socketClient, clientConfig);134 this.connectedClients.add(remoteClient);135 console.log(`[AGENT] New client ${remoteClient.getId()} connected. Jobs requested ${clientConfig.jobs} | specs: ${JSON.stringify(clientConfig.specs)}`);136 this.emit(BEST_RPC.AGENT_CONNECTED_CLIENT, { clientId: remoteClient.getId(), jobs: clientConfig.jobs });137 // Make sure we remove it from an agent's perspective if the client is disconnected138 remoteClient.on(BEST_RPC.DISCONNECT, () => {139 console.log(`[AGENT] Disconnected client ${remoteClient.getId()}`);...
Using AI Code Generation
1var BestBuy = require('./BestBuy.js');2var bestBuy = new BestBuy();3bestBuy.setupNewHub();4var Hub = require('./Hub.js');5var hub = new Hub();6module.exports = function(){7 this.setupNewHub = function(){8 hub.setupNewHub();9 }10}11module.exports = function(){12 this.setupNewHub = function(){13 console.log("new hub setup");14 }15}
Using AI Code Generation
1const Bestbuy = require("./bestbuy");2const bestbuy = new Bestbuy();3bestbuy.setupNewHub("Hub1");4const Bestbuy = require("./bestbuy");5const bestbuy = new Bestbuy();6bestbuy.setupNewHub("Hub2");7const Bestbuy = require("./bestbuy");8const bestbuy = new Bestbuy();9bestbuy.setupNewHub("Hub3");10const Bestbuy = require("./bestbuy");11const bestbuy = new Bestbuy();12bestbuy.setupNewHub("Hub4");13const Bestbuy = require("./bestbuy");14const bestbuy = new Bestbuy();15bestbuy.setupNewHub("Hub5");16const Bestbuy = require("./bestbuy");17const bestbuy = new Bestbuy();18bestbuy.setupNewHub("Hub6");19const Bestbuy = require("./bestbuy");20const bestbuy = new Bestbuy();21bestbuy.setupNewHub("Hub7");22const Bestbuy = require("./bestbuy");23const bestbuy = new Bestbuy();24bestbuy.setupNewHub("Hub8");25const Bestbuy = require("./bestbuy");26const bestbuy = new Bestbuy();27bestbuy.setupNewHub("Hub9");28const Bestbuy = require("./bestbuy");29const bestbuy = new Bestbuy();30bestbuy.setupNewHub("Hub10");31const Bestbuy = require("./bestbuy");32const bestbuy = new Bestbuy();33bestbuy.setupNewHub("Hub11");
Using AI Code Generation
1var BestBuy = require('./BestBuy');2var bestBuy = new BestBuy();3bestBuy.setupNewHub("TestHub");4var BestBuy = function() {5 this.setupNewHub = function(hubName) {6 console.log("hubName = " + hubName);7 }8};9module.exports = BestBuy;
Using AI Code Generation
1var BestBuy = require('./bestbuy');2var bb = new BestBuy();3bb.setupNewHub('test2');4var BestBuy = function() {5 this.setupNewHub = function(name) {6 console.log(name);7 }8}9module.exports = BestBuy;
Using AI Code Generation
1var BestBuyHub = require('./BestBuyHub.js');2var newHub = BestBuyHub.setupNewHub();3console.log('Hub created: ' + newHub);4var newHub = BestBuyHub.setupNewHub();5console.log('Hub created: ' + newHub);6function setupNewHub() {7 return 'new Hub created';8}9module.exports.setupNewHub = setupNewHub;10var BestBuyHub = require('./BestBuyHub.js');11var newHub = BestBuyHub.setupNewHub();12console.log('Hub created: ' + newHub);13var newHub = BestBuyHub.setupNewHub();14console.log('Hub created: ' + newHub);15function setupNewHub() {16 return 'new Hub created';17}18module.exports.setupNewHub = setupNewHub;19var BestBuyHub = require('./BestBuyHub.js');20var newHub = BestBuyHub.setupNewHub();21console.log('Hub created: ' + newHub);22var newHub = BestBuyHub.setupNewHub();23console.log('Hub created: ' + newHub);24function setupNewHub() {25 return 'new Hub created';26}27module.exports.setupNewHub = setupNewHub;28var BestBuyHub = require('./BestBuyHub.js');29var newHub = BestBuyHub.setupNewHub();30console.log('Hub created: ' + newHub);
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!!