Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BuilderManagerTests.cs « MonoDevelop.Projects « MonoDevelop.Core.Tests « tests « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 587dc1d8708fc5917d5c54629d2afcdf2791e309 (plain)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//
// BuilderManagerTests.cs
//
// Author:
//       Lluis Sanchez <llsan@microsoft.com>
//
// Copyright (c) 2017 Microsoft
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MonoDevelop.Core;
using NUnit.Framework;
using UnitTests;
using System.Linq;
using MonoDevelop.Projects.MSBuild;

namespace MonoDevelop.Projects
{
	public class BuilderManagerTests: TestBase
	{
		// The solution used in this test fixture uses a special task that can
		// pause the build until signaled to continue. The task creates a file
		// named 'sync-event' when executed, and pauses execution until a file
		// named 'continue-event' is created next to it. This is used to pause
		// a project in the middle of a build, so that concurrency can be tested.
		// The SyncBuildProject project uses this task in a post-build target,
		// so the project build will be paused until signaled to conitnue.
		// The FileSync target can also uses that task.

		const int timeoutMs = 30000;

		[Test]
		public async Task ConcurrentLongOperations ()
		{
			// When a long operation is running and a new one is requested, a new builder is created

			await RemoteBuildEngineManager.RecycleAllBuilders ();
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

			FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
			using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {

				var project1 = sol.Items.FirstOrDefault (p => p.Name == "SyncBuildProject");
				var project2 = sol.Items.FirstOrDefault (p => p.Name == "App");

				InitBuildSyncEvent (project1);

				// Start the build
				var build1 = project1.Build (Util.GetMonitor (), sol.Configurations [0].Selector);

				// Wait for the build to reach the sync task
				await WaitForBuildSyncEvent (project1);

				// The build is now in progess. Start a new build

				var build2 = project2.Build (Util.GetMonitor (), sol.Configurations [0].Selector);
				if (await Task.WhenAny (build2, Task.Delay (timeoutMs)) != build2)
					Assert.Fail ("Build did not start");

				// The second build should finish before the first one

				SignalBuildToContinue (project1);

				if (await Task.WhenAny (build1, Task.Delay (timeoutMs)) != build1)
					Assert.Fail ("Build did not end in time");
				
				Assert.AreEqual (0, build1.Result.ErrorCount);
				Assert.AreEqual (0, build2.Result.ErrorCount);
			}
		}

		[Test]
		public async Task ConcurrentLongAndShortOperations ()
		{
			// Tests that when a short operation is started while a long operation is in progress,
			// a new builder is created to execute the short operation

			await RemoteBuildEngineManager.RecycleAllBuilders ();
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

			FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
			using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {

				var project1 = sol.Items.FirstOrDefault (p => p.Name == "SyncBuildProject");
				var project2 = (Project)sol.Items.FirstOrDefault (p => p.Name == "App");

				InitBuildSyncEvent (project1);

				// Start the build
				var build1 = project1.Build (Util.GetMonitor (), sol.Configurations [0].Selector);

				// Wait for the build to reach the sync task
				await WaitForBuildSyncEvent (project1);

				// The build is now in progess. Start a short operation.
				var context = new TargetEvaluationContext {
					BuilderQueue = BuilderQueue.ShortOperations
				};
				var build2 = project2.RunTarget (Util.GetMonitor (), "QuickTarget", sol.Configurations [0].Selector, context);

				if (await Task.WhenAny (build2, Task.Delay (timeoutMs)) != build2)
					Assert.Fail ("Build did not start");

				SignalBuildToContinue (project1);

				if (await Task.WhenAny (build1, Task.Delay (timeoutMs)) != build1)
					Assert.Fail ("Build did not end in time");
				
				Assert.AreEqual (0, build1.Result.ErrorCount);
				Assert.NotNull (build2.Result);
			}
		}

