forked from tjanczuk/edge
/
105_node2net_sync.js
75 lines (66 loc) · 1.94 KB
/
105_node2net_sync.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
var edge = require('../lib/edge.js'), assert = require('assert');
var edgeTestDll = __dirname + '\\Edge.Tests.dll';
describe('sync call from node.js to .net', function () {
it('succeeds for hello world', function () {
var func = edge.func('async (input) => { return ".NET welcomes " + input.ToString(); }');
var result = func('Node.js', true);
assert.equal(result, '.NET welcomes Node.js');
});
it('succeeds for hello world when called sync and async', function (done) {
// create the func
var func = edge.func('async (input) => { return ".NET welcomes " + input.ToString(); }');
// call the func synchronously
var result = func('Node.js', true);
assert.equal(result, '.NET welcomes Node.js');
// call the same func asynchronously
func('Node.js', function (error, result) {
assert.ifError(error);
assert.equal(result, '.NET welcomes Node.js');
done();
});
});
it('successfuly marshals data from node.js to .net', function () {
var func = edge.func({
assemblyFile: edgeTestDll,
methodName: 'MarshalIn'
});
var payload = {
a: 1,
b: 3.1415,
c: 'foo',
d: true,
e: false,
f: new Buffer(10),
g: [ 1, 'foo' ],
h: { a: 'foo', b: 12 },
i: function (payload, callback) { }
};
var result = func(payload, true);
assert.equal(result, 'yes');
});
it('successfuly marshals .net exception thrown on v8 thread from .net to node.js', function () {
var func = edge.func(function() {/*
async (input) =>
{
throw new Exception("Test .NET exception");
}
*/});
assert.throws(
function() { func(null, true); },
/Test .NET exception/
);
});
it('fails if C# method does not complete synchronously', function () {
var func = edge.func(function() {/*
async (input) =>
{
await Task.Delay(1000);
return null;
}
*/});
assert.throws(
function() { func(null, true); },
/The CLR function was declared as synchronous but it returned without completing the Task/
);
});
});