Esempio n. 1
0
        public async Task UpdateFileAndRestart()
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            var fixture = new NodeEndToEndTests.TestFixture();
            var blob1   = UpdateOutputName("testblob", "first", fixture);

            await fixture.Host.StopAsync();

            var config = fixture.Host.ScriptConfig;

            ExceptionDispatchInfo exception = null;

            using (var manager = new ScriptHostManager(config))
            {
                // Background task to run while the main thread is pumping events at RunAndBlock().
                Thread t = new Thread(_ =>
                {
                    // don't start until the manager is running
                    TestHelpers.Await(() => manager.State == ScriptHostState.Running).Wait();

                    try
                    {
                        // Wait for initial execution.
                        TestHelpers.Await(() =>
                        {
                            bool exists = blob1.Exists();
                            return(exists);
                        }, timeout: 10 * 1000).Wait();

                        // This changes the bindings so that we now write to blob2
                        var blob2 = UpdateOutputName("first", "second", fixture);

                        // wait for newly executed
                        TestHelpers.Await(() =>
                        {
                            bool exists = blob2.Exists();
                            return(exists);
                        }, timeout: 30 * 1000).Wait();
                    }
                    catch (Exception ex)
                    {
                        exception = ExceptionDispatchInfo.Capture(ex);
                    }

                    cts.Cancel();
                });
                t.Start();

                manager.RunAndBlock(cts.Token);

                t.Join();

                Assert.True(exception == null, exception?.SourceException?.ToString());
            }
        }
        public async Task UpdateFileAndRestart()
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            var fixture = new NodeEndToEndTests.TestFixture();
            var blob1   = UpdateOutputName("testblob", "first", fixture);

            await fixture.Host.StopAsync();

            var config = fixture.Host.ScriptConfig;

            var success = true;

            using (var manager = new ScriptHostManager(config))
            {
                // Background task to run while the main thread is pumping events at RunAndBlock().
                Thread t = new Thread(_ =>
                {
                    try
                    {
                        // Wait for initial execution.
                        TestHelpers.Await(() =>
                        {
                            return(blob1.Exists());
                        }, timeout: 10 * 1000).Wait();

                        // This changes the bindings so that we now write to blob2
                        var blob2 = UpdateOutputName("first", "second", fixture);

                        // wait for newly executed
                        TestHelpers.Await(() =>
                        {
                            return(blob2.Exists());
                        }, timeout: 30 * 1000).Wait();
                    }
                    catch
                    {
                        success = false;
                    }

                    cts.Cancel();
                });
                t.Start();

                manager.RunAndBlock(cts.Token);

                t.Join();

                Assert.True(success);
            }
        }
Esempio n. 3
0
        public async Task RenameFunctionAndRestart()
        {
            var oldDirectory = Path.Combine(Directory.GetCurrentDirectory(), "TestScripts/Node/TimerTrigger");
            var newDirectory = Path.Combine(Directory.GetCurrentDirectory(), "TestScripts/Node/MovedTrigger");

            CancellationTokenSource cts = new CancellationTokenSource();
            var fixture = new NodeEndToEndTests.TestFixture();
            await fixture.Host.StopAsync();

            var config = fixture.Host.ScriptConfig;

            var blob = fixture.TestOutputContainer.GetBlockBlobReference("testblob");

            ExceptionDispatchInfo exception = null;
            var mockEnvironment             = new Mock <IScriptHostEnvironment>();

            using (var eventManager = new ScriptEventManager())
                using (var manager = new ScriptHostManager(config, eventManager, mockEnvironment.Object))
                    using (var resetEvent = new ManualResetEventSlim())
                    {
                        mockEnvironment.Setup(e => e.RestartHost())
                        .Callback(() =>
                        {
                            resetEvent.Set();
                            manager.RestartHost();
                        });

                        // Background task to run while the main thread is pumping events at RunAndBlock().
                        Thread t = new Thread(_ =>
                        {
                            // don't start until the manager is running
                            TestHelpers.Await(() => manager.State == ScriptHostState.Running).Wait();

                            try
                            {
                                // Wait for initial execution.
                                TestHelpers.Await(async() =>
                                {
                                    bool exists = await blob.ExistsAsync();
                                    return(exists);
                                }, timeout: 10 * 1000).Wait();

                                // find __dirname from blob
                                string text;
                                using (var stream = new MemoryStream())
                                {
                                    blob.DownloadToStreamAsync(stream).Wait();
                                    text = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                                }

                                Assert.Contains("TimerTrigger", text);

                                // rename directory & delete old blob
                                Directory.Move(oldDirectory, newDirectory);

                                resetEvent.Wait(TimeSpan.FromSeconds(10));

                                blob.DeleteIfExistsAsync();

                                // wait for newly executed
                                TestHelpers.Await(async() =>
                                {
                                    bool exists = await blob.ExistsAsync();
                                    return(exists);
                                }, timeout: 30 * 1000).Wait();

                                using (var stream = new MemoryStream())
                                {
                                    blob.DownloadToStreamAsync(stream).Wait();
                                    text = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                                }

                                Assert.Contains("MovedTrigger", text);
                            }
                            catch (Exception ex)
                            {
                                exception = ExceptionDispatchInfo.Capture(ex);
                            }
                            finally
                            {
                                try
                                {
                                    Directory.Move(newDirectory, oldDirectory);
                                }
                                catch
                                {
                                }
                            }

                            cts.Cancel();
                        });
                        t.Start();

                        manager.RunAndBlock(cts.Token);

                        t.Join();

                        Assert.True(exception == null, exception?.SourceException?.ToString());
                    }
        }