|
@@ -1,5 +1,4 @@
|
1
|
1
|
import { describe, it } from "mocha";
|
2
|
|
-import { RPCServer, RPCSocket, Serializable } from '../Index'
|
3
|
2
|
import { RPCExporter, Socket } from "../src/Interfaces";
|
4
|
3
|
import { ConnectedSocket, Callback, GenericFunction } from "../src/Types";
|
5
|
4
|
import * as log from 'why-is-node-running';
|
|
@@ -10,6 +9,7 @@ import { PromiseIO } from "../src/PromiseIO/Server";
|
10
|
9
|
import { PromiseIOClient } from "../src/PromiseIO/Client";
|
11
|
10
|
import { assert, expect } from 'chai';
|
12
|
11
|
import { CLASSNAME_ATTRIBUTE, USER_DEFINED_TIMEOUT } from "../src/Strings";
|
|
12
|
+import { RPCServer, RPCSocket } from "../Index";
|
13
|
13
|
var should = require('chai').should();
|
14
|
14
|
var chai = require("chai");
|
15
|
15
|
var chaiAsPromised = require("chai-as-promised");
|
|
@@ -1010,126 +1010,4 @@ describe("attaching handlers before connecting", () => {
|
1010
|
1010
|
done(e)
|
1011
|
1011
|
})
|
1012
|
1012
|
})
|
1013
|
|
-})
|
1014
|
|
-
|
1015
|
|
-describe("class (de-)serialization", () => {
|
1016
|
|
-
|
1017
|
|
- @Serializable()
|
1018
|
|
- class SubClass {
|
1019
|
|
- fString = "F"
|
1020
|
|
- }
|
1021
|
|
-
|
1022
|
|
- @Serializable()
|
1023
|
|
- class TestClass {
|
1024
|
|
- aString = "A"
|
1025
|
|
- aNumber = 46
|
1026
|
|
- aObject = {
|
1027
|
|
- x: "x",
|
1028
|
|
- y: undefined,
|
1029
|
|
- sub: new SubClass()
|
1030
|
|
- }
|
1031
|
|
- aClassObject = new SubClass()
|
1032
|
|
-
|
1033
|
|
- public returnOK() {
|
1034
|
|
- return "OK"
|
1035
|
|
- }
|
1036
|
|
- }
|
1037
|
|
-
|
1038
|
|
- const verifyObject = (obj: any) => {
|
1039
|
|
- expect(obj).to.be.an.instanceOf(TestClass)
|
1040
|
|
- expect(obj.aString).to.be.a('string')
|
1041
|
|
- expect(obj.aNumber).to.be.a('number')
|
1042
|
|
- expect(obj.aObject).to.be.a('object')
|
1043
|
|
- expect(obj.aObject.x).to.be.a('string')
|
1044
|
|
- expect(obj.aObject.y).to.be.undefined
|
1045
|
|
- expect(obj.aObject.sub).to.be.an.instanceOf(SubClass)
|
1046
|
|
- expect(obj.aClassObject).to.be.an.instanceOf(SubClass)
|
1047
|
|
- expect(obj).to.not.have.key(CLASSNAME_ATTRIBUTE)
|
1048
|
|
- expect(obj.aObject.sub).to.not.have.key(CLASSNAME_ATTRIBUTE)
|
1049
|
|
- expect(obj.aClassObject).to.not.have.key(CLASSNAME_ATTRIBUTE)
|
1050
|
|
- expect(obj.returnOK()).to.be.equal('OK')
|
1051
|
|
- }
|
1052
|
|
-
|
1053
|
|
- describe("Responses", () => {
|
1054
|
|
- type TestIfc = {
|
1055
|
|
- Test: {
|
1056
|
|
- returnClass: () => Promise<TestClass>
|
1057
|
|
- classCallback: (callback: Callback<[TestClass]>) => Promise<TestClass>
|
1058
|
|
- }
|
1059
|
|
- }
|
1060
|
|
-
|
1061
|
|
- let myServer: RPCServer<TestIfc>;
|
1062
|
|
- let mySocket: ConnectedSocket<TestIfc>;
|
1063
|
|
-
|
1064
|
|
- before(function (done) {
|
1065
|
|
- myServer = new RPCServer<TestIfc>([{
|
1066
|
|
- name: "Test",
|
1067
|
|
- RPCs: [
|
1068
|
|
- async function returnClass() {
|
1069
|
|
- return new TestClass()
|
1070
|
|
- }, {
|
1071
|
|
- name: "classCallback",
|
1072
|
|
- hook: async function (callback) {
|
1073
|
|
- setTimeout(_ => callback(new TestClass()), 250)
|
1074
|
|
- return new TestClass()
|
1075
|
|
- }
|
1076
|
|
- }
|
1077
|
|
- ]
|
1078
|
|
- }])
|
1079
|
|
- myServer.listen(8084)
|
1080
|
|
-
|
1081
|
|
- new RPCSocket<TestIfc>(8084, 'localhost').connect().then(connsock => {
|
1082
|
|
- mySocket = connsock
|
1083
|
|
- done()
|
1084
|
|
- })
|
1085
|
|
- })
|
1086
|
|
-
|
1087
|
|
- after(function (done) {
|
1088
|
|
- mySocket.close()
|
1089
|
|
- myServer.close()
|
1090
|
|
- done()
|
1091
|
|
- })
|
1092
|
|
-
|
1093
|
|
-
|
1094
|
|
- it("receives class object in call response", async () => {
|
1095
|
|
- const obj: TestClass = await mySocket['Test'].returnClass()
|
1096
|
|
- verifyObject(obj)
|
1097
|
|
- })
|
1098
|
|
-
|
1099
|
|
- it("receives class object in hook response", async function () {
|
1100
|
|
- const obj: TestClass = await mySocket.Test.classCallback(function(x){
|
1101
|
|
- x
|
1102
|
|
- })
|
1103
|
|
- verifyObject(obj)
|
1104
|
|
- })
|
1105
|
|
-
|
1106
|
|
- it("receives class object in callback", function (done) {
|
1107
|
|
- mySocket.Test.classCallback(function (cbValue) {
|
1108
|
|
- verifyObject(cbValue)
|
1109
|
|
- done()
|
1110
|
|
- }).then(verifyObject)
|
1111
|
|
- })
|
1112
|
|
- })
|
1113
|
|
- describe("Parameters", () => {
|
1114
|
|
- it("Class object in call", function(done){
|
1115
|
|
- const server = new RPCServer([
|
1116
|
|
- {
|
1117
|
|
- name: "Test",
|
1118
|
|
- RPCs: [
|
1119
|
|
- function callWithClass(testObj: TestClass){
|
1120
|
|
- verifyObject(testObj)
|
1121
|
|
- done()
|
1122
|
|
- }
|
1123
|
|
- ]
|
1124
|
|
- }
|
1125
|
|
- ]).listen(8086)
|
1126
|
|
-
|
1127
|
|
- new RPCSocket(8086, 'localhost').connect().then(sock => {
|
1128
|
|
- sock['Test'].callWithClass(new TestClass()).then(_ => {
|
1129
|
|
- sock.close()
|
1130
|
|
- server.close()
|
1131
|
|
- })
|
1132
|
|
- })
|
1133
|
|
- })
|
1134
|
|
- })
|
1135
|
|
-})
|
|
1013
|
+})
|