public void TryConvertJson_JsonObjectString()
        {
            JObject child = new JObject
            {
                { "Name", "Mary" },
                { "Location", "Seattle" },
                { "Age", 5 }
            };

            JObject parent = new JObject
            {
                { "Name", "Bob" },
                { "Location", "Seattle" },
                { "Age", 40 },
                { "Children", new JArray(child) }
            };

            object result;
            string input      = parent.ToString();
            bool   didConvert = NodeFunctionInvoker.TryConvertJson(input, out result);

            Assert.True(didConvert);

            var resultDictionary = (IDictionary <string, object>)result;
            var resultChildren   = (IEnumerable <object>)resultDictionary["Children"];
            var resultChild      = (IDictionary <string, object>)resultChildren.ElementAt(0);

            Assert.Equal(5, (long)resultChild["Age"]);
        }
示例#2
0
        public void TryConvertJson_ArrayOfJsonObjectStrings()
        {
            string[] input = new string[]
            {
                "{ \"name\": \"Larry\" }",
                "{ \"name\": \"Moe\" }",
                "{ \"name\": \"Curly\" }"
            };

            object result     = null;
            bool   didConvert = NodeFunctionInvoker.TryConvertJson(input, out result);

            Assert.True(didConvert);

            object[] objects = result as object[];
            Dictionary <string, object> obj = (Dictionary <string, object>)objects[0];

            Assert.Equal("Larry", obj["name"]);

            obj = (Dictionary <string, object>)objects[1];
            Assert.Equal("Moe", obj["name"]);

            obj = (Dictionary <string, object>)objects[2];
            Assert.Equal("Curly", obj["name"]);
        }
        private void RestartHost()
        {
            // signal host restart
            _restartEvent.Set();

            // whenever we're restarting the host, we want to let the Node
            // invoker know so it can clear the require cache, etc.
            NodeFunctionInvoker.OnHostRestart();
        }
        public void ToDictionary_ReturnsExpectedResult()
        {
            var test = new TestClass
            {
                Integer = 123,
                String  = "Testing",
                Object  = new TestClass()
            };
            var result = NodeFunctionInvoker.ToDictionary(test);

            Assert.Equal(test.Integer, result["integer"]);
            Assert.Equal(test.String, result["string"]);
        }
示例#5
0
        public void ConvertBindingValue_PerformsExpectedConversions()
        {
            var c = new ExpandoObject() as IDictionary <string, object>;

            c["A"] = "Testing";
            c["B"] = 1234;
            c["C"] = new object[] { 1, "Two", 3 };

            // don't expect functions to be serialized
            c["D"] = (Func <object, Task <object> >)(p => { return(Task.FromResult <object>(null)); });

            var o = new ExpandoObject() as IDictionary <string, object>;

            o["A"] = "Testing";
            o["B"] = c;

            string json = (string)NodeFunctionInvoker.ConvertBindingValue(o);

            Assert.Equal("{\"A\":\"Testing\",\"B\":{\"A\":\"Testing\",\"B\":1234,\"C\":[1,\"Two\",3]}}", json.Replace(" ", string.Empty));
        }
