MultiThreadingTest.cs 1.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Svg.UnitTests
{

	[TestClass()]
	public class MultiThreadingTest
	{

		private const string TestFile = @"d:\temp\test.svg";
		private const int ExpectedSize = 600000;
		private XmlDocument GetXMLDoc()
		{
			var xmlDoc = new XmlDocument();
			if(!System.IO.File.Exists(TestFile)) { Assert.Inconclusive("Test file missing"); }
			xmlDoc.LoadXml(System.IO.File.ReadAllText(TestFile));
			return xmlDoc;
		}

		[TestMethod]
		public void TestSingleThread()
		{
			LoadFile();
		}

		[TestMethod]
		public void TestMultiThread()
		{
			bool valid = true;
37
			Parallel.For(0, 3, (x) =>
38
			{
39
				LoadFile();
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
			});
			Assert.IsTrue(valid, "One or more of the runs was invalid");
			Trace.WriteLine("Done");
		}
		private void LoadFile()
		{
			var xml = GetXMLDoc();
			Trace.WriteLine("Reading and drawing file");
			SvgDocument d = SvgDocument.Open(xml);
			var b = d.Draw();
			Trace.WriteLine("Done reading file");
			using (var ms = new MemoryStream())
			{
				b.Save(ms, ImageFormat.Png);
				ms.Flush();
				Assert.IsTrue(ms.Length >= ExpectedSize, "File does not match expected minimum size");
			}
		}
	}
}