Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit aa21abe

Browse files
authored
Extend error handling in FluencePeer (#98)
1 parent 337a3f4 commit aa21abe

File tree

12 files changed

+214
-103
lines changed

12 files changed

+214
-103
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"license": "Apache-2.0",
2222
"dependencies": {
2323
"@chainsafe/libp2p-noise": "4.0.0",
24-
"@fluencelabs/avm": "0.15.4",
24+
"@fluencelabs/avm": "0.16.6",
2525
"async": "3.2.0",
2626
"base64-js": "1.5.1",
2727
"bs58": "4.0.1",

src/__test__/integration/avm.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FluencePeer } from '../../index';
22
import { Particle } from '../../internal/Particle';
3+
import { handleTimeout } from '../../internal/utils';
34
import { registerHandlersHelper } from '../util';
45

56
describe('Avm spec', () => {
@@ -21,10 +22,9 @@ describe('Avm spec', () => {
2122
resolve(res);
2223
},
2324
},
24-
_timeout: reject,
2525
});
2626

27-
peer.internals.initiateParticle(particle);
27+
peer.internals.initiateParticle(particle, handleTimeout(reject));
2828
});
2929

3030
// assert
@@ -61,10 +61,9 @@ describe('Avm spec', () => {
6161
}
6262
},
6363
},
64-
_timeout: reject,
6564
});
6665

67-
peer.internals.initiateParticle(particle);
66+
peer.internals.initiateParticle(particle, handleTimeout(reject));
6867
});
6968

7069
// assert

src/__test__/integration/compiler/compiler.spec.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Fluence, FluencePeer } from '../../..';
22
import { Particle } from '../../../internal/Particle';
33
import { registerHandlersHelper } from '../../util';
44
import { callMeBack, registerHelloWorld } from './gen1';
5+
import { callFunction } from '../../../internal/compilerSupport/v2';
6+
import { handleTimeout } from '../../../internal/utils';
57

68
describe('Compiler support infrastructure tests', () => {
79
it('Compiled code for function should work', async () => {
@@ -78,10 +80,9 @@ describe('Compiler support infrastructure tests', () => {
7880
resolve(val);
7981
},
8082
},
81-
_timeout: reject,
8283
});
8384

84-
Fluence.getPeer().internals.initiateParticle(particle);
85+
Fluence.getPeer().internals.initiateParticle(particle, handleTimeout(reject));
8586
});
8687

8788
// assert
@@ -166,9 +167,8 @@ describe('Compiler support infrastructure tests', () => {
166167
resolve(val);
167168
},
168169
},
169-
_timeout: reject,
170170
});
171-
anotherPeer.internals.initiateParticle(particle);
171+
anotherPeer.internals.initiateParticle(particle, handleTimeout(reject));
172172
});
173173

174174
// assert
@@ -177,4 +177,33 @@ describe('Compiler support infrastructure tests', () => {
177177

178178
await anotherPeer.stop();
179179
});
180+
181+
it('Should throw error if particle with incorrect AIR script is initiated', async () => {
182+
// arrange;
183+
const anotherPeer = new FluencePeer();
184+
await anotherPeer.start();
185+
186+
// act
187+
const action = callFunction(
188+
[anotherPeer],
189+
{
190+
functionName: 'dontcare',
191+
argDefs: [],
192+
returnType: { tag: 'void' },
193+
names: {
194+
relay: '-relay-',
195+
getDataSrv: 'getDataSrv',
196+
callbackSrv: 'callbackSrv',
197+
responseSrv: 'callbackSrv',
198+
responseFnName: 'response',
199+
errorHandlingSrv: 'errorHandlingSrv',
200+
errorFnName: 'error',
201+
},
202+
},
203+
'incorrect air script',
204+
);
205+
206+
// assert
207+
await expect(action).rejects.toMatch(/incorrect air script/);
208+
});
180209
});

src/__test__/integration/peer.spec.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Multiaddr } from 'multiaddr';
22
import { nodes } from '../connection';
33
import { Fluence, FluencePeer, setLogLevel } from '../../index';
4-
import { checkConnection } from '../../internal/utils';
4+
import { checkConnection, doNothing, handleTimeout } from '../../internal/utils';
55
import { Particle } from '../../internal/Particle';
66
import { registerHandlersHelper } from '../util';
77

@@ -121,10 +121,9 @@ describe('Typescript usage suite', () => {
121121
reject(error);
122122
},
123123
},
124-
_timeout: reject,
125124
});
126125

