before rewrite to have callbacks as FIRST parameter
This commit is contained in:
+117
-76
@@ -1,7 +1,7 @@
|
||||
import { describe, it } from "mocha";
|
||||
import { RPCServer, RPCSocket, Serializable } from '../Index'
|
||||
import { RPCExporter, Socket } from "../src/Interfaces";
|
||||
import { ConnectedSocket, Callback } from "../src/Types";
|
||||
import { ConnectedSocket, Callback, GenericFunction } from "../src/Types";
|
||||
import * as log from 'why-is-node-running';
|
||||
import * as http from 'http';
|
||||
import * as express from 'express';
|
||||
@@ -9,11 +9,11 @@ import * as fetch from 'node-fetch';
|
||||
import { PromiseIO } from "../src/PromiseIO/Server";
|
||||
import { PromiseIOClient } from "../src/PromiseIO/Client";
|
||||
import { assert, expect } from 'chai';
|
||||
import { USER_DEFINED_TIMEOUT } from "../src/Strings";
|
||||
import { CLASSNAME_ATTRIBUTE, USER_DEFINED_TIMEOUT } from "../src/Strings";
|
||||
var should = require('chai').should();
|
||||
var chai = require("chai");
|
||||
var chaiAsPromised = require("chai-as-promised");
|
||||
|
||||
|
||||
chai.use(chaiAsPromised);
|
||||
const noop = (...args) => { }
|
||||
|
||||
@@ -489,12 +489,12 @@ describe('RPCSocket', () => {
|
||||
})
|
||||
|
||||
|
||||
it('should have rpc echo', async() => {
|
||||
it('should have rpc echo', async () => {
|
||||
const x = await client['test'].echo("x")
|
||||
expect(x).to.be.equal('x')
|
||||
})
|
||||
|
||||
it('should add up to 6', async() => {
|
||||
it('should add up to 6', async () => {
|
||||
const sum = await client['test'].add(1, 2, 3)
|
||||
expect(sum).to.be.equal(6)
|
||||
})
|
||||
@@ -590,7 +590,7 @@ describe('It should do unhook', () => {
|
||||
let run = 0
|
||||
const expected = [yesCandy, noCandy, noCandy, noCandy]
|
||||
|
||||
it('Unhook+unsubscribe should stop callbacks', async() => {
|
||||
it('Unhook+unsubscribe should stop callbacks', async () => {
|
||||
await client['test'].subscribe(function myCallback(c) {
|
||||
if (run == 1)
|
||||
(myCallback as any).destroy()
|
||||
@@ -614,7 +614,7 @@ type topicDTO = { topic: string; }
|
||||
type SesameTestIfc = {
|
||||
test: {
|
||||
checkCandy: () => Promise<string>
|
||||
subscribe: (callback: Callback<string>) => Promise<topicDTO>
|
||||
subscribe: (callback: Callback<[string]>) => Promise<topicDTO>
|
||||
manyParams: <A = string, B = number, C = boolean, D = Object>(a: A, b: B, c: C, d: D) => Promise<[A, B, C, D]>
|
||||
}
|
||||
}
|
||||
@@ -688,7 +688,7 @@ describe('Sesame should unlock the socket', () => {
|
||||
})
|
||||
|
||||
it('callback should work with sesame', (done) => {
|
||||
client.test.subscribe(function(c){
|
||||
client.test.subscribe(function (c) {
|
||||
if (c === candy) {
|
||||
done()
|
||||
}
|
||||
@@ -725,21 +725,21 @@ describe('Error handling', () => {
|
||||
a: 'a',
|
||||
b: 'b'
|
||||
})
|
||||
.then(r => {
|
||||
if (r != null)
|
||||
done(new Error("UNEXPECTED RESULT " + r))
|
||||
})
|
||||
.catch((e) => {
|
||||
if (e.message === errtxt)
|
||||
done()
|
||||
else
|
||||
done(e)
|
||||
})
|
||||
.finally(() => {
|
||||
cli.close()
|
||||
sock.close()
|
||||
server.close()
|
||||
})
|
||||
.then(r => {
|
||||
if (r != null)
|
||||
done(new Error("UNEXPECTED RESULT " + r))
|
||||
})
|
||||
.catch((e) => {
|
||||
if (e.message === errtxt)
|
||||
done()
|
||||
else
|
||||
done(e)
|
||||
})
|
||||
.finally(() => {
|
||||
cli.close()
|
||||
sock.close()
|
||||
server.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1014,12 +1014,12 @@ describe("attaching handlers before connecting", () => {
|
||||
describe("class (de-)serialization", () => {
|
||||
|
||||
@Serializable()
|
||||
class SubClass{
|
||||
class SubClass {
|
||||
fString = "F"
|
||||
}
|
||||
|
||||
@Serializable()
|
||||
class TestClass{
|
||||
class TestClass {
|
||||
aString = "A"
|
||||
aNumber = 46
|
||||
aObject = {
|
||||
@@ -1029,63 +1029,104 @@ describe("class (de-)serialization", () => {
|
||||
}
|
||||
aClassObject = new SubClass()
|
||||
|
||||
public returnOK(){
|
||||
public returnOK() {
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
|
||||
let myServer: RPCServer;
|
||||
let mySocket: RPCSocket;
|
||||
const verifyObject = (obj: any) => {
|
||||
expect(obj).to.be.an.instanceOf(TestClass)
|
||||
expect(obj.aString).to.be.a('string')
|
||||
expect(obj.aNumber).to.be.a('number')
|
||||
expect(obj.aObject).to.be.a('object')
|
||||
expect(obj.aObject.x).to.be.a('string')
|
||||
expect(obj.aObject.y).to.be.undefined
|
||||
expect(obj.aObject.sub).to.be.an.instanceOf(SubClass)
|
||||
expect(obj.aClassObject).to.be.an.instanceOf(SubClass)
|
||||
expect(obj).to.not.have.key(CLASSNAME_ATTRIBUTE)
|
||||
expect(obj.aObject.sub).to.not.have.key(CLASSNAME_ATTRIBUTE)
|
||||
expect(obj.aClassObject).to.not.have.key(CLASSNAME_ATTRIBUTE)
|
||||
expect(obj.returnOK()).to.be.equal('OK')
|
||||
}
|
||||
|
||||
before(function(done){
|
||||
myServer = new RPCServer([{
|
||||
name: "Test",
|
||||
RPCs: [
|
||||
function returnClass(){
|
||||
return new TestClass()
|
||||
describe("Responses", () => {
|
||||
type TestIfc = {
|
||||
Test: {
|
||||
returnClass: () => Promise<TestClass>
|
||||
classCallback: (callback: Callback<[TestClass]>) => Promise<TestClass>
|
||||
}
|
||||
}
|
||||
|
||||
let myServer: RPCServer<TestIfc>;
|
||||
let mySocket: ConnectedSocket<TestIfc>;
|
||||
|
||||
before(function (done) {
|
||||
myServer = new RPCServer<TestIfc>([{
|
||||
name: "Test",
|
||||
RPCs: [
|
||||
async function returnClass() {
|
||||
return new TestClass()
|
||||
}, {
|
||||
name: "classCallback",
|
||||
hook: async function (callback) {
|
||||
setTimeout(_ => callback(new TestClass()), 250)
|
||||
return new TestClass()
|
||||
}
|
||||
}
|
||||
]
|
||||
}])
|
||||
myServer.listen(8084)
|
||||
|
||||
new RPCSocket<TestIfc>(8084, 'localhost').connect().then(connsock => {
|
||||
mySocket = connsock
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
after(function (done) {
|
||||
mySocket.close()
|
||||
myServer.close()
|
||||
done()
|
||||
})
|
||||
|
||||
|
||||
it("receives class object in call response", async () => {
|
||||
const obj: TestClass = await mySocket['Test'].returnClass()
|
||||
verifyObject(obj)
|
||||
})
|
||||
|
||||
it("receives class object in hook response", async function () {
|
||||
const obj: TestClass = await mySocket.Test.classCallback(noop)
|
||||
verifyObject(obj)
|
||||
})
|
||||
|
||||
it("receives class object in callback", function (done) {
|
||||
mySocket.Test.classCallback(function (cbValue) {
|
||||
verifyObject(cbValue)
|
||||
done()
|
||||
}).then(verifyObject)
|
||||
})
|
||||
})
|
||||
describe("Parameters", () => {
|
||||
it("Class object in call", function(done){
|
||||
const server = new RPCServer([
|
||||
{
|
||||
name: "Test",
|
||||
RPCs: [
|
||||
function callWithClass(testObj: TestClass){
|
||||
verifyObject(testObj)
|
||||
done()
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}])
|
||||
myServer.listen(8084)
|
||||
]).listen(8086)
|
||||
|
||||
mySocket = new RPCSocket(8084, 'localhost')
|
||||
mySocket.connect().then(() => done())
|
||||
})
|
||||
|
||||
after(function(done){
|
||||
mySocket.close()
|
||||
myServer.close()
|
||||
done()
|
||||
})
|
||||
|
||||
|
||||
it("receives class in call response", async () => {
|
||||
const obj: TestClass = await mySocket['Test'].returnClass()
|
||||
|
||||
expect(obj).to.be.an.instanceOf(TestClass)
|
||||
expect(obj.aString).to.be.a('string')
|
||||
expect(obj.aNumber).to.be.a('number')
|
||||
expect(obj.aObject).to.be.a('object')
|
||||
expect(obj.aObject.x).to.be.a('string')
|
||||
expect(obj.aObject.y).to.be.undefined
|
||||
expect(obj.aObject.sub).to.be.an.instanceOf(SubClass)
|
||||
expect(obj.aClassObject).to.be.an.instanceOf(SubClass)
|
||||
|
||||
expect(obj.returnOK()).to.be.equal('OK')
|
||||
})
|
||||
|
||||
it("receives class in hook response", async () => {
|
||||
const obj: TestClass = await mySocket['Test'].returnClass()
|
||||
|
||||
expect(obj).to.be.an.instanceOf(TestClass)
|
||||
expect(obj.aString).to.be.a('string')
|
||||
expect(obj.aNumber).to.be.a('number')
|
||||
expect(obj.aObject).to.be.a('object')
|
||||
expect(obj.aObject.x).to.be.a('string')
|
||||
expect(obj.aObject.y).to.be.undefined
|
||||
expect(obj.aObject.sub).to.be.an.instanceOf(SubClass)
|
||||
expect(obj.aClassObject).to.be.an.instanceOf(SubClass)
|
||||
|
||||
expect(obj.returnOK()).to.be.equal('OK')
|
||||
new RPCSocket(8086, 'localhost').connect().then(sock => {
|
||||
sock['Test'].callWithClass(new TestClass()).then(_ => {
|
||||
sock.close()
|
||||
server.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user