Esempio n. 1
0
        // Helper to send items to the listener, and return what they collected
        private object[] Run <TFunction>(params FakeQueueData[] items) where TFunction : FunctionsBase, new()
        {
            var activator = new FakeActivator();
            var func1     = new TFunction();

            activator.Add(func1);

            JobHostConfiguration config = new JobHostConfiguration()
            {
                TypeLocator  = new FakeTypeLocator(typeof(TFunction)),
                JobActivator = activator
            };

            FakeQueueClient    client     = new FakeQueueClient();
            IExtensionRegistry extensions = config.GetService <IExtensionRegistry>();

            extensions.RegisterExtension <IExtensionConfigProvider>(client);

            JobHost host = new JobHost(config);

            foreach (var item in items)
            {
                client.AddAsync(item).Wait();
            }

            host.Start();
            TestHelpers.WaitOne(func1._stopEvent);
            host.Stop();

            return(func1._collected.ToArray());
        }
Esempio n. 2
0
        private object[] Run <TFunction>(FakeQueueClient client, params FakeQueueData[] items) where TFunction : FunctionsBase, new()
        {
            var activator = new FakeActivator();
            var func1     = new TFunction();

            activator.Add(func1);

            var host = TestHelpers.NewJobHost <TFunction>(client, activator);

            foreach (var item in items)
            {
                client.AddAsync(item).Wait();
            }

            host.Start();
            TestHelpers.WaitOne(func1._stopEvent);
            host.Stop();

            // Add any items sent using [FakeQueue(Prefix=...)]
            foreach (var kv in client._prefixedItems)
            {
                func1._collected.AddRange(kv.Value);
            }

            return(func1._collected.ToArray());
        }
        public async Task Test()
        {
            var prog      = new MyProg();
            var activator = new FakeActivator();

            activator.Add(prog);
            var logger = new MyLogger();
            var host   = TestHelpers.NewJobHost <MyProg>(activator, logger);

            // Invoke with method Info
            var method = prog.GetType().GetMethod("Test");

            host.Call(method);
            prog.AssertValid();
            logger.AssertValid();

            // Invoke with new name.
            await host.CallAsync(MyProg.NewName);

            prog.AssertValid();
            logger.AssertValid();

            // Invoke with original name fails
            await Assert.ThrowsAsync <InvalidOperationException>(async() => await host.CallAsync("Test"));

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await host.CallAsync("MyProg.Test"));
        }
        public void Test()
        {
            MyProg prog      = new MyProg();
            var    activator = new FakeActivator();

            activator.Add(prog);

            JobHostConfiguration config = TestHelpers.NewConfig <MyProg>(activator);

            var ext = new TestExtension();

            config.AddExtension(ext);

            var host = new TestJobHost <MyProg>(config);
            IJobHostMetadataProvider metadataProvider = host.CreateMetadataProvider();

            Assert.Equal(1, ext._counter);

            // Callable
            host.Call("Test");
            Assert.Equal(1, ext._counter);

            // Fact that we registered a Widget converter is enough to add the assembly
            Assembly asm;
            bool     resolved;

            resolved = metadataProvider.TryResolveAssembly(typeof(Widget).Assembly.GetName().Name, out asm);
            Assert.True(resolved);
            Assert.Same(asm, typeof(Widget).Assembly);

            // check with full name
            resolved = metadataProvider.TryResolveAssembly(typeof(Widget).Assembly.GetName().FullName, out asm);
            Assert.True(resolved);
            Assert.Same(asm, typeof(Widget).Assembly);

            // This requires the target attribute to be unique within the assembly.
            var attrType = metadataProvider.GetAttributeTypeFromName("Test9");

            Assert.Equal(typeof(Test9Attribute), attrType);

            // JObject --> Attribute
            var attr = GetAttr <Test9Attribute>(metadataProvider, new { Flag = "xyz" });

            Assert.Equal("xyz", attr.Flag);

            // Getting default type.
            var defaultType = metadataProvider.GetDefaultType(attr, FileAccess.Read, null);

            Assert.Equal(typeof(JObject), defaultType);

            // If we have no match for output, we'll try IAsyncCollector<string>
            Assert.Equal(typeof(IAsyncCollector <string>), metadataProvider.GetDefaultType(attr, FileAccess.Write, typeof(object)));
        }
