-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotocol.test.js
144 lines (138 loc) · 4.21 KB
/
protocol.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { test, expect } from 'vitest';
import MessageCall from './message';
import JPCProtocol from './protocol';
class LPCMessage extends MessageCall {
remote = null;
connect(remote) {
this.remote = remote;
remote.remote = this;
}
send(message) {
this.remote._incomingMessage(message);
}
}
class LPCProtocol extends JPCProtocol {
message = new LPCMessage();
connect(remote) {
this.message.connect(remote.message);
}
registerIncomingCall(method, listener) {
this.message.register(method, listener);
}
callRemote(method, payload) {
return this.message.makeCall(method, payload);
}
}
class Collection {
_array = [];
add(item) {
this._array.push(item);
return true;
}
remove(item) {
let pos = this._array.indexOf(item);
if (pos < 0) {
return false;
}
this._array.splice(pos, 1);
return true;
}
clear() {
this._array.length = 0;
}
get isEmpty() {
return this._array.length == 0;
}
get size() {
return this._array.length;
}
set size(val) {
this._array.length = val;
}
contains(item) {
return this._array.includes(item);
}
}
test('Protocol', async () => {
let server = new LPCProtocol({ success: true, callable: function(arg) { return !arg; }, makeCollection: function() { return new Collection(); } });
let client = new LPCProtocol();
server.init();
client.init();
client.connect(server);
let start = await client.getRemoteStartObject();
expect(start).toBeInstanceOf(Object);
expect(start).toHaveProperty("success", true);
expect(start).toHaveProperty("callable");
await expect(start.callable(false)).resolves.toBe(true);
await expect(start.callable(true)).resolves.toBe(false);
expect(start).toHaveProperty("makeCollection");
let collection = await start.makeCollection();
await expect(collection.isEmpty).resolves.toBe(true);
await collection.add(0);
await collection.add("");
await collection.add(null);
await collection.add(false);
await expect(collection.isEmpty).resolves.toBe(false);
await expect(collection.size).resolves.toBe(4);
await expect(collection.contains(false)).resolves.toBe(true);
await expect(collection.contains(true)).resolves.toBe(false);
await collection.setSize(3);
await expect(collection.remove(0)).resolves.toBe(true);
await expect(collection.size).resolves.toBe(2);
await expect(collection.remove(0)).resolves.toBe(false);
await collection.clear();
await expect(collection.isEmpty).resolves.toBe(true);
if (globalThis.gc) {
expect(client._remoteObjects.size).toBe(3);
expect(server._localIDsToObjects.size).toBe(3);
collection = null;
start = null;
await new Promise(resolve => setTimeout(resolve, 0));
globalThis.gc();
await new Promise(resolve => setTimeout(resolve, 0));
globalThis.gc();
await new Promise(resolve => setTimeout(resolve, 0));
expect(client._remoteObjects.size).toBe(0);
expect(server._localIDsToObjects.size).toBe(2);
for (let [id, obj] of server._localIDsToObjects) {
expect(obj).toBeInstanceOf(WeakRef);
}
} else {
console.warn("Unable to run gc tests");
}
});
class Interesting {
_subscribers = new Set();
didStuff = false;
subscribe(subscriber) {
subscriber(this);
this._subscribers.add(subscriber);
return () => this._subscribers.remove(subscriber);
}
doStuff() {
this.didStuff = true;
for (let subscriber of this._subscribers) {
subscriber(this);
}
}
}
test('Observable', async () => {
let server = new LPCProtocol({ makeObservable: function() { return new Interesting(); } });
let client = new LPCProtocol();
server.init();
client.init();
client.connect(server);
let start = await client.getRemoteStartObject();
expect(start).toBeInstanceOf(Object);
expect(start).toHaveProperty("makeObservable");
let interesting = await start.makeObservable();
expect(interesting.didStuff).toBe(false);
let notifications = 0;
interesting.subscribe(() => notifications++);
expect(notifications).toBe(1);
await interesting.doStuff();
// Note: This only works because the test is mostly synchronous.
// In real life you might have to wait for the notification.
expect(interesting.didStuff).toBe(true);
expect(notifications).toBe(2);
});