Check if connection is available on creating a client (#23)

Check if connection is available on creating a client
This commit is contained in:
Dima 2021-02-19 16:32:02 +03:00 committed by GitHub
parent 78ed8ab46c
commit c10cd1942a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 26 deletions

View File

@ -12,6 +12,7 @@ import {
} from '../../internal/builtins';
import { ModuleConfig } from '../../internal/moduleConfig';
import { createConnectedClient } from '../util';
import {checkConnection} from "../../api";
const dev2multiaddr = '/dns4/dev.fluence.dev/tcp/19003/wss/p2p/12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb';
const dev3multiaddr = '/dns4/dev.fluence.dev/tcp/19004/wss/p2p/12D3KooWJbJFaZ3k5sNd8DjQgg3aERoKtBAnirEvPV8yp76kEXHB';
@ -43,6 +44,14 @@ describe('Builtins usage suite', () => {
expect(bpList).not.toBeUndefined;
});
it('check_connection', async function () {
const client = await createConnectedClient(dev2multiaddr);
let isConnected = await checkConnection(client);
expect(isConnected).toEqual(true);
});
it('upload_modules', async function () {
const client = await createConnectedClient(dev2multiaddr);

View File

@ -40,8 +40,7 @@ describe('Typescript usage suite', () => {
await client.sendScript(script, data);
const res = await resMakingPromise;
return res;
return await resMakingPromise;
};
it('address as string', async function () {
@ -198,13 +197,8 @@ describe('Typescript usage suite', () => {
it('two clients should work inside the same time browser', async function () {
// arrange
const pid1 = await generatePeerId();
const client1 = new FluenceClientImpl(pid1);
await client1.connect(devNodeAddress);
const pid2 = await generatePeerId();
const client2 = new FluenceClientImpl(pid2);
await client2.connect(devNodeAddress);
const client1 = await createConnectedClient(devNodeAddress);
const client2 = await createConnectedClient(devNodeAddress);
let resMakingPromise = new Promise((resolve) => {
client2.registerCallback('test', 'test', (args, _) => {
@ -216,7 +210,7 @@ describe('Typescript usage suite', () => {
let script = `
(seq
(call "${client1.relayPeerId}" ("op" "identity") [])
(call "${pid2.toB58String()}" ("test" "test") [a b c d])
(call "${client2.selfPeerId}" ("test" "test") [a b c d])
)
`;
@ -234,13 +228,8 @@ describe('Typescript usage suite', () => {
it('event registration should work', async function () {
// arrange
const pid1 = await generatePeerId();
const client1 = new FluenceClientImpl(pid1);
await client1.connect(devNodeAddress);
const pid2 = await generatePeerId();
const client2 = new FluenceClientImpl(pid2);
await client2.connect(devNodeAddress);
const client1 = await createConnectedClient(devNodeAddress);
const client2 = await createConnectedClient(devNodeAddress);
client2.registerEvent('event_stream', 'test');
const resMakingPromise = new Promise((resolve) => {
@ -249,7 +238,7 @@ describe('Typescript usage suite', () => {
// act
let script = `
(call "${pid2.toB58String()}" ("event_stream" "test") [hello])
(call "${client2.selfPeerId}" ("event_stream" "test") [hello])
`;
let data: Map<string, any> = new Map();

View File

@ -5,6 +5,7 @@ import Multiaddr from 'multiaddr';
import PeerId, { isPeerId } from 'peer-id';
import { generatePeerId, seedToPeerId } from './internal/peerIdUtils';
import { FluenceClientImpl } from './internal/FluenceClientImpl';
import log from "loglevel";
type Node = {
peerId: string;
@ -44,6 +45,9 @@ export const createClient = async (
}
await client.connect(theAddress);
if (!await checkConnection(client)) {
throw new Error("Connection check failed. Check if the node is working or try to connect to another node")
}
}
return client;
@ -147,7 +151,43 @@ export const sendParticleAsFetch = async <T>(
}, particle.ttl);
});
sendParticle(client, particle);
await sendParticle(client, particle);
return promise;
};
export const checkConnection = async (client: FluenceClient): Promise<boolean> => {
let msg = Math.random().toString(36).substring(7);
let callbackFn = "checkConnection"
let callbackService = "_callback"
const particle = new Particle(
`
(seq
(call __relay ("op" "identity") [msg] result)
(call myPeerId ("${callbackService}" "${callbackFn}") [result])
)
`,
{
__relay: client.relayPeerId,
myPeerId: client.selfPeerId,
msg
},
3000
);
if (!client.isConnected) {
return false;
}
try {
let result = await sendParticleAsFetch<string[][]>(client, particle, callbackFn, callbackService)
if (result[0][0] != msg) {
log.warn("unexpected behavior. 'identity' must return arguments the passed arguments.")
}
return true;
} catch (e) {
log.error("Error on establishing connection: ", e)
return false;
}
}