Skip to content

Commit ca666a0

Browse files
committed
testing fixes.
1 parent 64293ff commit ca666a0

5 files changed

Lines changed: 58 additions & 24 deletions

File tree

src/PolykeyAgent.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ class PolykeyAgent {
540540
tlsConfig,
541541
});
542542

543-
// TODO: nodeConnectionManager and NodeGraph lifecycle.
543+
// Await this.nodeConnectionManager.start(); // TODO: implement this.
544+
await this.nodeGraph.start({ fresh });
544545
await this.nodeManager.start({ fresh });
545546
await this.nodeConnectionManager.getConnectionsToSeedNodes();
546547
await this.nodeManager.syncNodeGraph();
@@ -569,8 +570,9 @@ class PolykeyAgent {
569570
await this.sessionManager.stop();
570571
await this.notificationsManager.stop();
571572
await this.vaultManager.stop();
572-
// TODO: nodeConnectionManager and NodeGraph lifecycle.
573573
await this.nodeManager.stop();
574+
// Await this.nodeConnectionManager.stop(); // TODO: Implement this.
575+
await this.nodeGraph.stop();
574576
await this.revProxy.stop();
575577
await this.fwdProxy.stop();
576578
await this.grpcServerAgent.stop();
@@ -594,8 +596,9 @@ class PolykeyAgent {
594596
await this.notificationsManager.destroy();
595597
await this.discovery.destroy();
596598
await this.vaultManager.destroy();
597-
// TODO: nodeConnectionManager and NodeGraph lifecycle.
598599
await this.nodeManager.destroy();
600+
await this.nodeConnectionManager.destroy();
601+
await this.nodeGraph.destroy();
599602
await this.gestaltGraph.destroy();
600603
await this.acl.destroy();
601604
await this.sigchain.destroy();

src/nodes/NodeConnectionManager.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import * as nodesUtils from './utils';
1717
import * as utils from '../utils';
1818
import { utils as networkUtils } from '../network';
1919

20+
// TODO: convert to CDSS to match NodeManager and NodeGraph. fits better with polykeyAgent lifecycle.
21+
2022
interface NodeConnectionManager extends CreateDestroy {}
2123
@CreateDestroy()
2224
class NodeConnectionManager {
@@ -87,7 +89,12 @@ class NodeConnectionManager {
8789
}
8890

8991
public async destroy() {
90-
// TODO: Needs to close out all of the active connections.
92+
for (const [nodeId, connAndLock] of this.connections) {
93+
if (connAndLock == null) continue;
94+
if (connAndLock.connection == null) continue;
95+
// It exists so we want to destroy it.
96+
await this.destroyConnection(nodeId);
97+
}
9198
}
9299

93100
@ready(new Error('tempError')) // TODO: Make a proper Error.

src/nodes/NodeManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ class NodeManager {
3737
protected nodeGraph: NodeGraph;
3838
protected sigchain: Sigchain;
3939
protected keyManager: KeyManager;
40-
protected fwdProxy: ForwardProxy;
41-
protected revProxy: ReverseProxy;
40+
protected fwdProxy: ForwardProxy; // FIXME: This needs to be removed.
4241
protected nodeConnectionManager: NodeConnectionManager;
4342

4443
static async createNodeManager({
@@ -179,6 +178,7 @@ class NodeManager {
179178
try {
180179
// Attempt to open a connection via the forward proxy
181180
// i.e. no NodeConnection object created (no need for GRPCClient)
181+
// TODO: NodeManager doesn't hold a reference to proxies anymore, we need an alternative method to ping them.
182182
await this.fwdProxy.openConnection(
183183
targetNodeId,
184184
await networkUtils.resolveHost(targetAddress.host),

tests/nodes/NodeConnectionManager.test.ts

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ import { makeNodeId } from '@/nodes/utils';
1212
import * as nodesUtils from '@/nodes/utils';
1313
import { ForwardProxy, ReverseProxy } from '@/network';
1414
import * as nodesErrors from '@/nodes/errors';
15+
import * as keysUtils from '@/keys/utils';
1516
import * as nodesTestUtils from './utils';
1617
import { makeCrypto } from '../utils';
1718
import * as testUtils from '../utils';
1819

20+
// Mocks.
21+
jest.mock('@/keys/utils', () => ({
22+
...jest.requireActual('@/keys/utils'),
23+
generateDeterministicKeyPair:
24+
jest.requireActual('@/keys/utils').generateKeyPair,
25+
}));
26+
1927
describe('NodeConnectionManager test', () => {
2028
const logger = new Logger('NodeGraph Test', LogLevel.WARN, [
2129
new StreamHandler(),
@@ -45,6 +53,9 @@ describe('NodeConnectionManager test', () => {
4553
'vi3et1hrpv2m2lrplcm7cu913kr45v51cak54vm68anlbvuf83ra0',
4654
);
4755

56+
const serverHost = '::1' as Host;
57+
const serverPort = 1 as Port;
58+
4859
//
4960
let dataDir: string;
5061
let keyManager: KeyManager;
@@ -92,17 +103,6 @@ describe('NodeConnectionManager test', () => {
92103
return makeNodeId(idArray);
93104
};
94105

95-
beforeAll(async () => {
96-
fwdProxy = new ForwardProxy({
97-
authToken: 'auth',
98-
logger: logger,
99-
});
100-
101-
revProxy = new ReverseProxy({
102-
logger: logger,
103-
});
104-
});
105-
106106
beforeEach(async () => {
107107
dataDir = await fs.promises.mkdtemp(
108108
path.join(os.tmpdir(), 'polykey-test-'),
@@ -124,6 +124,25 @@ describe('NodeConnectionManager test', () => {
124124
keyManager,
125125
logger,
126126
});
127+
const tlsConfig = {
128+
keyPrivatePem: keyManager.getRootKeyPairPem().privateKey,
129+
certChainPem: keysUtils.certToPem(keyManager.getRootCert()),
130+
};
131+
fwdProxy = new ForwardProxy({
132+
authToken: 'auth',
133+
logger: logger,
134+
});
135+
await fwdProxy.start({
136+
tlsConfig,
137+
});
138+
revProxy = new ReverseProxy({
139+
logger: logger,
140+
});
141+
await revProxy.start({
142+
serverHost,
143+
serverPort,
144+
tlsConfig,
145+
});
127146
});
128147

129148
afterEach(async () => {
@@ -133,9 +152,6 @@ describe('NodeConnectionManager test', () => {
133152
await db.destroy();
134153
await keyManager.stop();
135154
await keyManager.destroy();
136-
});
137-
138-
afterAll(async () => {
139155
await revProxy.stop();
140156
await fwdProxy.stop();
141157
});
@@ -178,6 +194,7 @@ describe('NodeConnectionManager test', () => {
178194
nodeGraph,
179195
fwdProxy,
180196
revProxy,
197+
logger,
181198
});
182199
try {
183200
const initialConnLock =
@@ -203,6 +220,7 @@ describe('NodeConnectionManager test', () => {
203220
nodeGraph,
204221
fwdProxy,
205222
revProxy,
223+
logger,
206224
});
207225
try {
208226
// @ts-ignore accessing protected NodeConnectionMap
@@ -228,6 +246,7 @@ describe('NodeConnectionManager test', () => {
228246
nodeGraph,
229247
fwdProxy,
230248
revProxy,
249+
logger,
231250
});
232251
try {
233252
// @ts-ignore accessing protected NodeConnectionMap
@@ -260,6 +279,7 @@ describe('NodeConnectionManager test', () => {
260279
nodeGraph,
261280
fwdProxy,
262281
revProxy,
282+
logger,
263283
});
264284
try {
265285
// Add the dummy node
@@ -268,15 +288,15 @@ describe('NodeConnectionManager test', () => {
268288
port: 55555 as Port,
269289
});
270290
// @ts-ignore accessing protected NodeConnectionMap
271-
expect(nodeManager.connections.size).toBe(0);
291+
expect(nodeConnectionManager.connections.size).toBe(0);
272292

273293
await expect(() =>
274294
nodeConnectionManager.createConnection(dummyNode),
275295
).rejects.toThrow(nodesErrors.ErrorNodeConnectionTimeout);
276296
// @ts-ignore accessing protected NodeConnectionMap
277-
expect(nodeManager.connections.size).toBe(1);
297+
expect(nodeConnectionManager.connections.size).toBe(1);
278298
// @ts-ignore accessing protected NodeConnectionMap
279-
const connLock = nodeManager.connections.get(dummyNode);
299+
const connLock = nodeConnectionManager.connections.get(dummyNode);
280300
// There should still be an entry in the connection map, but it should
281301
// only contain a lock - no connection.
282302
expect(connLock).toBeDefined();
@@ -301,6 +321,7 @@ describe('NodeConnectionManager test', () => {
301321
nodeGraph,
302322
fwdProxy,
303323
revProxy,
324+
logger,
304325
});
305326
try {
306327
// Case 1: node already exists in the local node graph (no contact required)
@@ -328,6 +349,7 @@ describe('NodeConnectionManager test', () => {
328349
nodeGraph,
329350
fwdProxy,
330351
revProxy,
352+
logger,
331353
});
332354
try {
333355
// Case 2: node can be found on the remote node
@@ -362,6 +384,7 @@ describe('NodeConnectionManager test', () => {
362384
nodeGraph,
363385
fwdProxy,
364386
revProxy,
387+
logger,
365388
});
366389
try {
367390
// Case 3: node exhausts all contacts and cannot find node
@@ -398,6 +421,7 @@ describe('NodeConnectionManager test', () => {
398421
nodeGraph,
399422
fwdProxy,
400423
revProxy,
424+
logger,
401425
});
402426
try {
403427
// New node added

tests/nodes/NodeManager.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('NodeManager', () => {
103103
nodeGraph = await NodeGraph.createNodeGraph({
104104
db,
105105
keyManager,
106-
logger: undefined,
106+
logger,
107107
});
108108
nodeConnectionManager =
109109
await NodeConnectionManager.createNodeConnectionManager({

0 commit comments

Comments
 (0)