Skip to content

Commit

Permalink
#889 add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Oct 20, 2020
1 parent a5620e8 commit d98c57e
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 256 deletions.
Expand Up @@ -23,7 +23,7 @@ import {
EnumerationDefinitionSchema
} from "node-opcua-factory";
import { ExpandedNodeId, makeExpandedNodeId, NodeId, resolveNodeId, sameNodeId } from "node-opcua-nodeid";
import { BrowseDescriptionLike, IBasicSession, ReadValueIdLike } from "node-opcua-pseudo-session";
import { browseAll, BrowseDescriptionLike, IBasicSession, ReadValueIdLike } from "node-opcua-pseudo-session";
import {
createDynamicObjectConstructor,
DataTypeAndEncodingId,
Expand Down Expand Up @@ -64,28 +64,6 @@ async function _readNamespaceUriProperty(session: IBasicSession, dataTypeDiction
return dataValue.value.value || "<not set>";
}

async function browseAll(session: IBasicSession, nodeToBrowse: BrowseDescriptionLike): Promise<BrowseResult>;
async function browseAll(session: IBasicSession, nodesToBrowse: BrowseDescriptionLike[]): Promise<BrowseResult[]>;
async function browseAll(session: IBasicSession, nodesToBrowse: BrowseDescriptionLike[] | BrowseDescriptionLike): Promise<any> {
if (!(nodesToBrowse instanceof Array)) {
return (await browseAll(session, [nodesToBrowse]))[0];
}
if (nodesToBrowse.length === 0) {
return [];
}
const results = await session.browse(nodesToBrowse);

for (const result of results) {
let continuationPoint = result.continuationPoint;
while (continuationPoint) {
debugLog(" Continuation points");
const result2 = await session.browseNext(result.continuationPoint, false);
result.references!.push.apply(result.references, result2.references || []);
continuationPoint = result2.continuationPoint;
}
}
return results;
}
async function _getDataTypeDescriptions(session: IBasicSession, dataTypeDictionaryNodeId: NodeId): Promise<IDataTypeDescription[]> {
const nodeToBrowse2: BrowseDescriptionLike = {
browseDirection: BrowseDirection.Forward,
Expand Down Expand Up @@ -531,6 +509,7 @@ export async function populateDataTypeManager(session: IBasicSession, dataTypeMa

// istanbul ignore next
if (!namespaceArray) {
console.log("session: cannot read Server_NamespaceArray");
// throw new Error("Cannot get Server_NamespaceArray as a array of string");
return;
}
Expand Down
13 changes: 9 additions & 4 deletions packages/node-opcua-client/source/index.ts
Expand Up @@ -19,7 +19,6 @@ export * from "./alarms_and_conditions/client_alarm_tools_acknowledge_all_condit
export * from "./tools/findservers";
export * from "./tools/read_history_server_capabilities";
export * from "./client_utils";

export { assert } from "node-opcua-assert";
export * from "node-opcua-utils";

Expand All @@ -39,8 +38,14 @@ export { hexDump } from "node-opcua-debug";

///
export {
NodeId, resolveNodeId, makeNodeId, coerceNodeId, sameNodeId,
ExpandedNodeId, makeExpandedNodeId, coerceExpandedNodeId
NodeId,
resolveNodeId,
makeNodeId,
coerceNodeId,
sameNodeId,
ExpandedNodeId,
makeExpandedNodeId,
coerceExpandedNodeId
} from "node-opcua-nodeid";
export { StatusCode, StatusCodes } from "node-opcua-status-code";
export * from "node-opcua-variant";
Expand All @@ -64,5 +69,5 @@ export * from "node-opcua-service-subscription";
export * from "node-opcua-service-translate-browse-path";
export * from "node-opcua-service-write";
export * from "node-opcua-service-filter";
export { IBasicSession } from "node-opcua-pseudo-session";
export { IBasicSession, browseAll } from "node-opcua-pseudo-session";
export * from "node-opcua-client-dynamic-extension-object";

This file was deleted.

@@ -0,0 +1,115 @@
// tslint:disable:no-var-requires
// tslint:disable:no-empty

import {
BrowseDescriptionLike,
BrowseDirection,
BrowseRequestOptions,
BrowseResult,
ClientSession,
NodeClassMask,
NodeId,
OPCUAClient,
OPCUAServer,
browseAll
} from "node-opcua";
import { spy } from "sinon";

const should = require("should");
const doDebug = false;

// #519_NodeCrawler is not browsing some nodes
// #889
const describe = require("node-opcua-leak-detector").describeWithLeakDetector;
describe("testing browse & browseNext", () => {
let server: OPCUAServer;
let endpointUrl: any;

let port = 20000;
let groupNodeId: NodeId;

before(async () => {
port += 1;

const options = { port };
server = new OPCUAServer(options);

await server.initialize();
const addressSpace = server.engine.addressSpace!;

const group = addressSpace.getOwnNamespace().addObject({
browseName: "Group",
organizedBy: addressSpace.rootFolder.objects
});
for (let i = 0; i < 27; i++) {
addressSpace.getOwnNamespace().addObject({
browseName: "Object" + i,
organizedBy: group
});
}
groupNodeId = group.nodeId;
await server.start();
endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
});

let data: { session: ClientSession; client: OPCUAClient };
beforeEach(async () => {
const client = OPCUAClient.create({});
await client.connect(endpointUrl);
const session = await client.createSession();
// we want a maximum of 10 references per nodes
session.requestedMaxReferencesPerNode = 10;
data = { client, session };
});

afterEach(async () => {
const { session, client } = data;
await session.close();
await client.disconnect();
});
after(async () => {
await server.shutdown();
});

it("should browse all references of a node using browse and browseNext", async () => {
const { session } = data;

const nodeToBrowse: BrowseDescriptionLike = {
browseDirection: BrowseDirection.Forward,
nodeClassMask: NodeClassMask.Object,
nodeId: groupNodeId
};

const result: BrowseResult = await session.browse(nodeToBrowse);
result.references!.length.should.eql(10);

should.exist(result.continuationPoint);

const resultNext1: BrowseResult = await session.browseNext(result.continuationPoint, false);

resultNext1.references!.length.should.eql(10);

should.exist(resultNext1.continuationPoint);

const resultNext2: BrowseResult = await session.browseNext(resultNext1.continuationPoint, false);
resultNext2.references!.length.should.eql(7);
should.not.exist(resultNext2.continuationPoint);
});

it("should browse all references using browseAll ", async () => {
const { session } = data;
const nodeToBrowse: BrowseDescriptionLike = {
browseDirection: BrowseDirection.Forward,
nodeClassMask: NodeClassMask.Object,
nodeId: groupNodeId
};
const browseSpy = spy(session, "browse");
const browseNextSpy = spy(session, "browseNext");

const result = await browseAll(session, nodeToBrowse);
result.references!.length.should.eql(27);

browseSpy.callCount.should.eql(1);
browseNextSpy.callCount.should.eql(2);
});
});

0 comments on commit d98c57e

Please sign in to comment.