Skip to content

Commit

Permalink
Refactored tests and removed list_state references (hyperledger-archi…
Browse files Browse the repository at this point in the history
…ves#22)

* When node's garbage collection is triggered, a callback in clearOnExit
will be called which calls release on the Rust connection object.
Garbage collection should happen with our sdk when garbage collection
happens with node.
Improved Tests

* added comments for the clearOnExit

* Removed the list_state function in connection object because it isn't
implemented in Rust yet.

* fixed connection tests and Connection Interface

* changed vars to camel case. Changed docker file to update npm

* deleted js files in src directory

* changed test because of naming conflicts

* fixed path for one of the imports

* changed tsconfig

* added dist directory. This will be temporary

* added weak library to package.json

* Corrected the test format for index-test.js
Removed the references to cxs_connection_list_state because we aren't using it yet
Fixed paths in connection-test

* added js files from dist. Won't have in the future

* Removed the dist directory from github

Signed-off-by: ryMarsh44 <ryan.marsh44@gmail.com>
  • Loading branch information
rytmarsh authored and hadleym committed Oct 10, 2017
1 parent 4bc88c2 commit a5ddbe8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 84 deletions.
4 changes: 1 addition & 3 deletions cxs/wrappers/node/src/api/connection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as ffi from 'ffi'
import * as ref from 'ref'
import * as Struct from 'ref-struct'
import * as weak from 'weak'
import { CXSRuntime } from '../index'
import { CXSRuntimeConfig, CxsStatus, FFI_CXS_STATUS_PTR } from '../rustlib'
import { CXSRuntimeConfig } from '../rustlib'

import {
IConnections
Expand All @@ -12,7 +11,6 @@ import {
export class Connection implements IConnections {
public connectionHandle: ref.types.uint32
public state: ref.types.uint32
public statusList: any
private RUST_API: ffi

constructor ( path?: string ) {
Expand Down
6 changes: 2 additions & 4 deletions cxs/wrappers/node/src/rustlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export const FFI_CONNECTION_HANDLE = 'uint32'
export const FFI_UNSIGNED_INT = 'uint32'
export const FFI_UNSIGNED_INT_PTR = ref.refType('uint32')
export const FFI_STRING = 'string'
export const FFI_CONFIG_PATH = FFI_STRING
export const FFI_STRING_DATA = 'string'
export const FFI_CONNECTION_HANDLE_PTR = ref.refType(FFI_CONNECTION_HANDLE)
export const FFI_CXS_STATUS_PTR = ref.refType(CxsStatus)

// Rust Lib Native Types
export type rust_did = string
Expand Down Expand Up @@ -48,9 +48,7 @@ export const FFIConfiguration = {
cxs_connection_create: [FFI_ERROR_CODE, [FFI_STRING_DATA, FFI_CONNECTION_HANDLE_PTR]],
cxs_connection_get_data: [FFI_STRING_DATA, [FFI_CONNECTION_HANDLE]],
cxs_connection_get_state: [FFI_ERROR_CODE, [FFI_CONNECTION_HANDLE, FFI_UNSIGNED_INT_PTR]],
cxs_connection_list_state: [FFI_ERROR_CODE, [FFI_CXS_STATUS_PTR]],
cxs_connection_release: [FFI_ERROR_CODE, [FFI_CONNECTION_HANDLE]],

cxs_init: ['int', ['string']]

cxs_init: [FFI_ERROR_CODE, [FFI_CONFIG_PATH]]
}
15 changes: 6 additions & 9 deletions cxs/wrappers/node/test/connection-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
var chai = require('chai');
var fs = require('fs-extra');
var ref = require('ref')
var Struct = require('ref-struct')
var parentDir = require('path');
var currentDir = parentDir.dirname(module.filename);
var Connection = require(parentDir.dirname(currentDir) + '/dist/api/connection').Connection
Expand Down Expand Up @@ -51,19 +48,19 @@ describe('A Connection object with ', function () {
connection.create("dog, cat, man")
var data = connection.getData()
var jsonData = JSON.parse(data)
assert.notEqual(data, "")
assert.notEqual(data, null)
assert.equal(jsonData.handle, connection.connectionHandle)
})

it('a call to get_data where connection doesnt exist should return an empty string', function () {
it('a call to get_data where connection doesnt exist should return a null value', function () {
assert.equal(connection.getData(), null)
})

it('a call to get_data where connection with connection released should return an empty string', function () {
it('a call to get_data where connection was released should return a null value', function () {
connection.create("info")
assert.equal(connection.connect(), 0)
var data = connection.getData()
assert.notEqual(data, "")
assert.notEqual(data, null)
assert.equal(connection.release(), 0)
data = connection.getData()
assert.equal(data, null)
Expand Down Expand Up @@ -117,13 +114,13 @@ describe('A Connection object with ', function () {
return predicate()
}

it('connection and GC deletes object should return empty whet get_data is called ', function () {
it('connection and GC deletes object should return null whet get_data is called ', function () {
const connection = new Connection(path)
connection.create("msg")
connection.connect()
const getData = connection.RUST_API.cxs_connection_get_data
const handle = connection.connectionHandle
assert.notEqual(connection.getData(handle), "")
assert.notEqual(connection.getData(handle), null)

this.timeout(30000)
delete connection
Expand Down
70 changes: 14 additions & 56 deletions cxs/wrappers/node/test/index-test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
require('chai');
require('fs-extra');
var index = require('../dist/index');
var rustlib = require('../dist/rustlib');
var assert = require('assert');
var parentDir = require('path');
var currentDir = parentDir.dirname(module.filename);
var index = require(parentDir.dirname(currentDir) + '/dist/index')
var rustlib = require(parentDir.dirname(currentDir) + '/dist/rustlib')
var assert = require('assert');
var CXSRuntime = index.CXSRuntime;
var CXSRuntimeConfig = rustlib.CXSRuntimeConfig;
var currentDir = parentDir.dirname(module.filename);
var ref = require('ref')
var Struct = require('ref-struct')


describe('call to cxs_init with provided path', function() {
Expand All @@ -24,74 +23,33 @@ describe('call to cxs_init with provided path', function() {
})
});

// these tests were created to only test that the ffi could be called with each function

describe('call to cxs_connection_create ', function() {
describe('Using the cxs ffi directly ', function() {
var path = parentDir.dirname(currentDir);
path += "/lib/libcxs.so";
var run = new CXSRuntime(new CXSRuntimeConfig(path));
var intPtr = ref.alloc('int')
it('should return 0', function () {

it('a call to cxs_connection_create should return 0', function () {
var intPtr = ref.alloc('int')
assert.equal(run.ffi.cxs_connection_create("dog, cat, man", intPtr), 0)
})
});

describe('call to cxs_connection_connect without the ability to connect', function() {
var path = parentDir.dirname(currentDir);
path += "/lib/libcxs.so";
var run = new CXSRuntime(new CXSRuntimeConfig(path));
it('should return 1', function () {
it('a to cxs_connection_connect without the ability to connect should return 1', function () {
assert.equal(run.ffi.cxs_connection_connect(2), 1001)
})
});

describe('call to cxs_connection_get_data ', function() {
var path = parentDir.dirname(currentDir);
path += "/lib/libcxs.so";
var run = new CXSRuntime(new CXSRuntimeConfig(path));
it('should return 0', function () {
it('a call to cxs_connection_get_data should return 0', function () {
assert.equal(run.ffi.cxs_connection_get_data(2), null)
})
});

describe('call to cxs_connection_get_state ', function() {
var path = parentDir.dirname(currentDir);
path += "/lib/libcxs.so";
var run = new CXSRuntime(new CXSRuntimeConfig(path));
it('should return 0', function () {
it('a call to cxs_connection_get_state should return 0', function () {
var intPtr = ref.alloc('int')
assert.equal(run.ffi.cxs_connection_get_state(2, intPtr), 0)
})
});

describe('call to cxs_connection_release without ability to release ', function() {
var path = parentDir.dirname(currentDir);
path += "/lib/libcxs.so";
var run = new CXSRuntime(new CXSRuntimeConfig(path));
it('should return 1', function () {
it('a call to cxs_connection_release without ability to release should return 1', function() {
assert.equal(run.ffi.cxs_connection_release(2), 1001)
})
});

// var CxsStatus = Struct({
// 'handle': 'int',
// 'status': 'int',
// 'msg': 'string'
// });
//
// var cxsStruct = new CxsStatus({
// 'handle': 8,
// 'status': 9,
// 'msg': "StringMSG"
// });


// describe('call to cxs_connection_list_state ', function() {
// var path = parentDir.dirname(currentDir);
// path += "/lib/libcxs.so";
// var run = new CXSRuntime(new CXSRuntimeConfig(path));
// var structPtr = ref.alloc(cxsStruct)
//
// it('should return 0', function () {
// assert.equal(run.ffi.cxs_connection_list_state(structPtr), 0)
// })
// });
});
24 changes: 12 additions & 12 deletions cxs/wrappers/node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"sourceMap": true,
"outDir": "./dist"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"sourceMap": true,
"outDir": "./dist"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
}

0 comments on commit a5ddbe8

Please sign in to comment.