using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading; using UnitTestSharp; using Action = System.Action; namespace Foundations.UnitTests { public class JitWarmupTests : TestFixture { public static readonly TimeSpan GateWait = TimeSpan.FromSeconds(5); public static void Spin(TimeSpan time) { var watch = Stopwatch.StartNew(); while (watch.Elapsed < time) { } } public class BeginTests : TestFixture { public void RunsThePassesInOrderOnAnotherThread() { var ran = new List(); int testThread = Thread.CurrentThread.ManagedThreadId; int passThread = testThread; JitWarmup warmup = JitWarmup.Begin(new Action[] { () => ran.Add("first"), () => { ran.Add("second"); passThread = Thread.CurrentThread.ManagedThreadId; }, }); warmup.Wait(); CheckEqual("first second", string.Join(" ", ran)); CheckTrue(passThread != testThread); } public void RunsOnABackgroundThread() { bool isBackground = false; string name = null; JitWarmup.Begin(new Action[] { () => { isBackground = Thread.CurrentThread.IsBackground; name = Thread.CurrentThread.Name; }, }).Wait(); CheckTrue(isBackground); CheckEqual("JitWarmup", name); } public void ReturnsBeforeThePassesAreDone() { var gate = new ManualResetEventSlim(); JitWarmup warmup = JitWarmup.Begin(new Action[] { () => gate.Wait(GateWait) }); CheckFalse(warmup.IsDone); gate.Set(); warmup.Wait(); } public void NoPasses_IsDoneOnceStarted() { JitWarmup warmup = JitWarmup.Begin(new Action[0]); warmup.Wait(); CheckTrue(warmup.IsDone); } public void CollectsTheGarbageThePassesLeave() { WeakReference garbage = null; JitWarmup.Begin(new Action[] { () => garbage = new WeakReference(new object()) }).Wait(); CheckFalse(garbage.IsAlive); } public void ANullListOfPasses_Throws() { CheckThrow(typeof(ArgumentNullException)); JitWarmup.Begin(null); } public void ANullPass_Throws() { CheckThrow(typeof(ArgumentException)); JitWarmup.Begin(new Action[] { null }); } public void TheListIsCopied_SoChangingItLaterChangesNothing() { var gate = new ManualResetEventSlim(); int later = 0; var passes = new List { () => gate.Wait(GateWait) }; JitWarmup warmup = JitWarmup.Begin(passes); passes.Add(() => ++later); gate.Set(); warmup.Wait(); CheckEqual(0, later); } } public class WaitTests : TestFixture { public void ReturnsOnceThePassesAreDone() { var gate = new ManualResetEventSlim(); JitWarmup warmup = JitWarmup.Begin(new Action[] { () => gate.Wait(GateWait) }); gate.Set(); warmup.Wait(); CheckTrue(warmup.IsDone); } public void RethrowsWhatAPassThrew() { JitWarmup warmup = JitWarmup.Begin(new Action[] { () => throw new InvalidOperationException("boom"), }); CheckThrow(typeof(InvalidOperationException)); warmup.Wait(); } public void RethrowsOnEveryCall() { JitWarmup warmup = JitWarmup.Begin(new Action[] { () => throw new InvalidOperationException("boom"), }); int thrown = 0; for (int i = 0; i < 2; ++i) { try { warmup.Wait(); } catch (InvalidOperationException) { ++thrown; } } CheckEqual(2, thrown); } public void AFailingPass_StopsTheLaterPasses() { bool laterRan = false; JitWarmup warmup = JitWarmup.Begin(new Action[] { () => throw new InvalidOperationException("boom"), () => laterRan = true, }); try { warmup.Wait(); } catch (InvalidOperationException) { } CheckFalse(laterRan); CheckTrue(warmup.IsDone); } public void AFailingPass_StillGetsTheGarbageCollected() { WeakReference garbage = null; JitWarmup warmup = JitWarmup.Begin(new Action[] { () => { garbage = new WeakReference(new object()); throw new InvalidOperationException("boom"); }, }); try { warmup.Wait(); } catch (InvalidOperationException) { } CheckFalse(garbage.IsAlive); } } public class FailureTests : TestFixture { public void IsNullWhenEveryPassSucceeds() { JitWarmup warmup = JitWarmup.Begin(new Action[] { () => { } }); warmup.Wait(); CheckNull(warmup.Failure); } public void IsWhatTheFailingPassThrew() { var thrown = new InvalidOperationException("boom"); JitWarmup warmup = JitWarmup.Begin(new Action[] { () => throw thrown }); try { warmup.Wait(); } catch (InvalidOperationException) { } CheckTrue(ReferenceEquals(thrown, warmup.Failure)); } } public class DurationTests : TestFixture { public void WhileRunning_Throws() { var gate = new ManualResetEventSlim(); JitWarmup warmup = JitWarmup.Begin(new Action[] { () => gate.Wait(GateWait) }); bool threw = false; try { TimeSpan unused = warmup.Duration; } catch (InvalidOperationException) { threw = true; } gate.Set(); warmup.Wait(); CheckTrue(threw); } public void IsAtLeastAsLongAsThePassesTook() { TimeSpan spent = TimeSpan.FromMilliseconds(20); JitWarmup warmup = JitWarmup.Begin(new Action[] { () => Spin(spent) }); warmup.Wait(); CheckTrue(warmup.Duration >= spent); } } public class BeginStandardTests : TestFixture { public void RunsTheGlassWarmupToTheEnd() { JitWarmup warmup = JitWarmup.BeginStandard(); warmup.Wait(); CheckTrue(warmup.IsDone); CheckNull(warmup.Failure); } } } }