mirror of
https://github.com/Ylianst/MeshAgent
synced 2026-02-05 11:13:51 +00:00
1. Added ability to disconnect control channel
2. Added event emitter tests
This commit is contained in:
@@ -1899,6 +1899,14 @@ duk_ret_t ILibDuktape_MeshAgent_DataPing(duk_context *ctx)
|
||||
MeshServer_SendJSON(agent, agent->controlChannel, "{\"action\":\"ping\"}", 17);
|
||||
return(1);
|
||||
}
|
||||
duk_ret_t ILibDuktape_MeshAgent_Disconnect(duk_context *ctx)
|
||||
{
|
||||
duk_push_this(ctx); // [MeshAgent]
|
||||
MeshAgentHostContainer *agent = (MeshAgentHostContainer*)Duktape_GetPointerProperty(ctx, -1, MESH_AGENT_PTR);
|
||||
ILibWebClient_Disconnect(agent->controlChannel);
|
||||
return(0);
|
||||
}
|
||||
|
||||
void ILibDuktape_MeshAgent_PUSH(duk_context *ctx, void *chain)
|
||||
{
|
||||
MeshAgentHostContainer *agent;
|
||||
@@ -2013,6 +2021,7 @@ void ILibDuktape_MeshAgent_PUSH(duk_context *ctx, void *chain)
|
||||
ILibDuktape_CreateInstanceMethod(ctx, "eval", ILibDuktape_MeshAgent_eval, 1);
|
||||
ILibDuktape_CreateInstanceMethod(ctx, "forceExit", ILibDuktape_MeshAgent_forceExit, DUK_VARARGS);
|
||||
ILibDuktape_CreateInstanceMethod(ctx, "hostname", ILibDuktape_MeshAgent_hostname, 0);
|
||||
ILibDuktape_CreateInstanceMethod(ctx, "disconnect", ILibDuktape_MeshAgent_Disconnect, 0);
|
||||
|
||||
Duktape_CreateEnum(ctx, "ContainerPermissions", (char*[]) { "DEFAULT", "NO_AGENT", "NO_MARSHAL", "NO_PROCESS_SPAWNING", "NO_FILE_SYSTEM_ACCESS", "NO_NETWORK_ACCESS" }, (int[]) { 0x00, 0x10000000, 0x08000000, 0x04000000, 0x00000001, 0x00000002 }, 6);
|
||||
duk_push_string(ctx, agent->displayName); ILibDuktape_CreateReadonlyProperty_SetEnumerable(ctx, "displayName",1);
|
||||
|
||||
@@ -1944,4 +1944,73 @@ if (!localOnly)
|
||||
if (localOnly)
|
||||
{
|
||||
console.log('Running Local Tests');
|
||||
eventtests()
|
||||
|
||||
|
||||
process.exit();
|
||||
}
|
||||
|
||||
|
||||
function newListenerSink(name, handler)
|
||||
{
|
||||
global.evresults[name]++;
|
||||
}
|
||||
function removeListenerSink(name, handler)
|
||||
{
|
||||
global.evresults[name + '_Rem']++;
|
||||
}
|
||||
|
||||
function A_Sink()
|
||||
{
|
||||
global.evresults.A_Res += '2';
|
||||
}
|
||||
function B_Sink()
|
||||
{
|
||||
global.evresults.B_Trig++;
|
||||
}
|
||||
function A_Prepended_Sink()
|
||||
{
|
||||
global.evresults.A_Res += '1';
|
||||
}
|
||||
function A_Prepended_Once_Sink()
|
||||
{
|
||||
global.evresults.A_Res += 'A';
|
||||
}
|
||||
|
||||
function eventtests()
|
||||
{
|
||||
global.evresults = { A: 0, B: 0, B_Trig: 0, A_Res: '', A_Rem: 0, B_Rem: 0 };
|
||||
var obj = {};
|
||||
var res;
|
||||
require('events').EventEmitter.call(obj);
|
||||
|
||||
console.log(' Event Emitter Tests');
|
||||
|
||||
obj.on('removeListener', removeListenerSink);
|
||||
obj.on('newListener', newListenerSink);
|
||||
|
||||
obj.on('A', A_Sink);
|
||||
obj.once('B', B_Sink);
|
||||
obj.prependListener('A', A_Prepended_Sink);
|
||||
obj.prependOnceListener('A', A_Prepended_Once_Sink);
|
||||
|
||||
res = (global.evresults.A==3 && global.evresults.B==1) ? 'OK' : 'FAILED'
|
||||
console.log(' newListener dispatched correctly with on/once/prepend/prependOnce..............................[' + res + ']');
|
||||
|
||||
obj.emit('B');
|
||||
obj.emit('B');
|
||||
obj.emit('B');
|
||||
obj.emit('A');
|
||||
obj.emit('A');
|
||||
obj.removeListener('A', A_Prepended_Sink);
|
||||
obj.emit('A');
|
||||
|
||||
res = (global.evresults.B_Trig == 1) ? 'OK' : 'FAILED';
|
||||
console.log(' once events correctly auto unregister..........................................................[' + res + ']');
|
||||
|
||||
res = (global.evresults.A_Rem == 2 && global.evresults.B_Rem == 1) ? 'OK' : 'FAILED';
|
||||
console.log(' removeListener correctly dispatched unsubscriptions............................................[' + res + ']');
|
||||
|
||||
res = (global.evresults.A_Res == 'A12122') ? 'OK' : 'FAILED';
|
||||
console.log(' events are dispatched in correct order.........................................................[' + res + ']');
|
||||
}
|
||||
Reference in New Issue
Block a user