コード例 #1
0
ファイル: Program.cs プロジェクト: MiningCat/NetSlimerJS
        static void Main(string[] args)
        {
            var runner = new SlimerJs();
            runner.RunAsync(@"test.js", new[] { @"http://happyworks.ru/netslimerjs/" }, Console.WriteLine).GetAwaiter().GetResult();

            System.Console.Read();
        }
コード例 #2
0
        public void RunScriptAsync_AnyScript_CorrectScriptFileContent(
            Mock<ISlimerJsProcessProvider> slimerJsProcessProvider,
            Mock<ISlimerJsProcces> slimerJsProcces,
            CancellationTokenSource cancellationTokenSource,
            string script)
        {
            //arrange
            string filePath = null;
            var sut = new SlimerJs(new SlimerJsSettings(), slimerJsProcessProvider.Object);
            slimerJsProcces.Setup(p => p.RunAsync(It.IsAny<string>(), It.IsAny<Action<string>>()))
                .Returns(Task.Delay(TimeSpan.FromMinutes(5), cancellationTokenSource.Token))
                .Callback<string, Action<string>>((arg, sub) => { filePath = arg; });
            slimerJsProcessProvider.Setup(p => p.Create(It.IsAny<SlimerJsSettings>())).Returns(slimerJsProcces.Object);

            //act
            var task = sut.RunScriptAsync(script);
            Task.Factory.StartNew(() => task).Unwrap();

            //assert
            filePath.Should().NotBeNullOrEmpty();
            filePath = filePath.Replace("\"", "");
            File.Exists(filePath).Should().BeTrue();
            File.ReadAllText(filePath).Should().Be(script);
            cancellationTokenSource.Cancel();
        }
コード例 #3
0
        public async void RunScriptAsync_AnyScript_FileDeletedAfterCompletion(
            Mock<ISlimerJsProcessProvider> slimerJsProcessProvider,
            Mock<ISlimerJsProcces> slimerJsProcces,
            string script)
        {
            //arrange
            string filePath = null;
            var sut = new SlimerJs(new SlimerJsSettings(), slimerJsProcessProvider.Object);
            slimerJsProcces.Setup(p => p.RunAsync(It.IsAny<string>(), It.IsAny<Action<string>>()))
                .Returns(Task.FromResult(0))
                .Callback<string, Action<string>>((arg, sub) => { filePath = arg; });
            slimerJsProcessProvider.Setup(p => p.Create(It.IsAny<SlimerJsSettings>())).Returns(slimerJsProcces.Object);

            //act
            await sut.RunScriptAsync(script);

            //assert
            filePath.Should().NotBeNullOrEmpty();
            File.Exists(filePath.Replace("\"", "")).Should().BeFalse();
        }
コード例 #4
0
        public void RunScriptAsync_ManyInstances_WorkCorrectly(SlimerJs sut)
        {
            var tasks = Enumerable.Range(0, 10)
                .Select(
                    i => new Task((async () => await sut.RunScriptAsync($"console.log('{i}'); slimer.exit();"))))
                .ToArray();

            foreach (var task in tasks)
            {
                task.Start();
            }
            Task.WaitAll(tasks);

            tasks.Select(a => a.Status).Should().OnlyContain(s => s == TaskStatus.RanToCompletion);
        }