Exemplo n.º 1
0
        public void AllInvokeMethods_AreThreadSafe()
        {
            // Arrange
            StaticNodeJSService.DisposeServiceProvider(); // In case previous test registered a custom service

            // Act
            var       results    = new ConcurrentQueue <string>();
            const int numThreads = 5;
            var       threads    = new List <Thread>();

            for (int i = 0; i < numThreads; i++)
            {
                var thread = new Thread(() => results.Enqueue(StaticNodeJSService.InvokeFromStringAsync <string>("module.exports = (callback) => callback(null, process.pid);").GetAwaiter().GetResult()));
                threads.Add(thread);
                thread.Start();
            }
            foreach (Thread thread in threads)
            {
                thread.Join();
            }

            // Assert
            Assert.Equal(numThreads, results.Count);
            Assert.Single(results.Distinct()); // All invocations should run in process started by first invocation
        }
Exemplo n.º 2
0
        public async void DisposeServiceProvider_RestartsNodeJSProcess()
        {
            // Arrange
            const string dummyTestVariableName  = "TEST_VARIABLE";
            const string dummyTestVariableValue = "testVariableValue";

            StaticNodeJSService.Configure <NodeJSProcessOptions>(options => options.EnvironmentVariables.Add(dummyTestVariableName, dummyTestVariableValue));
            string result1 = await StaticNodeJSService.
                             InvokeFromStringAsync <string>($"module.exports = (callback) => callback(null, process.env.{dummyTestVariableName});").ConfigureAwait(false);

            // Act
            StaticNodeJSService.DisposeServiceProvider(); // Dispose, environment variable should not be set in the next call

            // Assert
            string result2 = await StaticNodeJSService.
                             InvokeFromStringAsync <string>($"module.exports = (callback) => callback(null, process.env.{dummyTestVariableName});").ConfigureAwait(false);

            Assert.Equal(dummyTestVariableValue, result1);
            Assert.Equal(string.Empty, result2);
        }
        public async void DisposeServiceProvider_DisposesServiceProvider()
        {
            // Arrange
            const string dummyTestVariableName  = "TEST_VARIABLE";
            const string dummyTestVariableValue = "testVariableValue";

            StaticNodeJSService.
            Configure <NodeJSProcessOptions>(options => options.EnvironmentVariables.Add(dummyTestVariableName, dummyTestVariableValue));
            DummyResult initialInvocationResult = await StaticNodeJSService.
                                                  InvokeFromStringAsync <DummyResult>($"module.exports = (callback) => callback(null, {{result: process.env.{dummyTestVariableName}}});").
                                                  ConfigureAwait(false);

            // Act
            StaticNodeJSService.DisposeServiceProvider(); // Dispose, environment variable should not be set in the next call
            DummyResult result = await StaticNodeJSService.
                                 InvokeFromStringAsync <DummyResult>($"module.exports = (callback) => callback(null, {{result: process.env.{dummyTestVariableName}}});").
                                 ConfigureAwait(false);

            // Assert
            Assert.Equal(dummyTestVariableValue, initialInvocationResult.Result);
            Assert.Null(result.Result);
        }