Esempio n. 5
0
        public void Test()
        {
            MyProg prog      = new MyProg();
            var    activator = new FakeActivator();

            activator.Add(prog);

            JobHostConfiguration config = TestHelpers.NewConfig <MyProg>(activator);

            var ext = new TestExtension();

            config.AddExtension(ext);

            IJobHostMetadataProvider tooling = config.CreateMetadataProvider();

            Assert.Equal(1, ext._counter);

            // Callable
            var host = new TestJobHost <MyProg>(config);

            host.Call("Test");
            Assert.Equal(1, ext._counter);

            // Fact that we registered a Widget converter is enough to add the assembly
            Assembly asm;
            bool     resolved;

            resolved = tooling.TryResolveAssembly(typeof(Widget).Assembly.GetName().Name, out asm);
            Assert.True(resolved);
            Assert.Same(asm, typeof(Widget).Assembly);

            // check with full name
            resolved = tooling.TryResolveAssembly(typeof(Widget).Assembly.GetName().FullName, out asm);
            Assert.True(resolved);
            Assert.Same(asm, typeof(Widget).Assembly);

            var attrType = tooling.GetAttributeTypeFromName("Test");

            Assert.Equal(typeof(TestAttribute), attrType);

            // JObject --> Attribute
            var attr = GetAttr <TestAttribute>(tooling, new { Flag = "xyz" });

            Assert.Equal("xyz", attr.Flag);

            // Getting default type.
            var defaultType = tooling.GetDefaultType(attr, FileAccess.Read, null);

            Assert.Equal(typeof(JObject), defaultType);

            Assert.Throws <InvalidOperationException>(() => tooling.GetDefaultType(attr, FileAccess.Write, typeof(object)));
        }
        public void TestSkip()
        {
            var prog = new Program();
            var jobActivator = new FakeActivator();
            jobActivator.Add(prog);

            var nr = new FakeNameResolver().Add("x", "false");
            var host = TestHelpers.NewJobHost<Program>(nr, jobActivator, new FakeExtClient());
            host.Call("Func");

            // Skipped first rule, applied second 
            Assert.Equal(prog._value, "xxx");
        }
Esempio n. 7
0
        public void Test()
        {
            var prog         = new Program();
            var jobActivator = new FakeActivator();

            jobActivator.Add(prog);

            var host = TestHelpers.NewJobHost <Program>(jobActivator, new FakeExtClient());

            host.Call("Func", new { k = 1 });

            // Skipped first rule, applied second
            Assert.Equal(prog._value, "abc-1");
        }
        // Glue to initialize a JobHost with the correct config and invoke the Test method.
        // Config also has the program on it.
        private void TestWorker <TConfig>() where TConfig : IExtensionConfigProvider, ITest <TConfig>, new()
        {
            var prog         = new TConfig();
            var jobActivator = new FakeActivator();

            jobActivator.Add(prog);

            IExtensionConfigProvider ext = prog;
            var host = TestHelpers.NewJobHost <TConfig>(jobActivator, ext);

            ITest <TConfig> test = prog;

            test.Test(host);
        }
Esempio n. 9
0
        public void TestSuccessNull()
        {
            var prog         = new Program();
            var jobActivator = new FakeActivator();

            jobActivator.Add(prog);

            var nr   = new FakeNameResolver().Add("x", "something");
            var host = TestHelpers.NewJobHost <Program>(nr, jobActivator, new FakeExtClient());

            host.Call(nameof(Program.FuncNull));

            // Skipped first rule, applied second
            Assert.Equal(prog._value, "xxx");
        }
Esempio n. 10
0
        public JobHostConfiguration NewConfig <TProgram>(TProgram program, params object[] services)
        {
            JobHostConfiguration config = new JobHostConfiguration();

            var activator = new FakeActivator();

            activator.Add(program);
            config.TypeLocator  = new FakeTypeLocator(typeof(TProgram));
            config.JobActivator = activator;

            config.AddServices(services);
            config.AddServices(_nameResolver);
            config.AddService <IWebJobsExceptionHandler>(new TestExceptionHandler());
            return(config);
        }
Esempio n. 11
0
        // Helper to invoke the method with the given parameters
        private async Task <string> Invoke <TFunction>(object arguments) where TFunction : FunctionBase, new()
        {
            var       activator    = new FakeActivator();
            TFunction testInstance = new TFunction();

            activator.Add(testInstance);

            FakeExtClient client = new FakeExtClient();

            var host = TestHelpers.NewJobHost <TFunction>(activator, client);

            await host.CallAsync("Func", arguments);

            var x = testInstance._sb.ToString();

            return(x);
        }
        public IHostBuilder NewBuilder <TProgram>(TProgram program, Action <IWebJobsBuilder> configure = null)
        {
            var activator = new FakeActivator();

            activator.Add(program);

            return(new HostBuilder()
                   .ConfigureDefaultTestHost <TProgram>(b =>
            {
                b.AddAzureStorageBlobs().AddAzureStorageQueues();
                configure?.Invoke(b);
            })
                   .ConfigureServices(services =>
            {
                services.AddSingleton <IJobActivator>(activator);
                services.AddSingleton <INameResolver>(_nameResolver);
            }));
        }
        // Glue to initialize a JobHost with the correct config and invoke the Test method.
        // Config also has the program on it.
        private void TestWorker <TConfig>() where TConfig : IExtensionConfigProvider, ITest <TConfig>, new()
        {
            var prog         = new TConfig();
            var jobActivator = new FakeActivator();

            jobActivator.Add(prog);

            var appSettings = new FakeNameResolver();

            appSettings.Add("y", "123");

            IExtensionConfigProvider ext = prog;
            var host = TestHelpers.NewJobHost <TConfig>(jobActivator, ext, appSettings);

            ITest <TConfig> test = prog;

            test.Test(host);
        }
        // Glue to initialize a JobHost with the correct config and invoke the Test method.
        // Config also has the program on it.
        private void TestWorker <TConfig>() where TConfig : IExtensionConfigProvider, ITest <TConfig>, new()
        {
            var prog         = new TConfig();
            var jobActivator = new FakeActivator();

            jobActivator.Add(prog);

            IExtensionConfigProvider ext = prog;

            IHost host = new HostBuilder()
                         .ConfigureDefaultTestHost <TConfig>(b =>
            {
                b.AddExtension(ext);
            }, activator: jobActivator)
                         .Build();

            ITest <TConfig> test = prog;

            test.Test(host.GetJobHost <TConfig>());
        }
