Best JavaScript code snippet using best
hub.ts
Source:hub.ts
...32 // -- Client lifecycle ---------------------------------------------------------------33 onClientConnect(clientSocket: Socket) {34 const query = clientSocket.handshake.query;35 const config = normalizeClientConfig(query);36 const invalidConfig = validateConfig(config, this.hubConfig, this.getAgentSpecs(), clientSocket.id);37 if (invalidConfig) {38 clientSocket.emit(BEST_RPC.AGENT_REJECTION, invalidConfig);39 return clientSocket.disconnect(true);40 }41 const remoteClient = this.setupNewClient(clientSocket, config);42 console.log(`[HUB] Connected clients: ${this.connectedClients.size}`);43 if (this.idleAgentMatchingSpecs(remoteClient)) {44 this.runBenchmarks(remoteClient);45 } else {46 remoteClient.log(`Client enqueued. Waiting for an agent to be free...`);47 this.emit(BEST_RPC.AGENT_QUEUED_CLIENT, { clientId: remoteClient.getId(), jobs: config.jobs, specs: config.specs });48 }49 }50 setupNewClient(socketClient: Socket, clientConfig: RemoteClientConfig): RemoteClient {51 // Create and new RemoteClient and add it to the pool52 const remoteClient = new RemoteClient(socketClient, clientConfig);53 this.connectedClients.add(remoteClient);54 console.log(`[HUB] New client ${remoteClient.getId()} connected. Jobs requested ${clientConfig.jobs} | specs: ${JSON.stringify(clientConfig.specs)}`);55 this.emit(BEST_RPC.AGENT_CONNECTED_CLIENT, { clientId: remoteClient.getId(), jobs: clientConfig.jobs, specs: remoteClient.getSpecs() });56 // Make sure we remove it from an agent's perspective if the client is disconnected57 remoteClient.on(BEST_RPC.DISCONNECT, () => {58 console.log(`[HUB] Disconnected client ${remoteClient.getId()}`);59 this.emit(BEST_RPC.AGENT_DISCONNECTED_CLIENT, remoteClient.getId());60 this.connectedClients.delete(remoteClient);61 console.log(`[HUB] Connected clients: ${this.connectedClients.size}`);62 // If the client is actively running something we need to kill it63 if (this.activeClients.has(remoteClient)) {64 const remoteAgent = this.activeClients.get(remoteClient);65 if (remoteAgent && remoteAgent.isBusy()) {66 remoteAgent.interruptRunner();67 }68 this.activeClients.delete(remoteClient);69 }70 });71 remoteClient.on(BEST_RPC.BENCHMARK_START, (benchmarkId: string) => {72 const agent = this.activeClients.get(remoteClient);73 this.emit(BEST_RPC.BENCHMARK_START, { agentId: agent && agent.getId(), clientId: remoteClient.getId(), benchmarkId });74 });75 remoteClient.on(BEST_RPC.BENCHMARK_END, (benchmarkId: string) => {76 const agent = this.activeClients.get(remoteClient);77 this.emit(BEST_RPC.BENCHMARK_END, { agentId: agent && agent.getId(), clientId: remoteClient.getId(), benchmarkId });78 });79 remoteClient.on(BEST_RPC.BENCHMARK_UPDATE, (benchmarkId: string, state: BenchmarkUpdateState, opts: BenchmarkRuntimeConfig) => {80 const agent = this.activeClients.get(remoteClient);81 this.emit(BEST_RPC.BENCHMARK_UPDATE, { agentId: agent && agent.getId(), clientId: remoteClient.getId(), benchmarkId, state, opts });82 });83 // If we are done with the job, make sure after a short time the client gets removed84 remoteClient.on(BEST_RPC.REMOTE_CLIENT_EMPTY_QUEUE, () => {85 console.log(`[HUB] Remote client ${remoteClient.getId()} is done. Scheduling a force disconnect.`);86 setTimeout(() => {87 if (this.connectedClients.has(remoteClient)) {88 console.log(`[HUB] Force client disconnect (${remoteClient.getId()}): With no more jobs to run an agent must disconnect`);89 remoteClient.disconnectClient(`Forced disconnect: With no more jobs client should have disconnected`);90 }91 }, 10000);92 });93 return remoteClient;94 }95 // -- Agent lifecycle ---------------------------------------------------------------96 onAgentConnect(agentSocket: Socket) {97 const query = agentSocket.handshake.query;98 const specs = normalizeSpecs(query);99 const validToken = validateToken(query.authToken as string, this.hubConfig.authToken);100 const hasSpecs = specs.length > 0;101 if (!validToken) {102 agentSocket.emit(BEST_RPC.AGENT_REJECTION, 'Invalid Token');103 return agentSocket.disconnect(true);104 }105 if (!hasSpecs) {106 agentSocket.emit(BEST_RPC.AGENT_REJECTION, 'An agent must provide specs');107 return agentSocket.disconnect(true);108 }109 if (!query.agentUri) {110 agentSocket.emit(BEST_RPC.AGENT_REJECTION, 'An agent must provide a URI');111 return agentSocket.disconnect(true);112 }113 const remoteAgent = this.setupNewAgent(agentSocket, specs, { agentUri: query.agentUri, agentToken: query.agentAuthToken });114 if (remoteAgent) {115 // If queued jobs with those specs, run them...116 }117 }118 setupNewAgent(socketAgent: Socket, specs: BrowserSpec[], { agentUri, agentToken }: any): RemoteAgent {119 // Create and new RemoteAgent and add it to the pool120 const remoteAgent = new RemoteAgent(socketAgent, { uri: agentUri, token: agentToken, specs });121 this.connectedAgents.add(remoteAgent);122 console.log(`[HUB] New Agent ${remoteAgent.getId()} connected with specs: ${JSON.stringify(remoteAgent.getSpecs())}`);123 this.emit(BEST_RPC.HUB_CONNECTED_AGENT, { agentId: remoteAgent.getId(), specs: remoteAgent.getSpecs(), uri: remoteAgent.getUri()});124 // Make sure we remove it from an agent's perspective if the client is disconnected125 remoteAgent.on(BEST_RPC.DISCONNECT, () => {126 console.log(`[HUB] Disconnected Agent ${remoteAgent.getId()}`);127 this.emit(BEST_RPC.HUB_DISCONNECTED_AGENT, { agentId: remoteAgent.getId() });128 this.connectedAgents.delete(remoteAgent);129 if (remoteAgent.isBusy()) {130 remoteAgent.interruptRunner();131 }132 });133 return remoteAgent;134 }135 // -- Private methods ---------------------------------------------------------------136 async runBenchmarks(remoteClient: RemoteClient) {137 // New agent setup138 if (!this.activeClients.has(remoteClient)) {139 const matchingAgents = this.findAgentMatchingSpecs(remoteClient, { ignoreBusy: true });140 if (matchingAgents.length > 0) {141 const remoteAgent = matchingAgents[0];142 this.activeClients.set(remoteClient, remoteAgent);143 this.emit(BEST_RPC.AGENT_RUNNING_CLIENT, { clientId: remoteClient.getId(), agentId: remoteAgent.getId(), jobs: remoteClient.getPendingBenchmarks() });144 try {145 await remoteAgent.runBenchmarks(remoteClient);146 } catch(err) {147 console.log(`[HUB] Error running benchmark for remote client ${remoteClient.getId()}`);148 remoteClient.disconnectClient(`Error running benchmark ${err}`); // make sure we disconnect the agent149 } finally {150 this.activeClients.delete(remoteClient);151 queueMicrotask(() => this.runQueuedBenchmarks());152 }153 } else {154 console.log('[HUB] All agents are busy at this moment...');155 }156 } else {157 console.log(`[HUB] Client ${remoteClient.getId()} is actively running already`)158 }159 }160 runQueuedBenchmarks() {161 Array.from(this.connectedClients).forEach((remoteClient) => {162 if (!this.activeClients.has(remoteClient)) {163 if (this.idleAgentMatchingSpecs(remoteClient) && remoteClient.getPendingBenchmarks() > 0) {164 console.log(`[HUB] Running benchmark: "${remoteClient.getId()}" has ${remoteClient.getPendingBenchmarks()} to run`);165 this.runBenchmarks(remoteClient);166 } else {167 console.log(`[HUB] All matching agents still busy for ${remoteClient.getId()}`);168 }169 }170 });171 }172 getAgentSpecs(): BrowserSpec[] {173 const specs: BrowserSpec[] = [];174 for (const agent of this.connectedAgents) {175 specs.push(...agent.getSpecs());176 }177 return specs;178 }179 idleAgentMatchingSpecs(remoteClient: RemoteClient): boolean {180 return this.findAgentMatchingSpecs(remoteClient, { ignoreBusy: true }).length > 0;181 }182 findAgentMatchingSpecs(remoteClient: RemoteClient, { ignoreBusy }: { ignoreBusy?: boolean } = {}): RemoteAgent[] {183 const specs = remoteClient.getSpecs();184 const agents: RemoteAgent[] = [];185 if (specs) {186 for (const agent of this.connectedAgents) {...
Using AI Code Generation
1var request = require('request');2var async = require('async');3var fs = require('fs');4var path = require('path');5var _ = require('underscore');6var request = require('request');7var url = require('url');8var moment = require('moment');9var cheerio = require('cheerio');10var json2csv = require('json2csv');
Using AI Code Generation
1var bestPractices = require('bestPractices');2var agentSpecs = bestPractices.getAgentSpecs();3console.log('agentSpecs = ' + JSON.stringify(agentSpecs));4var bestPractices = require('bestPractices');5var agentSpecs = bestPractices.getAgentSpecs('myAgentName');6console.log('agentSpecs = ' + JSON.stringify(agentSpecs));
Using AI Code Generation
1var BestBuy = require('./BestBuy');2var bb = new BestBuy();3bb.getAgentSpecs();4function BestBuy() {5}6BestBuy.prototype.getAgentSpecs = function() {7 console.log("getting agent specs");8}9function BestBuy() {10}11BestBuy.prototype.getAgentSpecs = function() {12 console.log("getting agent specs");13}14var BestBuy = require('./BestBuy');15var bb = new BestBuy();16bb.getAgentSpecs();17function BestBuy() {18}19BestBuy.prototype.getAgentSpecs = function() {20 console.log("getting agent specs");21}22var BestBuy = require('./BestBuy');23var bb = new BestBuy();24bb.getAgentSpecs();25function BestBuy() {26}27BestBuy.prototype.getAgentSpecs = function() {28 console.log("getting agent specs");29}30var BestBuy = require('./BestBuy');31var bb = new BestBuy();32bb.getAgentSpecs();33function BestBuy() {34}35BestBuy.prototype.getAgentSpecs = function() {36 console.log("getting agent specs");37}38var BestBuy = require('./BestBuy');39var bb = new BestBuy();40bb.getAgentSpecs();41function BestBuy() {42}43BestBuy.prototype.getAgentSpecs = function()
Using AI Code Generation
1var BestBuyAgent = require("./BestBuyAgent");2var myAgent = new BestBuyAgent();3var specs = myAgent.getAgentSpecs();4console.log(specs);5var BestBuyAgent = require("./BestBuyAgent");6var myAgent = new BestBuyAgent();7var specs = myAgent.getAgentSpecs();8for (var i = 0; i < specs.length; i++) {9 console.log(specs[i]);10}11var BestBuyAgent = require("./BestBuyAgent");12var myAgent = new BestBuyAgent();13var specs = myAgent.getAgentSpecs();14for (var i = 0; i < specs.length; i++) {15 console.log("Spec " + i + ": " + specs[i]);16}17var BestBuyAgent = require("./BestBuyAgent");18var myAgent = new BestBuyAgent();19var specs = myAgent.getAgentSpecs();
Using AI Code Generation
1var agent_id = "bestbuy";2var agent_type = "e-commerce";3var agent_version = "1.0";4var agent = new Agent(agent_id, agent_type, agent_version);5var agent_specs = agent.getAgentSpecs();6console.log(agent_specs);7var agent_id = "bestbuy";8var agent_type = "e-commerce";9var agent_version = "1.0";10var agent = new Agent(agent_id, agent_type, agent_version);11var agent_specs = agent.getAgentSpecs();12console.log(agent_specs);13var agent_id = "bestbuy";14var agent_type = "e-commerce";15var agent_version = "1.0";16var agent = new Agent(agent_id, agent_type, agent_version);17var agent_specs = agent.getAgentSpecs();18console.log(agent_specs);19var agent_id = "bestbuy";20var agent_type = "e-commerce";21var agent_version = "1.0";22var agent = new Agent(agent_id, agent_type, agent_version);23var agent_specs = agent.getAgentSpecs();24console.log(agent_specs);25var agent_id = "bestbuy";26var agent_type = "e-commerce";27var agent_version = "1.0";28var agent = new Agent(agent_id, agent_type, agent_version);29var agent_specs = agent.getAgentSpecs();30console.log(agent_specs);31var agent_id = "bestbuy";32var agent_type = "e-commerce";33var agent_version = "1.0";34var agent = new Agent(agent_id, agent_type, agent_version);35var agent_specs = agent.getAgentSpecs();36console.log(agent_specs);
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!!