Exemplo n.º 1
0
        public async void SetServices_RestartsNodeJSProcessWithNewServices()
        {
            // Arrange
            const string dummyTestVariableName   = "TEST_VARIABLE_1";
            const string dummyTestVariableValue1 = "testVariableValue1";
            const string dummyTestVariableValue2 = "testVariableValue2";

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

            var dummyServices = new ServiceCollection();

            dummyServices.
            AddNodeJS().
            Configure <NodeJSProcessOptions>(options => options.EnvironmentVariables.Add(dummyTestVariableName, dummyTestVariableValue2));

            // Act
            StaticNodeJSService.SetServices(dummyServices);

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

            Assert.Equal(dummyTestVariableValue1, result1);
            Assert.Equal(dummyTestVariableValue2, result2);
        }
        public async void InvokeFromStringAsync_WithTypeParameter_WithRawStringModule_InvokesFromString()
        {
            // Arrange
            const int             dummyResult             = 1;
            const string          dummyModuleString       = "dummyModuleString";
            const string          dummyNewCacheIdentifier = "dummyNewCacheIdentifier";
            const string          dummyExportName         = "dummyExportName";
            var                   dummyArgs = new object[0];
            var                   dummyCancellationToken = new CancellationToken();
            Mock <INodeJSService> mockNodeJSService      = _mockRepository.Create <INodeJSService>();

            mockNodeJSService.
            Setup(t => t.InvokeFromStringAsync <int>(dummyModuleString, dummyNewCacheIdentifier, dummyExportName, dummyArgs, dummyCancellationToken)).
            ReturnsAsync(dummyResult);
            var dummyServices = new ServiceCollection();

            dummyServices.AddSingleton(typeof(INodeJSService), mockNodeJSService.Object);
            StaticNodeJSService.SetServices(dummyServices);

            // Act
            int result = await StaticNodeJSService.InvokeFromStringAsync <int>(dummyModuleString, dummyNewCacheIdentifier, dummyExportName, dummyArgs, dummyCancellationToken).ConfigureAwait(false);

            // Assert
            Assert.Equal(dummyResult, result);
            _mockRepository.VerifyAll();
        }
        public async void TryInvokeFromCacheAsync_WithTypeParameter_CreatesInvocationRequestAndCallsTryInvokeCoreAsync()
        {
            // Arrange
            const int             dummyResult = 1;
            const string          dummyModuleCacheIdentifier = "dummyModuleCacheIdentifier";
            const string          dummyExportName            = "dummyExportName";
            var                   dummyArgs = new object[0];
            var                   dummyCancellationToken = new CancellationToken();
            Mock <INodeJSService> mockNodeJSService      = _mockRepository.Create <INodeJSService>();

            mockNodeJSService.
            Setup(t => t.TryInvokeFromCacheAsync <int>(dummyModuleCacheIdentifier, dummyExportName, dummyArgs, dummyCancellationToken)).
            ReturnsAsync((true, dummyResult));
            var dummyServices = new ServiceCollection();

            dummyServices.AddSingleton(typeof(INodeJSService), mockNodeJSService.Object);
            StaticNodeJSService.SetServices(dummyServices);

            // Act
            (bool success, int result) = await StaticNodeJSService.TryInvokeFromCacheAsync <int>(dummyModuleCacheIdentifier, dummyExportName, dummyArgs, dummyCancellationToken).ConfigureAwait(false);

            // Assert
            Assert.True(success);
            Assert.Equal(dummyResult, result);
            _mockRepository.VerifyAll();
        }
        public async void InvokeFromFileAsync_WithoutTypeParameter_InvokesFromFile()
        {
            // Arrange
            const string          dummyModulePath        = "dummyModulePath";
            const string          dummyExportName        = "dummyExportName";
            var                   dummyArgs              = new object[0];
            var                   dummyCancellationToken = new CancellationToken();
            Mock <INodeJSService> mockNodeJSService      = _mockRepository.Create <INodeJSService>();

            mockNodeJSService.
            Setup(t => t.InvokeFromFileAsync(dummyModulePath, dummyExportName, dummyArgs, dummyCancellationToken));
            var dummyServices = new ServiceCollection();

            dummyServices.AddSingleton(typeof(INodeJSService), mockNodeJSService.Object);
            StaticNodeJSService.SetServices(dummyServices);

            // Act
            await StaticNodeJSService.InvokeFromFileAsync(dummyModulePath, dummyExportName, dummyArgs, dummyCancellationToken).ConfigureAwait(false);

            // Assert
            _mockRepository.VerifyAll();
        }
        public async void InvokeFromStreamAsync_WithoutTypeParameter_WithModuleFactory_IfModuleIsCachedInvokesFromCacheOtherwiseInvokesFromStream()
        {
            // Arrange
            Func <Stream>         dummyModuleFactory     = () => new MemoryStream();
            const string          dummyCacheIdentifier   = "dummyCacheIdentifier";
            const string          dummyExportName        = "dummyExportName";
            var                   dummyArgs              = new object[0];
            var                   dummyCancellationToken = new CancellationToken();
            Mock <INodeJSService> mockNodeJSService      = _mockRepository.Create <INodeJSService>();

            mockNodeJSService.
            Setup(t => t.InvokeFromStreamAsync(dummyModuleFactory, dummyCacheIdentifier, dummyExportName, dummyArgs, dummyCancellationToken));
            var dummyServices = new ServiceCollection();

            dummyServices.AddSingleton(typeof(INodeJSService), mockNodeJSService.Object);
            StaticNodeJSService.SetServices(dummyServices);

            // Act
            await StaticNodeJSService.InvokeFromStreamAsync(dummyModuleFactory, dummyCacheIdentifier, dummyExportName, dummyArgs, dummyCancellationToken).ConfigureAwait(false);

            // Assert
            _mockRepository.VerifyAll();
        }