MultiThreadingTest.cs 1.52 KB
Newer Older
1
2
using System;
using System.Diagnostics;
3
4
5
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
6
using System.Threading.Tasks;
7
8
9
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Svg.Exceptions;
10
11
12
13

namespace Svg.UnitTests
{

14
15
16
	[TestClass()]
	public class MultiThreadingTest
	{
17

18
19
20
21
22
23
24
25
26
		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;
		}
27

28
29
30
31
32
		[TestMethod]
		public void TestSingleThread()
		{
			LoadFile();
		}
33

34
35
36
37
38
39
40
41
42
		[TestMethod]
		public void TestMultiThread()
		{
			Parallel.For(0, 10, (x) =>
			{
				LoadFile();
			});			
			Trace.WriteLine("Done");
		}
43

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
		[TestMethod]
		[ExpectedException(typeof(SvgMemoryException))]
		public void SVGGivesMemoryExceptionOnTooManyParallelTest()
		{
			try
			{
				Parallel.For(0, 50, (x) =>
				{
					LoadFile();
				});
			}
			catch (AggregateException ex)
			{
				throw ex.InnerException;
			}
		}
		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");
			}
		}
	}
}