127-
anotherPeer.internals.initiateParticle(particle);
126+
anotherPeer.internals.initiateParticle(particle, handleTimeout(reject));
128127
});
129128

130129
// assert
@@ -169,7 +168,7 @@ describe('Typescript usage suite', () => {
169168
)
170169
`;
171170
const particle = Particle.createNew(script);
172-
await peer1.internals.initiateParticle(particle);
171+
await peer1.internals.initiateParticle(particle, doNothing);
173172

174173
// assert
175174
const res = await resMakingPromise;
@@ -309,10 +308,9 @@ describe('Typescript usage suite', () => {
309308
resolve(res);
310309
},
311310
},
312-
_timeout: reject,
313311
});
314312

315-
anotherPeer.internals.initiateParticle(particle);
313+
anotherPeer.internals.initiateParticle(particle, handleTimeout(reject));
316314
});
317315

318316
// assert
@@ -368,10 +366,9 @@ describe('Typescript usage suite', () => {
368366
reject(error);
369367
},
370368
},
371-
_timeout: reject,
372369
});
373370

374-
anotherPeer.internals.initiateParticle(particle);
371+
anotherPeer.internals.initiateParticle(particle, handleTimeout(reject));
375372
});
376373

377374
// assert
@@ -381,7 +378,6 @@ describe('Typescript usage suite', () => {
381378

382379
it('Should not crash if an error ocurred in user-defined handler', async () => {
383380
// arrange;
384-
setLogLevel('trace');
385381
await anotherPeer.start();
386382

387383
// act
@@ -405,10 +401,9 @@ describe('Typescript usage suite', () => {
405401
reject(error);
406402
},
407403
},
408-
_timeout: reject,
409404
});
410405

411-
anotherPeer.internals.initiateParticle(particle);
406+
anotherPeer.internals.initiateParticle(particle, handleTimeout(reject));
412407
});
413408

414409
// assert
@@ -417,6 +412,22 @@ describe('Typescript usage suite', () => {
417412
});
418413
});
419414

415+
it('Should throw error if particle is initiated on a stopped peer', async () => {
416+
// arrange;
417+
const stoppedPeer = new FluencePeer();
418+
419+
// act
420+
const action = () => {
421+
const script = `(null)`;
422+
const particle = Particle.createNew(script);
423+
424+
stoppedPeer.internals.initiateParticle(particle, doNothing);
425+
};
426+
427+
// assert
428+
await expect(action).toThrow('Cannot initiate new particle: peer is not initialized');
429+
});
430+
420431
it.skip('Should throw correct error when the client tries to send a particle not to the relay', async () => {
421432
// arrange;
422433
await anotherPeer.start({ connectTo: nodes[0] });
@@ -439,7 +450,7 @@ describe('Typescript usage suite', () => {
439450
},
440451
});
441452

442-
anotherPeer.internals.initiateParticle(particle);
453+
anotherPeer.internals.initiateParticle(particle, doNothing);
443454
});
444455

445456
// assert
@@ -468,10 +479,9 @@ async function callIncorrectService(peer: FluencePeer): Promise<string[]> {
468479
reject(error);
469480
},
470481
},
471-
_timeout: reject,
472482
});
473483

474-
peer.internals.initiateParticle(particle);
484+
peer.internals.initiateParticle(particle, handleTimeout(reject));
475485
});
476486

477487
return promise;

src/__test__/util.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ import { Particle } from '../internal/Particle';
33
import { MakeServiceCall } from '../internal/utils';
44

55
export const registerHandlersHelper = (peer: FluencePeer, particle: Particle, handlers) => {
6-
const { _timeout, ...rest } = handlers;
7-
if (_timeout) {
8-
peer.internals.regHandler.timeout(particle.id, _timeout);
9-
}
10-
for (let serviceId in rest) {
11-
for (let fnName in rest[serviceId]) {
6+
for (let serviceId in handlers) {
7+
for (let fnName in handlers[serviceId]) {
128
// of type [args] => result
13-
const h = rest[serviceId][fnName];
9+
const h = handlers[serviceId][fnName];
1410
peer.internals.regHandler.forParticle(particle.id, serviceId, fnName, MakeServiceCall(h));
1511
}
1612
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const Fluence = {
4444
},
4545

4646
/**
47-
* Uninitializes the default peer: stops all the underltying workflows, stops the Aqua VM
47+
* Un-initializes the default peer: stops all the underlying workflows, stops the Aqua VM
4848
* and disconnects from the Fluence network
4949
*/
5050
stop: (): Promise<void> => {

0 commit comments

Comments
 (0)