forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadingTest.cs
More file actions
361 lines (317 loc) · 12.5 KB
/
ThreadingTest.cs
File metadata and controls
361 lines (317 loc) · 12.5 KB
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
37
38
39
40
41
42
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using GeoAPI.Geometries;
using NUnit.Framework;
using NetTopologySuite.Geometries;
using SharpMap.Data;
using SharpMap.Data.Providers;
using SharpMap.Utilities.Indexing;
namespace UnitTests.Data.Providers
{
public class ShapeFileWithMemoryCacheThreadingTest : ThreadingTest
{
private static string TestDataPath
{
get => TestUtility.GetPathToTestFile("roads_ugl.shp");
}
public ShapeFileWithMemoryCacheThreadingTest()
: base(new ShapeFile(TestDataPath, false, true))
{
}
[Test, Description("Simulates two threads using the same provider at the same time.")]
public void TestTwoOpenClose()
{
//Simulates two threads using the same provider at the same time..
var provider = new ShapeFile(TestDataPath, false, true);
provider.Open();
provider.Open();
provider.GetGeometriesInView(GetRandomEnvelope());
provider.Close();
provider.GetGeometriesInView(GetRandomEnvelope());
provider.Close();
}
[Test, Description("Simulates two threads using the datasource with different providers at the same time.")]
public void TestTwoThreadsUsingDifferentProviders()
{
var provider1 = new ShapeFile(TestDataPath, false, true);
var provider2 = new ShapeFile(TestDataPath, false, true);
provider1.Open();
provider2.Open();
provider1.GetGeometriesInView(GetRandomEnvelope());
provider1.Close();
provider2.GetGeometriesInView(GetRandomEnvelope());
provider2.Close();
}
}
public class ShapeFileThreadingTest : ThreadingTest
{
internal static string TestDataPath
{
get { return TestUtility.GetPathToTestFile("roads_ugl.shp"); }
}
public override void OneTimeSetUp()
{
base.OneTimeSetUp();
ShapeFile.SpatialIndexFactory = new SharpSbnIndexFactory();
}
public ShapeFileThreadingTest()
: base(new ShapeFile(TestDataPath, true, false))
{
}
[Test, Description("Simulates two threads using the same provider at the same time.")]
public void TestTwoOpenClose()
{
//Simulates two threads using the same provider at the same time..
var provider = new ShapeFile(TestDataPath, true, false);
provider.Open();
provider.Open();
provider.GetGeometriesInView(GetRandomEnvelope());
provider.Close();
provider.GetGeometriesInView(GetRandomEnvelope());
provider.Close();
}
[Test, Description("Simulates two threads using the datasource with different providers at the same time.")]
public void TestTwoThreadsUsingDifferentProviders()
{
var provider1 = new ShapeFile(TestDataPath, true, false);
var provider2 = new ShapeFile(TestDataPath, true, false);
provider1.Open();
provider2.Open();
provider1.GetGeometriesInView(GetRandomEnvelope());
provider1.Close();
provider2.GetGeometriesInView(GetRandomEnvelope());
provider2.Close();
}
}
//[Ignore("Only run if you have a proper postgis connection")]
public class PostGisThreadingTest : ThreadingTest
{
public PostGisThreadingTest()
: base(Serialization.ProviderTest.CreateProvider<PostGIS>())
{
}
}
#if LINUX
[Ignore("Only run if you have a proper ManagedSpatiaLite connection")]
#endif
public class ManagedSpatiaLiteThreadingTest : ThreadingTest
{
public ManagedSpatiaLiteThreadingTest()
: base(Serialization.ProviderTest.CreateProvider<ManagedSpatiaLite>())
{
}
}
[Ignore("Only run if you have a proper sqlserver2008 connection")]
public class SqlSever2008ThreadingTest : ThreadingTest
{
public SqlSever2008ThreadingTest()
: base(Serialization.ProviderTest.CreateProvider<SqlServer2008>())
{
}
}
[TestFixture]
public abstract class ThreadingTest
{
//private static readonly ILog Logger = LogManager.GetLogger(typeof(ThreadingTest));
static ThreadingTest ()
{
GeoAPI.GeometryServiceProvider.Instance =
NetTopologySuite.NtsGeometryServices.Instance;
}
private const int NumberOfTests = 200;
private readonly IProvider _provider;
private readonly Envelope _extents;
private readonly Random _rnd = new Random(6584);
private readonly List<bool> _testsRun = new List<bool>(NumberOfTests);
private readonly List<Exception> _testsFailed = new List<Exception>(NumberOfTests);
protected ThreadingTest(IProvider provider)
{
_provider = provider;
_provider.Open();
_extents = provider.GetExtents();
_provider.Close();
}
[OneTimeSetUp]
public virtual void OneTimeSetUp()
{
}
protected Envelope GetRandomEnvelope()
{
var minX = NextRandom(-0.1, 0.5, _extents.MinX, _extents.Width);
var minY = NextRandom(-0.1, 0.5, _extents.MinY, _extents.Height);
var maxX = NextRandom(-0.5, 0.1, _extents.MaxX, _extents.Width);
var maxY = NextRandom(-0.5, 0.1, _extents.MaxY, _extents.Height);
return new Envelope(minX, maxX, minY, maxY);
}
private double NextRandom(double min, double max, double start, double amount)
{
var d = max - min;
return start + min * amount + d*amount*_rnd.NextDouble();
}
[Test]
public void TestGetGeometriesInView()
{
Assert.DoesNotThrow(() => RunTest(0));
}
[Test]
public void TestExecuteIntersectionQueryEnvelope()
{
Assert.DoesNotThrow(() => RunTest(1));
}
[Test]
public void TestExecuteIntersectionQueryGeometry()
{
Assert.DoesNotThrow(() => RunTest(2));
}
[Test]
public void TestMixed()
{
Assert.DoesNotThrow(() => RunTest(3));
}
public void RunTest(int kind)
{
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
_provider.Open();
_testsRun.Clear();
_testsFailed.Clear();
for (var i = 0; i < NumberOfTests; i++)
{
_testsRun.Add(false);
_testsFailed.Add(null);
switch (kind)
{
case 0:
ThreadPool.QueueUserWorkItem(ExecuteGetGeometriesInView, i);
break;
case 1:
ThreadPool.QueueUserWorkItem(ExecuteExecuteFeatureQueryEnvelope, i);
break;
case 2:
ThreadPool.QueueUserWorkItem(ExecuteExecuteFeatureQueryGeometry, i);
break;
case 3:
switch (_rnd.Next(0, 3))
{
case 0:
ThreadPool.QueueUserWorkItem(ExecuteGetGeometriesInView, i);
break;
case 1:
ThreadPool.QueueUserWorkItem(ExecuteExecuteFeatureQueryEnvelope, i);
break;
case 2:
ThreadPool.QueueUserWorkItem(ExecuteExecuteFeatureQueryGeometry, i);
break;
}
break;
default:
throw new NotSupportedException();
}
}
WaitAllRun();
_provider.Close();
foreach (var exception in _testsFailed)
{
if (exception != null)
throw exception;
}
sw.Stop();
System.Diagnostics.Trace.WriteLine($"\nTest performed in {sw.ElapsedMilliseconds}ms");
}
private readonly object _runLock = new object();
private void SignalRun(int id)
{
lock (_runLock)
{
_testsRun[id] = true;
}
}
private void WaitAllRun()
{
while (true)
{
Thread.Sleep(2500);
lock (_runLock)
{
var allRun = true;
foreach (var b in _testsRun)
{
if (!b)
{
allRun = false;
break;
}
}
if (allRun) break;
}
}
}
private void ExecuteGetGeometriesInView(object arguments)
{
var env = GetRandomEnvelope();
System.Diagnostics.Trace.WriteLine(string.Format(
"Thread {0}: {2}. GetGeometriesInView({1})", Thread.CurrentThread.ManagedThreadId, env, arguments));
try
{
var geoms = _provider.GetGeometriesInView(env);
System.Diagnostics.Trace.WriteLine(string.Format(
"Thread {0}: {2}. {1} geometries", Thread.CurrentThread.ManagedThreadId, geoms.Count, arguments));
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(string.Format(
"Thread {0}: {1}. failed:\n{2}", Thread.CurrentThread.ManagedThreadId, arguments, ex.Message));
_testsFailed[(int)arguments] = ex;
}
SignalRun((int)arguments);
}
private void ExecuteExecuteFeatureQueryEnvelope(object arguments)
{
var env = GetRandomEnvelope();
System.Diagnostics.Trace.WriteLine(string.Format(
"Thread {0}: {2}. ExecuteFeatureQuery with ({1})", Thread.CurrentThread.ManagedThreadId, env, arguments));
try
{
var fds = new FeatureDataSet();
_provider.ExecuteIntersectionQuery(env, fds);
System.Diagnostics.Trace.WriteLine(string.Format(
"Thread {0}: {2}. {1} features", Thread.CurrentThread.ManagedThreadId, fds.Tables[0].Count, arguments));
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(string.Format(
"Thread {0}: {1}. failed:\n{2}", Thread.CurrentThread.ManagedThreadId, arguments, ex.Message));
_testsFailed[(int)arguments] = ex;
}
SignalRun((int)arguments);
}
private void ExecuteExecuteFeatureQueryGeometry(object arguments)
{
var env = GetRandomEnvelope();
var geom = GeometryFactory.Default.ToGeometry(env);
System.Diagnostics.Trace.WriteLine(string.Format(
"Thread {0}: {2}. ExecuteFeatureQuery with ({1})", Thread.CurrentThread.ManagedThreadId, geom, arguments));
try
{
var fds = new FeatureDataSet();
_provider.ExecuteIntersectionQuery(geom, fds);
System.Diagnostics.Trace.WriteLine(string.Format(
"Thread {0}: {2}. {1} features", Thread.CurrentThread.ManagedThreadId, fds.Tables[0].Count, arguments));
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(string.Format(
"Thread {0}: {1}. failed:\n{2}", Thread.CurrentThread.ManagedThreadId, arguments, ex.Message));
_testsFailed[(int)arguments] = ex;
}
SignalRun((int)arguments);
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
_provider.Dispose();
}
}
}