		[Test]
		public async Task ShortOperationsInSingleBuilder ()
		{
			// Tests that targets using BuilderQueue.ShortOperations share the same builder
			// and don't spawn a new builder when one of them is being executed and another
			// one starts executing.

			await RemoteBuildEngineManager.RecycleAllBuilders ();
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

			FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
			using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {

				var project1 = (Project)sol.Items.FirstOrDefault (p => p.Name == "SyncBuildProject");
				var project2 = (Project)sol.Items.FirstOrDefault (p => p.Name == "App");

				InitBuildSyncEvent (project1);

				// Start the first target. The FileSync target will pause until signaled to continue.
				// Select the ShortOperations build queue.
				var context = new TargetEvaluationContext {
					BuilderQueue = BuilderQueue.ShortOperations
				};
				var build1 = project1.RunTarget (Util.GetMonitor (), "FileSync", sol.Configurations [0].Selector, context);

				// Wait for the build to reach the sync task
				await WaitForBuildSyncEvent (project1);

				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);

				// The build is now in progess. Run a new target.

				context = new TargetEvaluationContext {
					BuilderQueue = BuilderQueue.ShortOperations
				};
				var build2 = project2.RunTarget (Util.GetMonitor (), "QuickTarget", sol.Configurations [0].Selector, context);

				// Wait a bit. This should be enough to ensure the build has started.
				await Task.Delay (1000);

				// The RunTarget request should be queued, not new builder should be spawned
				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);

				// Continue building the first project
				SignalBuildToContinue (project1);

				// The first build should end now
				if (await Task.WhenAny (build1, Task.Delay (timeoutMs)) != build1)
					Assert.Fail ("Build did not end in time");
				
				// And now the second build should end
				if (await Task.WhenAny (build2, Task.Delay (timeoutMs)) != build2)
					Assert.Fail ("Build did not end in time");
				
