public void TemplateService_CanParseTemplatesInParallel_WithManualThreadModel()
        {
            var service = new TemplateService();

            const int    threadCount = 10;
            const string template    = "<h1>Hello you are @Model.Age</h1>";

            var threads = new List <Thread>();

            for (int i = 0; i < threadCount; i++)
            {
                // Capture enumerating index here to avoid closure issues.
                int index = i;

                var thread = new Thread(() =>
                {
                    var model = new Person {
                        Age = index
                    };
                    string expected = "<h1>Hello you are " + index + "</h1>";
                    string result   = service.Parse(template, model, null, null);

                    Assert.That(result == expected, "Result does not match expected: " + result);
                });

                threads.Add(thread);
                thread.Start();
            }

            // Block until all threads have joined.
            threads.ForEach(t => t.Join());

            service.Dispose();
        }
        private void AssertAllViewsInAssemblyCanBeCompiled(Assembly assembly)
        {
            var config = new TemplateServiceConfiguration();

            config.BaseTemplateType = typeof(MvcTemplateBase <>);

            var processor = new TemplateService(config);

            var resourceNames = assembly.GetManifestResourceNames();

            foreach (var resource in resourceNames)
            {
                if (resource.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
                {
                    var content = this.GetEmbeddedViewContent(resource, assembly);

                    // The namespace for ChildActionExtensions
                    processor.AddNamespace("System.Web.Mvc");
                    processor.AddNamespace("System.Web.Mvc.Html");

                    processor.AddNamespace("System.Web");

                    this.AssertViewCanBeCompiled(content, resource, processor);
                }
            }

            processor.Dispose();
        }
Esempio n. 3
0
 ///<summary>Releases the unmanaged resources used by the StatementBuilder and optionally releases the managed resources.</summary>
 ///<param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         TemplateService.Dispose();
     }
 }
        public void TemplateService_CanParseTemplatesInParallel_WithThreadPool()
        {
            var service = new TemplateService();

            const int    count    = 10;
            const string template = "<h1>Hello you are @Model.Age</h1>";

            /* As we are leaving the threading to the pool, we need a way of coordinating the execution
             * of the test after the threadpool has done its work. ManualResetEvent instances are the way. */
            var resetEvents = new ManualResetEvent[count];

            for (int i = 0; i < count; i++)
            {
                // Capture enumerating index here to avoid closure issues.
                int index = i;

                string expected = "<h1>Hello you are " + index + "</h1>";
                resetEvents[index] = new ManualResetEvent(false);

                var model = new Person {
                    Age = index
                };
                var item = new ThreadPoolItem <Person>(model, resetEvents[index], m =>
                {
                    string result = service.Parse(template, model, null, null);

                    Assert.That(result == expected, "Result does not match expected: " + result);
                });

                ThreadPool.QueueUserWorkItem(item.ThreadPoolCallback);
            }

            // Block until all events have been set.
            WaitHandle.WaitAll(resetEvents);

            service.Dispose();
        }
        private void AssertAllViewsInAssemblyCanBeCompiled(Assembly assembly)
        {
            var config = new TemplateServiceConfiguration();
            config.BaseTemplateType = typeof(MvcTemplateBase<>);

            var processor = new TemplateService(config);

            var resourceNames = assembly.GetManifestResourceNames();

            foreach (var resource in resourceNames)
            {
                if (resource.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
                {
                    var content = this.GetEmbeddedViewContent(resource, assembly);

                    // The namespace for ChildActionExtensions
                    processor.AddNamespace("System.Web.Mvc.Html");

                    processor.AddNamespace("System.Web");

                    this.AssertViewCanBeCompiled(content, resource, processor);
                }
            }

            processor.Dispose();
        }
        public void TemplateService_CanParseTemplatesInParallel_WithThreadPool()
        {
            var service = new TemplateService();

            const int count = 10;
            const string template = "<h1>Hello you are @Model.Age</h1>";

            /* As we are leaving the threading to the pool, we need a way of coordinating the execution
             * of the test after the threadpool has done its work. ManualResetEvent instances are the way. */
            var resetEvents = new ManualResetEvent[count];
            for (int i = 0; i < count; i++)
            {
                // Capture enumerating index here to avoid closure issues.
                int index = i;

                string expected = "<h1>Hello you are " + index + "</h1>";
                resetEvents[index] = new ManualResetEvent(false);

                var model = new Person { Age = index };
                var item = new ThreadPoolItem<Person>(model, resetEvents[index], m =>
                {
                    string result = service.Parse(template, model);

                    Assert.That(result == expected, "Result does not match expected: " + result);
                });

                ThreadPool.QueueUserWorkItem(item.ThreadPoolCallback);
            }

            // Block until all events have been set.
            WaitHandle.WaitAll(resetEvents);

            service.Dispose();
        }
        public void TemplateService_CanParseTemplatesInParallel_WithManualThreadModel()
        {
            var service = new TemplateService();

            const int threadCount = 10;
            const string template = "<h1>Hello you are @Model.Age</h1>";

            var threads = new List<Thread>();
            for (int i = 0; i < threadCount; i++)
            {
                // Capture enumerating index here to avoid closure issues.
                int index = i;

                var thread = new Thread(() =>
                {
                    var model = new Person { Age = index };
                    string expected = "<h1>Hello you are " + index + "</h1>";
                    string result = service.Parse(template, model);

                    Assert.That(result == expected, "Result does not match expected: " + result);
                });

                threads.Add(thread);
                thread.Start();
            }

            // Block until all threads have joined.
            threads.ForEach(t => t.Join());

            service.Dispose();
        }