Esempio n. 15
0
        // Helper to invoke the method with the given parameters
        private async Task <string> Invoke <TFunction>(object arguments) where TFunction : FunctionBase, new()
        {
            var       activator    = new FakeActivator();
            TFunction testInstance = new TFunction();

            activator.Add(testInstance);

            IHost host = new HostBuilder()
                         .ConfigureDefaultTestHost <TFunction>(b =>
            {
                b.AddExtension <FakeExtClient>();
            })
                         .ConfigureServices(services => services.AddSingleton <IJobActivator>(activator))
                         .Build();

            await host.GetJobHost().CallAsync(typeof(TFunction).GetMethod("Func"), arguments);

            var x = testInstance._sb.ToString();

            return(x);
        }
Esempio n. 16
0
        public async Task TestSuccessNull()
        {
            var prog         = new Program();
            var jobActivator = new FakeActivator();

            jobActivator.Add(prog);

            var nr = new FakeNameResolver().Add("x", "something");

            IHost host = new HostBuilder()
                         .ConfigureDefaultTestHost <Program>(b =>
            {
                b.AddExtension <FakeExtClient>();
            }, nr, jobActivator)
                         .Build();

            await host.GetJobHost <Program>().CallAsync(nameof(Program.FuncNull));

            // Skipped first rule, applied second
            Assert.Equal(prog._value, "xxx");
        }
Esempio n. 17
0
        private object[] Run <TFunction>(Action <FakeQueueClient> clientSetup, params FakeQueueData[] items) where TFunction : FunctionsBase, new()
        {
            var activator = new FakeActivator();
            var func1     = new TFunction();

            activator.Add(func1);

            FakeQueueClient fakeClient = null;
            var             host       = new HostBuilder()
                                         .ConfigureDefaultTestHost <TFunction>(b => { }, activator: activator)
                                         .ConfigureServices(services =>
            {
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IExtensionConfigProvider, FakeQueueClient>(p =>
                {
                    fakeClient = new FakeQueueClient(p.GetService <INameResolver>(), p.GetService <IConverterManager>());
                    clientSetup?.Invoke(fakeClient);
                    foreach (var item in items)
                    {
                        fakeClient.AddAsync(item).Wait();
                    }
                    return(fakeClient);
                }));
            })
                                         .Build();

            host.Start();
            TestHelpers.WaitOne(func1._stopEvent);
            host.StopAsync().GetAwaiter().GetResult();

            // Add any items sent using [FakeQueue(Prefix=...)]
            foreach (var kv in fakeClient._prefixedItems)
            {
                func1._collected.AddRange(kv.Value);
            }

            return(func1._collected.ToArray());
        }
        public async Task Test()
        {
            var prog      = new MyProg();
            var activator = new FakeActivator();

            activator.Add(prog);
            var logger = new MyLogger();

            IHost host = new HostBuilder()
                         .ConfigureDefaultTestHost <MyProg>(_ => { }, activator: activator)
                         .ConfigureServices(services =>
            {
                services.AddSingleton <IAsyncCollector <FunctionInstanceLogEntry> >(logger);
            })
                         .Build();

            var jobHost = host.GetJobHost <MyProg>();

            // Invoke with method Info
            var method = prog.GetType().GetMethod("Test");
            await jobHost.CallAsync(method);

            prog.AssertValid();
            logger.AssertValid();

            // Invoke with new name.
            await jobHost.CallAsync(MyProg.NewName);

            prog.AssertValid();
            logger.AssertValid();

            // Invoke with original name fails
            await Assert.ThrowsAsync <InvalidOperationException>(async() => await jobHost.CallAsync("Test"));

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await jobHost.CallAsync("MyProg.Test"));
        }