示例#6
0
 public static async Task WarmUp(ScriptHost host)
 {
     // exercise the Node pipeline
     await NodeFunctionInvoker.InitializeAsync();
 }
        public static async Task WarmUp(WebHostSettings settings, IScriptEventManager eventManager)
        {
            var        traceWriter = new FileTraceWriter(Path.Combine(settings.LogPath, "Host"), TraceLevel.Info);
            ScriptHost host        = null;

            try
            {
                traceWriter.Info("Warm up started");

                string rootPath = settings.ScriptPath;
                if (Directory.Exists(rootPath))
                {
                    Directory.Delete(rootPath, true);
                }
                Directory.CreateDirectory(rootPath);

                string content = ReadResourceString("Functions.host.json");
                File.WriteAllText(Path.Combine(rootPath, "host.json"), content);

                // read in the C# function
                string functionPath = Path.Combine(rootPath, "Test-CSharp");
                Directory.CreateDirectory(functionPath);
                content = ReadResourceString("Functions.Test_CSharp.function.json");
                File.WriteAllText(Path.Combine(functionPath, "function.json"), content);
                content = ReadResourceString("Functions.Test_CSharp.run.csx");
                File.WriteAllText(Path.Combine(functionPath, "run.csx"), content);

                traceWriter.Info("Warm up functions deployed");

                ScriptHostConfiguration config = new ScriptHostConfiguration
                {
                    RootScriptPath      = rootPath,
                    FileLoggingMode     = FileLoggingMode.Never,
                    RootLogPath         = settings.LogPath,
                    TraceWriter         = traceWriter,
                    FileWatchingEnabled = false
                };
                config.HostConfig.StorageConnectionString   = null;
                config.HostConfig.DashboardConnectionString = null;

                host = ScriptHost.Create(new NullScriptHostEnvironment(), eventManager, config, ScriptSettingsManager.Instance);
                traceWriter.Info(string.Format("Starting Host (Id={0})", host.ScriptConfig.HostConfig.HostId));

                host.Start();

                var arguments = new Dictionary <string, object>
                {
                    { "input", "{}" }
                };
                host.CallAsync("Test-CSharp", arguments).Wait();
                host.Stop();

                await NodeFunctionInvoker.InitializeAsync();

                traceWriter.Info("Warm up succeeded");
            }
            catch (Exception ex)
            {
                traceWriter.Error(string.Format("Warm up failed: {0}", ex));
            }
            finally
            {
                host?.Dispose();
                traceWriter.Dispose();
            }
        }
        public void NormalizeBindingData_TypeHandling()
        {
            var inputObject1 = new Dictionary <string, object>
            {
                { "TestProp1", "value1" },
                { "TestProp2", 123 }
            };
            var dateTime     = DateTime.UtcNow;
            var inputObject2 = new Dictionary <string, object>
            {
                { "TestProp1", "value1" },
                { "TestProp2", 456 },
                { "TestProp3", new string[] { "value1", "value2", "value3" } },
                { "TestProp4", dateTime }
            };
            var objectArray = new Dictionary <string, object>[] { inputObject1, inputObject2 };
            var bindingData = new Dictionary <string, object>
            {
                { "TestProp1", "value1" },
                { "TestProp2", "value2" },
                { "TestProp3", inputObject1 },
                { "TestProp4", inputObject2 },
                { "TestProp5", objectArray },
                { "TestProp6", null },
                { "TestProp7", new BlobProperties {
                      ContentType = "application/json", ContentMD5 = "xyz"
                  } },
                { "TestProp8", new Uri("http://microsoft.com") },
                { "TestProp9", new PartitionContext {
                      EventHubPath = "myhub", ConsumerGroupName = "Default"
                  } }
            };
            var result = NodeFunctionInvoker.NormalizeBindingData(bindingData);

            Assert.Equal(9, result.Count);
            Assert.Equal(bindingData["TestProp1"], result["testProp1"]);
            Assert.Equal(bindingData["TestProp2"], result["testProp2"]);

            var blobProperties = (IDictionary <string, object>)result["testProp7"];

            Assert.Equal(16, blobProperties.Count);
            Assert.Equal("application/json", blobProperties["contentType"]);
            Assert.Equal("xyz", blobProperties["contentMD5"]);

            var partitionContextProperties = (IDictionary <string, object>)result["testProp9"];

            Assert.Equal(2, partitionContextProperties.Count);
            Assert.Equal("myhub", partitionContextProperties["eventHubPath"]);
            Assert.Equal("Default", partitionContextProperties["consumerGroupName"]);

            Assert.Equal("http://microsoft.com/", result["testProp8"].ToString());

            var resultChild = (IDictionary <string, object>)result["testProp3"];

            Assert.Equal(inputObject1["TestProp1"], resultChild["testProp1"]);
            Assert.Equal(inputObject1["TestProp2"], resultChild["testProp2"]);

            resultChild = (IDictionary <string, object>)result["testProp4"];
            Assert.Equal(inputObject2["TestProp1"], resultChild["testProp1"]);
            Assert.Equal(inputObject2["TestProp2"], resultChild["testProp2"]);
            var resultArray = (string[])resultChild["testProp3"];

            Assert.Equal(3, resultArray.Length);
            Assert.Equal("value1", resultArray[0]);
            Assert.Equal("value2", resultArray[1]);
            Assert.Equal("value3", resultArray[2]);
            Assert.Equal(dateTime, resultChild["testProp4"]);

            var resultObjectArray = (Dictionary <string, object>[])result["testProp5"];

            Assert.Equal(456, resultObjectArray[1]["testProp2"]);
        }
 public void IsEdgeSupportedType_ReturnsExpectedResult(Type type, bool expected)
 {
     Assert.Equal(expected, NodeFunctionInvoker.IsEdgeSupportedType(type));
 }