				Assert.NotNull (build1.Result);
				Assert.NotNull (build2.Result);
				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
			}
		}

		[Test]
		public async Task ConcurrentShortAndLongOperations ()
		{
			// If a builder is running a short operation and a long operation is started,
			// that long operation will wait for the sort operation to finish
			// and will use the same builder, instead of starting a new one.

			await RemoteBuildEngineManager.RecycleAllBuilders ();
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

			FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
			using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {

				var project1 = (Project)sol.Items.FirstOrDefault (p => p.Name == "SyncBuildProject");
				var project2 = (Project)sol.Items.FirstOrDefault (p => p.Name == "App");

				InitBuildSyncEvent (project1);

				// Start the first target. The FileSync target will pause until signaled to continue.
				// Select the ShortOperations build queue.
				var context = new TargetEvaluationContext {
					BuilderQueue = BuilderQueue.ShortOperations
				};
				var build1 = project1.RunTarget (Util.GetMonitor (), "FileSync", sol.Configurations [0].Selector, context);

				// Wait for the build to reach the sync task
				await WaitForBuildSyncEvent (project1);

				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);

				// The build is now in progess. Run a new target.

				context = new TargetEvaluationContext {
					BuilderQueue = BuilderQueue.LongOperations
				};
				var build2 = project2.RunTarget (Util.GetMonitor (), "QuickTarget", sol.Configurations [0].Selector, context);

				// Wait a bit. This should be enough to ensure the build has started.
				await Task.Delay (1000);

				// The RunTarget request should be queued, not new builder should be spawned
				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);

				// Continue building the first project
				SignalBuildToContinue (project1);

				// The first build should end now
				if (await Task.WhenAny (build1, Task.Delay (timeoutMs)) != build1)
					Assert.Fail ("Build did not end in time");

				// And now the second build should end
				if (await Task.WhenAny (build2, Task.Delay (timeoutMs)) != build2)
					Assert.Fail ("Build did not end in time");

				Assert.NotNull (build1.Result);
				Assert.NotNull (build2.Result);
				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
			}
		}

		[Test]
		public async Task ConcurrentShortAndBuildOperations ()
		{
			// If a builder is running a short operation and a build is started,
			// the build operation will wait for the sort operation to finish
			// and will use the same builder, instead of starting a new one.
			// Also, the build session should not start until the short operation
			// is finished.

			await RemoteBuildEngineManager.RecycleAllBuilders ();
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

			FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
			using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {

				var project1 = (Project)sol.Items.FirstOrDefault (p => p.Name == "SyncBuildProject");
				var project2 = (Project)sol.Items.FirstOrDefault (p => p.Name == "App");

				InitBuildSyncEvent (project1);

				// Start the first target. The FileSync target will pause until signaled to continue.
				// Select the ShortOperations build queue.
				var context = new TargetEvaluationContext {
					BuilderQueue = BuilderQueue.ShortOperations
				};
				var build1 = project1.RunTarget (Util.GetMonitor (), "FileSync", sol.Configurations [0].Selector, context);

				// Wait for the build to reach the sync task
				await WaitForBuildSyncEvent (project1);

				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);

				// The build is now in progess. Run a new target.

				var build2 = project2.Build (Util.GetMonitor (), sol.Configurations [0].Selector);

				// Wait a bit. This should be enough to ensure the build has started.
				await Task.Delay (1000);

				// The RunTarget request should be queued, no new builder should be spawned
				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);

				// Continue building the first project
				SignalBuildToContinue (project1);

				// The first build should end now
				if (await Task.WhenAny (build1, Task.Delay (timeoutMs)) != build1)
					Assert.Fail ("Build did not end in time");

				// And now the second build should end
				if (await Task.WhenAny (build2, Task.Delay (timeoutMs)) != build2)
					Assert.Fail ("Build did not end in time");

				Assert.NotNull (build1.Result);
				Assert.AreEqual (0, build2.Result.ErrorCount);
				      
				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
			}
		}

		[Test]
		public async Task DisposingSolutionDisposesBuilder ()
		{
			// When a solution is disposed, all builders bound to the solution should be shut down

			await RemoteBuildEngineManager.RecycleAllBuilders ();
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

			FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
			FilePath projectFile;

			using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {
				var project = (DotNetProject)sol.Items.FirstOrDefault (p => p.Name == "App");
				projectFile = project.FileName;
				await project.GetReferencedAssemblies (sol.Configurations [0].Selector);
				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
				Assert.AreEqual (1, await RemoteBuildEngineManager.CountActiveBuildersForProject (project.FileName));
			}
			Assert.AreEqual (0, await RemoteBuildEngineManager.CountActiveBuildersForProject (projectFile));
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);
		}

		[Test]
		public async Task DisposingProjectDisposesProjectBuilder ()
		{
			// When a project is disposed, all project builders should be shut down

			await RemoteBuildEngineManager.RecycleAllBuilders ();
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

			FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
			FilePath projectFile;

			using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {
				var project = (DotNetProject)sol.Items.FirstOrDefault (p => p.Name == "App");
				projectFile = project.FileName;
				await project.GetReferencedAssemblies (sol.Configurations [0].Selector);
				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
				Assert.AreEqual (1, await RemoteBuildEngineManager.CountActiveBuildersForProject (project.FileName));

				sol.RootFolder.Items.Remove (project);
				project.Dispose ();

				Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
				Assert.AreEqual (1, RemoteBuildEngineManager.EnginesCount);
				Assert.AreEqual (0, await RemoteBuildEngineManager.CountActiveBuildersForProject (project.FileName));
			}
			Assert.AreEqual (0, await RemoteBuildEngineManager.CountActiveBuildersForProject (projectFile));
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);
			Assert.AreEqual (0, RemoteBuildEngineManager.EnginesCount);
		}

		/// <summary>
		/// Tests that a project builder that is marked as shutdown but currently running is not used for a new msbuild
		/// target. This prevents the project builder being disposed whilst the second msbuild target is being run.
		/// Tests that a null reference exception is not thrown by the project builder.
		/// </summary>
		[Test]
		public async Task ReloadProject_ProjectDisposedWhilstTargetRunning_AnotherTargetRun ()
		{
			// When a long operation is running and a new one is requested, a new builder is created

			await RemoteBuildEngineManager.RecycleAllBuilders ();
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

			FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
			using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {

				var project1 = (Project)sol.Items.FirstOrDefault (p => p.Name == "SyncBuildProject");

				InitBuildSyncEvent (project1);

				// Start the build. Use RunTargetInternal to avoid the BindTask which will cancel the build on project dispose.
				var build1 = project1.RunTargetInternal (Util.GetMonitor (), "Build", sol.Configurations [0].Selector);

				// Wait for the build to reach the sync task
				await WaitForBuildSyncEvent (project1);

				// The build is now in progess. Simulate reloading the project.
				using (var project2 = (Project)await sol.RootFolder.ReloadItem (Util.GetMonitor (), project1)) {
					var builderTask = project2.GetProjectBuilder (CancellationToken.None, null, allowBusy: true);

					// Allow second build to finish and dispose its project builder. Have to do this here otherwise
					// GetProjectBuilder will hang waiting for a connection response back after it creates a new
					// project builder.
					SignalBuildToContinue (project1);
					if (await Task.WhenAny (build1, Task.Delay (timeoutMs)) != build1)
						Assert.Fail ("Build did not end in time");

					Assert.AreEqual (0, build1.Result.BuildResult.ErrorCount);

					using (var builder = await builderTask) {

						// Sanity check. We should only have one project builder and one build engine.
						Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
						Assert.AreEqual (1, RemoteBuildEngineManager.EnginesCount);
						Assert.AreEqual (1, await RemoteBuildEngineManager.CountActiveBuildersForProject (project2.FileName));

						var configs = project2.GetConfigurations (sol.Configurations [0].Selector, false);

						var build2 = await Task.Run (() => {
							// Previously this would throw a NullReferenceException since the builder has been disposed
							// and the engine is null.
							return builder.Run (
								configs,
								new StringWriter (),
								new MSBuildLogger (),
								MSBuildVerbosity.Quiet,
								new [] { "ResolveAssemblyReferences" },
								new string [0],
								new string [0],
								new System.Collections.Generic.Dictionary<string, string> (),
								CancellationToken.None);
						});

						Assert.AreEqual (0, build2.Errors.Length);
					}
				}
			}
		}

		[Test]
		public async Task ExtraBuilderAutoShutdown ()
		{
			// When an extra builder is created, it should be shut down when it is
			// not used anymore, after a delay

			var currentDelay = RemoteBuildEngineManager.EngineDisposalDelay;

			try {
				RemoteBuildEngineManager.EngineDisposalDelay = 400;

				await RemoteBuildEngineManager.RecycleAllBuilders ();
				Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

				FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
				using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {

					var project1 = sol.Items.FirstOrDefault (p => p.Name == "SyncBuildProject");
					var project2 = sol.Items.FirstOrDefault (p => p.Name == "App");

					InitBuildSyncEvent (project1);

					// Start the build
					var build1 = project1.Build (Util.GetMonitor (), sol.Configurations [0].Selector);

					// Wait for the build to reach the sync task
					await WaitForBuildSyncEvent (project1);

					Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
					Assert.AreEqual (0, await RemoteBuildEngineManager.CountActiveBuildersForProject (project2.FileName));

					// The build is now in progess. Start a new build

					var build2 = project2.Build (Util.GetMonitor (), sol.Configurations [0].Selector);
					if (await Task.WhenAny (build2, Task.Delay (timeoutMs)) != build2)
						Assert.Fail ("Build did not start");
					
					Assert.AreEqual (2, RemoteBuildEngineManager.ActiveEnginesCount);
					Assert.AreEqual (1, await RemoteBuildEngineManager.CountActiveBuildersForProject (project2.FileName));

					// Build engine disposal delay is set to 400ms, so it should be gone after waiting the following wait.

					await Task.Delay (500);

					// The second builder should now be gone

					Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
					Assert.AreEqual (1, RemoteBuildEngineManager.EnginesCount);
					Assert.AreEqual (0, await RemoteBuildEngineManager.CountActiveBuildersForProject (project2.FileName));

					SignalBuildToContinue (project1);

					if (await Task.WhenAny (build1, Task.Delay (timeoutMs)) != build1)
						Assert.Fail ("Build did not end in time");
				}
			} finally {
				RemoteBuildEngineManager.EngineDisposalDelay = currentDelay;
			}
		}

		[Test]
		public async Task AtLeastOneBuilderPersolution ()
		{
			// There should always be at least one builder running per solution,
			// until the solution is explicitly disposed

			var currentDelay = RemoteBuildEngineManager.EngineDisposalDelay;

			try {
				RemoteBuildEngineManager.EngineDisposalDelay = 400;

				await RemoteBuildEngineManager.RecycleAllBuilders ();
				Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

				FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
				using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {

					var project1 = sol.Items.FirstOrDefault (p => p.Name == "SyncBuildProject");
					var project2 = sol.Items.FirstOrDefault (p => p.Name == "App");

					InitBuildSyncEvent (project1);

					// Start the build
					var build1 = project1.Build (Util.GetMonitor (), sol.Configurations [0].Selector);

					// Wait for the build to reach the sync task
					await WaitForBuildSyncEvent (project1);

					Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
					Assert.AreEqual (0, await RemoteBuildEngineManager.CountActiveBuildersForProject (project2.FileName));

					// The build is now in progess. Start a new build

					var build2 = project2.Build (Util.GetMonitor (), sol.Configurations [0].Selector);

					if (await Task.WhenAny (build2, Task.Delay (timeoutMs)) != build2)
						Assert.Fail ("Build did not start");

					Assert.AreEqual (2, RemoteBuildEngineManager.ActiveEnginesCount);
					Assert.AreEqual (2, RemoteBuildEngineManager.EnginesCount);
					Assert.AreEqual (1, await RemoteBuildEngineManager.CountActiveBuildersForProject (project1.FileName));
					Assert.AreEqual (1, await RemoteBuildEngineManager.CountActiveBuildersForProject (project2.FileName));

					SignalBuildToContinue (project1);

					if (await Task.WhenAny (build1, Task.Delay (timeoutMs)) != build1)
						Assert.Fail ("Build did not end in time");

					// Build engine disposal delay is set to 400ms, so unused
					// builders should go away after a 500ms wait.

					await Task.Delay (500);

					// There should be at least one builder left

					Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
					Assert.AreEqual (1, RemoteBuildEngineManager.EnginesCount);

				}
			} finally {
				RemoteBuildEngineManager.EngineDisposalDelay = currentDelay;
			}

			// All builders should be gone now
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);
			Assert.AreEqual (0, RemoteBuildEngineManager.EnginesCount);
		}

		[Test]
		public async Task RecycleBuildersIsGraceful ()
		{
			// RecycleBuilders can be called in the middle of a build and that should not interrupt
			// the build. Builders will be shut down when the build is finished.

			var currentDelay = RemoteBuildEngineManager.EngineDisposalDelay;

			try {
				RemoteBuildEngineManager.EngineDisposalDelay = 400;

				await RemoteBuildEngineManager.RecycleAllBuilders ();
				Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

				FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
				using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {

					var project1 = sol.Items.FirstOrDefault (p => p.Name == "SyncBuildProject");
					var project2 = sol.Items.FirstOrDefault (p => p.Name == "App");

					InitBuildSyncEvent (project1);

					// Start the build
					var build1 = project1.Build (Util.GetMonitor (), sol.Configurations [0].Selector);

					// Wait for the build to reach the sync task
					await WaitForBuildSyncEvent (project1);

					Assert.AreEqual (1, RemoteBuildEngineManager.ActiveEnginesCount);
					Assert.AreEqual (0, await RemoteBuildEngineManager.CountActiveBuildersForProject (project2.FileName));

					// The build is now in progess. Start a new build

					var build2 = project2.Build (Util.GetMonitor (), sol.Configurations [0].Selector);
					if (await Task.WhenAny (build2, Task.Delay (timeoutMs)) != build2)
						Assert.Fail ("Build did not start");

					// There should be one builder for each build

					Assert.AreEqual (2, RemoteBuildEngineManager.ActiveEnginesCount);
					Assert.AreEqual (2, RemoteBuildEngineManager.EnginesCount);

					// Ask for all active builders to be shut down

					await RemoteBuildEngineManager.RecycleAllBuilders ();

					// There is a build in progress, so there must still be one engine running

					Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);
					Assert.AreEqual (1, RemoteBuildEngineManager.EnginesCount);

					SignalBuildToContinue (project1);

					if (await Task.WhenAny (build1, Task.Delay (timeoutMs)) != build1)
						Assert.Fail ("Build did not end in time");
					
					Assert.AreEqual (0, build1.Result.ErrorCount);
					Assert.AreEqual (0, build2.Result.ErrorCount);

					// The builder that was running the build and was shutdown should be immediately stopped after build finishes
					Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);
					Assert.AreEqual (0, RemoteBuildEngineManager.EnginesCount);

				}
			} finally {
				RemoteBuildEngineManager.EngineDisposalDelay = currentDelay;
			}
		}

		[Test]
		public async Task ParallelBuilds ()
		{
			// Check that the project system can start two builds in parallel.

			await RemoteBuildEngineManager.RecycleAllBuilders ();
			Assert.AreEqual (0, RemoteBuildEngineManager.ActiveEnginesCount);

			var currentSetting = Runtime.Preferences.ParallelBuild.Value;
			try {
				Runtime.Preferences.ParallelBuild.Set (true);

				FilePath solFile = Util.GetSampleProject ("builder-manager-tests", "builder-manager-tests.sln");
				using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {

					// DepMain depends on both Dep1 and Dep2.

					var demMain = sol.Items.FirstOrDefault (p => p.Name == "DepMain");
					var dep1 = sol.Items.FirstOrDefault (p => p.Name == "Dep1");
					var dep2 = sol.Items.FirstOrDefault (p => p.Name == "Dep2");

					InitBuildSyncEvent (dep1);
					InitBuildSyncEvent (dep2);

					// Start the build
					var build1 = demMain.Build (Util.GetMonitor (), sol.Configurations [0].Selector, true);

					// Wait for sync signal from projects Dep1 and Dep2, which will mean that
					// both projects started building in parallel

					var syncAll = Task.WhenAll (WaitForBuildSyncEvent (dep1), WaitForBuildSyncEvent (dep2));

					//NOTE: this has a longer timeout because otherwise it sometimes times out
					//maybe something to do with the fact it compiles tasks
					if (await Task.WhenAny (syncAll, Task.Delay (timeoutMs)) != syncAll)
						Assert.Fail ("Not all builds were started");

					// Finish the build
					SignalBuildToContinue (dep1);
					SignalBuildToContinue (dep2);

					if (await Task.WhenAny (build1, Task.Delay (timeoutMs)) != build1)
						Assert.Fail ("Build did not end in time");

					Assert.AreEqual (0, build1.Result.ErrorCount);
				}
			} finally {
				Runtime.Preferences.ParallelBuild.Set (currentSetting);
			}
		}
		void InitBuildSyncEvent (SolutionItem p)
		{
			var file = p.FileName.ParentDirectory.Combine ("sync-event");
			if (File.Exists (file))
				File.Delete (file);
			file = p.FileName.ParentDirectory.Combine ("continue-event");
			if (File.Exists (file))
				File.Delete (file);
		}

		async Task WaitForBuildSyncEvent (SolutionItem p)
		{
			var file = p.FileName.ParentDirectory.Combine ("sync-event");
			while (!File.Exists (file)) {
				await Task.Delay (50);
			}
		}

		void SignalBuildToContinue (SolutionItem p)
		{
			var file = p.FileName.ParentDirectory.Combine ("continue-event");
			File.WriteAllText (file, "");
		}
	}
}