Can I call Node.js function from MsTest?

247 Views Asked by At

I was wanting to create unit tests around my .NET library that hosts Edge.js to make node.js function calls.

It fails with an System.AccessViolationException error and I went on to reproduce the issue by trying to use Edge in a unit test.

Will I be able to do this, or am I not understanding the current limitations?

To reproduce, a simple unit test

using System.Threading.Tasks;
using EdgeJs;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTest {
    [TestClass]
    public class UnitTest1 {
        [TestMethod]
        public void TestMethod1() {
            Start().Wait();
        }
        public static async Task<object> Start() {
            var func = Edge.Func(@"
                return function (data, cb) {
                    cb(null, 'Node.js ' + process.version + ' welcomes ' + data);
                }
            ");
            return await func("MSTEST");
        }
    }
}

and MSTEST hangs and the reported message to the output shows

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at _atexit_helper(IntPtr func, UInt32* __pexit_list_size, (fnptr)** __ponexitend_e, (fnptr)** __ponexitbegin_e)
   at _atexit_m(IntPtr func)
   at ClrFunc.Initialize(Local<v8::Function>* , Func`2 func)
   at ClrFunc.Initialize(FunctionCallbackInfo<v8::Value>* info)
   at initializeClrFunc(FunctionCallbackInfo<v8::Value>* info)
   at Nan.imp.?A0x8dda69af.FunctionCallbackWrapper(FunctionCallbackInfo<v8::Value>* info)
   at EdgeJs.Edge.NodeStartx86(Int32 argc, String[] argv)
   at EdgeJs.Edge.<>c__DisplayClass11_0.<Func>b__0()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
   at _atexit_helper(IntPtr func, UInt32* __pexit_list_size, (fnptr)** __ponexitend_e, (fnptr)** __ponexitbegin_e)
   at _atexit_m(IntPtr func)
   at ClrFunc.Initialize(Local<v8::Function>* , Func`2 func)
   at ClrFunc.Initialize(FunctionCallbackInfo<v8::Value>* info)
   at initializeClrFunc(FunctionCallbackInfo<v8::Value>* info)
   at Nan.imp.?A0x8dda69af.FunctionCallbackWrapper(FunctionCallbackInfo<v8::Value>* info)
   at EdgeJs.Edge.NodeStartx86(Int32 argc, String[] argv)
   at EdgeJs.Edge.<>c__DisplayClass11_0.<Func>b__0()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
0

There are 0 best solutions below