Skip to content

Commit

Permalink
Wait for workers deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
InversionSpaces committed Oct 20, 2023
1 parent 2a4a619 commit 4314958
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
export default {
testEnvironment: "node",
testTimeout: 1000 * 60 * 10, // 10 minutes in milliseconds
bail: true,
projects: [
{
// Uses the serial runner for integration test files
Expand Down
2 changes: 1 addition & 1 deletion src/aqua/main.aqua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use "hosts.aqua"
import "services.aqua"

-- IMPORTANT: Add exports for all functions that you want to run
export helloWorld, helloWorldRemote, getInfo, getInfos
export showSubnet, helloWorld, helloWorldRemote, getInfo, getInfos

-- DOCUMENTATION:
-- https://fluence.dev
Expand Down
39 changes: 33 additions & 6 deletions test/fRPC.integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
import { multiaddr } from "@multiformats/multiaddr";

import { FLUENCE_CHAIN_PRIVATE_KEY, FLUENCE_ENV, RPC_PROVIDERS } from "./env";
import { startGateway, fluence, backupFile, randomElement } from "./utils";
import {
startGateway,
fluence,
subnet,
backupFile,
randomElement,
} from "./utils";
import { updateConfig } from "./config";

function throwError(msg: string): never {
Expand All @@ -41,6 +47,7 @@ async function testGateway(mode?: string, times = 6) {
id,
};
const response = await gateway.request(request);
console.log(response);
expect(response).toMatchObject({
jsonrpc: "2.0",
id,
Expand Down Expand Up @@ -154,14 +161,34 @@ describe("fRPC", () => {
// Remove previous deployment info
await backupFile(".fluence/workers.yaml");

const [stdout, _] = await fluenceKeyEnv("deal", "deploy");
const [stdout, stderr] = await fluenceKeyEnv("deal", "deploy");

expect(stdout.includes("Success!")).toBeTruthy();

// Wait until workers are deployed
// TODO: Make this gracefully,
// call aqua subnet resolution
await new Promise((resolve) => setTimeout(resolve, 10000));
const workersMatch = stderr.match(/(\d+)\s*workers/);
const workers =
workersMatch?.[1] ?? throwError("Failed to parse workers");
const workersNum = parseInt(workers);

expect(workersNum).toBeGreaterThanOrEqual(3);

/**
* Wait for workers to deploy
*/
const DEPLOY_TIMEOUT = 60_000;
const deadline = Date.now() + DEPLOY_TIMEOUT;
for (;;) {
const workers = await subnet(FLUENCE_ENV);
const deployed = workers.filter((w) => w.worker_id !== undefined);
if (deployed.length === workersNum) {
break;
}
if (Date.now() > deadline) {
throw new Error(
`Deployment timeout: ${workersNum} workers expected, ${deployed.length} deployed`,
);
}
}
});

["random", "round-robin", "quorum"].forEach((mode) => {
Expand Down
24 changes: 24 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ export async function fluence(...args: string[]): Promise<[string, string]> {
return execute("fluence", ...args, "--no-input");
}

interface Worker {
host_id: string;
worker_id: string | undefined;
}

/**
* Run `showSubnet` function and return workers
* @param env Fluence environment
* @returns Subnet workers
*/
export async function subnet(env: string): Promise<Worker[]> {
const [stdout, _] = await fluence(
"run",
"-f",
"showSubnet()",
"-i",
"src/aqua/main.aqua",
"--env",
env,
);

return JSON.parse(stdout);
}

export class Gateway {
private stdout: string = "";

Expand Down

0 comments on commit 4314958

Please sign in to comment.