Skip to content

Commit

Permalink
testing fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Jan 7, 2022
1 parent 64293ff commit ca666a0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
9 changes: 6 additions & 3 deletions src/PolykeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,8 @@ class PolykeyAgent {
tlsConfig,
});

// TODO: nodeConnectionManager and NodeGraph lifecycle.
// Await this.nodeConnectionManager.start(); // TODO: implement this.
await this.nodeGraph.start({ fresh });
await this.nodeManager.start({ fresh });
await this.nodeConnectionManager.getConnectionsToSeedNodes();
await this.nodeManager.syncNodeGraph();
Expand Down Expand Up @@ -569,8 +570,9 @@ class PolykeyAgent {
await this.sessionManager.stop();
await this.notificationsManager.stop();
await this.vaultManager.stop();
// TODO: nodeConnectionManager and NodeGraph lifecycle.
await this.nodeManager.stop();
// Await this.nodeConnectionManager.stop(); // TODO: Implement this.
await this.nodeGraph.stop();
await this.revProxy.stop();
await this.fwdProxy.stop();
await this.grpcServerAgent.stop();
Expand All @@ -594,8 +596,9 @@ class PolykeyAgent {
await this.notificationsManager.destroy();
await this.discovery.destroy();
await this.vaultManager.destroy();
// TODO: nodeConnectionManager and NodeGraph lifecycle.
await this.nodeManager.destroy();
await this.nodeConnectionManager.destroy();
await this.nodeGraph.destroy();
await this.gestaltGraph.destroy();
await this.acl.destroy();
await this.sigchain.destroy();
Expand Down
9 changes: 8 additions & 1 deletion src/nodes/NodeConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import * as nodesUtils from './utils';
import * as utils from '../utils';
import { utils as networkUtils } from '../network';

// TODO: convert to CDSS to match NodeManager and NodeGraph. fits better with polykeyAgent lifecycle.

interface NodeConnectionManager extends CreateDestroy {}
@CreateDestroy()
class NodeConnectionManager {
Expand Down Expand Up @@ -87,7 +89,12 @@ class NodeConnectionManager {
}

public async destroy() {
// TODO: Needs to close out all of the active connections.
for (const [nodeId, connAndLock] of this.connections) {
if (connAndLock == null) continue;
if (connAndLock.connection == null) continue;
// It exists so we want to destroy it.
await this.destroyConnection(nodeId);
}
}

@ready(new Error('tempError')) // TODO: Make a proper Error.
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class NodeManager {
protected nodeGraph: NodeGraph;
protected sigchain: Sigchain;
protected keyManager: KeyManager;
protected fwdProxy: ForwardProxy;
protected revProxy: ReverseProxy;
protected fwdProxy: ForwardProxy; // FIXME: This needs to be removed.
protected nodeConnectionManager: NodeConnectionManager;

static async createNodeManager({
Expand Down Expand Up @@ -179,6 +178,7 @@ class NodeManager {
try {
// Attempt to open a connection via the forward proxy
// i.e. no NodeConnection object created (no need for GRPCClient)
// TODO: NodeManager doesn't hold a reference to proxies anymore, we need an alternative method to ping them.
await this.fwdProxy.openConnection(
targetNodeId,
await networkUtils.resolveHost(targetAddress.host),
Expand Down
58 changes: 41 additions & 17 deletions tests/nodes/NodeConnectionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ import { makeNodeId } from '@/nodes/utils';
import * as nodesUtils from '@/nodes/utils';
import { ForwardProxy, ReverseProxy } from '@/network';
import * as nodesErrors from '@/nodes/errors';
import * as keysUtils from '@/keys/utils';
import * as nodesTestUtils from './utils';
import { makeCrypto } from '../utils';
import * as testUtils from '../utils';

// Mocks.
jest.mock('@/keys/utils', () => ({
...jest.requireActual('@/keys/utils'),
generateDeterministicKeyPair:
jest.requireActual('@/keys/utils').generateKeyPair,
}));

describe('NodeConnectionManager test', () => {
const logger = new Logger('NodeGraph Test', LogLevel.WARN, [
new StreamHandler(),
Expand Down Expand Up @@ -45,6 +53,9 @@ describe('NodeConnectionManager test', () => {
'vi3et1hrpv2m2lrplcm7cu913kr45v51cak54vm68anlbvuf83ra0',
);

const serverHost = '::1' as Host;
const serverPort = 1 as Port;

//
let dataDir: string;
let keyManager: KeyManager;
Expand Down Expand Up @@ -92,17 +103,6 @@ describe('NodeConnectionManager test', () => {
return makeNodeId(idArray);
};

beforeAll(async () => {
fwdProxy = new ForwardProxy({
authToken: 'auth',
logger: logger,
});

revProxy = new ReverseProxy({
logger: logger,
});
});

beforeEach(async () => {
dataDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'polykey-test-'),
Expand All @@ -124,6 +124,25 @@ describe('NodeConnectionManager test', () => {
keyManager,
logger,
});
const tlsConfig = {
keyPrivatePem: keyManager.getRootKeyPairPem().privateKey,
certChainPem: keysUtils.certToPem(keyManager.getRootCert()),
};
fwdProxy = new ForwardProxy({
authToken: 'auth',
logger: logger,
});
await fwdProxy.start({
tlsConfig,
});
revProxy = new ReverseProxy({
logger: logger,
});
await revProxy.start({
serverHost,
serverPort,
tlsConfig,
});
});

afterEach(async () => {
Expand All @@ -133,9 +152,6 @@ describe('NodeConnectionManager test', () => {
await db.destroy();
await keyManager.stop();
await keyManager.destroy();
});

afterAll(async () => {
await revProxy.stop();
await fwdProxy.stop();
});
Expand Down Expand Up @@ -178,6 +194,7 @@ describe('NodeConnectionManager test', () => {
nodeGraph,
fwdProxy,
revProxy,
logger,
});
try {
const initialConnLock =
Expand All @@ -203,6 +220,7 @@ describe('NodeConnectionManager test', () => {
nodeGraph,
fwdProxy,
revProxy,
logger,
});
try {
// @ts-ignore accessing protected NodeConnectionMap
Expand All @@ -228,6 +246,7 @@ describe('NodeConnectionManager test', () => {
nodeGraph,
fwdProxy,
revProxy,
logger,
});
try {
// @ts-ignore accessing protected NodeConnectionMap
Expand Down Expand Up @@ -260,6 +279,7 @@ describe('NodeConnectionManager test', () => {
nodeGraph,
fwdProxy,
revProxy,
logger,
});
try {
// Add the dummy node
Expand All @@ -268,15 +288,15 @@ describe('NodeConnectionManager test', () => {
port: 55555 as Port,
});
// @ts-ignore accessing protected NodeConnectionMap
expect(nodeManager.connections.size).toBe(0);
expect(nodeConnectionManager.connections.size).toBe(0);

await expect(() =>
nodeConnectionManager.createConnection(dummyNode),
).rejects.toThrow(nodesErrors.ErrorNodeConnectionTimeout);
// @ts-ignore accessing protected NodeConnectionMap
expect(nodeManager.connections.size).toBe(1);
expect(nodeConnectionManager.connections.size).toBe(1);
// @ts-ignore accessing protected NodeConnectionMap
const connLock = nodeManager.connections.get(dummyNode);
const connLock = nodeConnectionManager.connections.get(dummyNode);
// There should still be an entry in the connection map, but it should
// only contain a lock - no connection.
expect(connLock).toBeDefined();
Expand All @@ -301,6 +321,7 @@ describe('NodeConnectionManager test', () => {
nodeGraph,
fwdProxy,
revProxy,
logger,
});
try {
// Case 1: node already exists in the local node graph (no contact required)
Expand Down Expand Up @@ -328,6 +349,7 @@ describe('NodeConnectionManager test', () => {
nodeGraph,
fwdProxy,
revProxy,
logger,
});
try {
// Case 2: node can be found on the remote node
Expand Down Expand Up @@ -362,6 +384,7 @@ describe('NodeConnectionManager test', () => {
nodeGraph,
fwdProxy,
revProxy,
logger,
});
try {
// Case 3: node exhausts all contacts and cannot find node
Expand Down Expand Up @@ -398,6 +421,7 @@ describe('NodeConnectionManager test', () => {
nodeGraph,
fwdProxy,
revProxy,
logger,
});
try {
// New node added
Expand Down
2 changes: 1 addition & 1 deletion tests/nodes/NodeManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('NodeManager', () => {
nodeGraph = await NodeGraph.createNodeGraph({
db,
keyManager,
logger: undefined,
logger,
});
nodeConnectionManager =
await NodeConnectionManager.createNodeConnectionManager({
Expand Down

0 comments on commit ca666a0

Please sign in to comment.