/// <exception cref="System.Exception"/> public virtual void TestThreadSafety() { // Test for thread safety by starting multiple threads that mutate the same // StartupProgress instance in various ways. We expect no internal // corruption of data structures and no lost updates on counter increments. int numThreads = 100; // Data tables used by each thread to determine values to pass to APIs. Phase[] phases = new Phase[] { Phase.LoadingFsimage, Phase.LoadingFsimage, Phase. LoadingEdits, Phase.LoadingEdits }; Step[] steps = new Step[] { new Step(StepType.Inodes), new Step(StepType.DelegationKeys ), new Step(StepType.Inodes), new Step(StepType.DelegationKeys) }; string[] files = new string[] { "file1", "file1", "file2", "file2" }; long[] sizes = new long[] { 1000L, 1000L, 2000L, 2000L }; long[] totals = new long[] { 10000L, 20000L, 30000L, 40000L }; ExecutorService exec = Executors.NewFixedThreadPool(numThreads); try { for (int i = 0; i < numThreads; ++i) { Phase phase = phases[i % phases.Length]; Step step = steps[i % steps.Length]; string file = files[i % files.Length]; long size = sizes[i % sizes.Length]; long total = totals[i % totals.Length]; exec.Submit(new _Callable_369(this, phase, file, size, step, total)); } } finally { exec.Shutdown(); NUnit.Framework.Assert.IsTrue(exec.AwaitTermination(10000L, TimeUnit.Milliseconds )); } StartupProgressView view = startupProgress.CreateView(); NUnit.Framework.Assert.IsNotNull(view); NUnit.Framework.Assert.AreEqual("file1", view.GetFile(Phase.LoadingFsimage)); NUnit.Framework.Assert.AreEqual(1000L, view.GetSize(Phase.LoadingFsimage)); NUnit.Framework.Assert.AreEqual(10000L, view.GetTotal(Phase.LoadingFsimage, new Step (StepType.Inodes))); NUnit.Framework.Assert.AreEqual(2500L, view.GetCount(Phase.LoadingFsimage, new Step (StepType.Inodes))); NUnit.Framework.Assert.AreEqual(20000L, view.GetTotal(Phase.LoadingFsimage, new Step (StepType.DelegationKeys))); NUnit.Framework.Assert.AreEqual(2500L, view.GetCount(Phase.LoadingFsimage, new Step (StepType.DelegationKeys))); NUnit.Framework.Assert.AreEqual("file2", view.GetFile(Phase.LoadingEdits)); NUnit.Framework.Assert.AreEqual(2000L, view.GetSize(Phase.LoadingEdits)); NUnit.Framework.Assert.AreEqual(30000L, view.GetTotal(Phase.LoadingEdits, new Step (StepType.Inodes))); NUnit.Framework.Assert.AreEqual(2500L, view.GetCount(Phase.LoadingEdits, new Step (StepType.Inodes))); NUnit.Framework.Assert.AreEqual(40000L, view.GetTotal(Phase.LoadingEdits, new Step (StepType.DelegationKeys))); NUnit.Framework.Assert.AreEqual(2500L, view.GetCount(Phase.LoadingEdits, new Step (StepType.DelegationKeys))); }
public virtual void TestInitialState() { StartupProgressView view = startupProgress.CreateView(); NUnit.Framework.Assert.IsNotNull(view); NUnit.Framework.Assert.AreEqual(0L, view.GetElapsedTime()); NUnit.Framework.Assert.AreEqual(0.0f, view.GetPercentComplete(), 0.001f); IList <Phase> phases = new AList <Phase>(); foreach (Phase phase in view.GetPhases()) { phases.AddItem(phase); NUnit.Framework.Assert.AreEqual(0L, view.GetElapsedTime(phase)); NUnit.Framework.Assert.IsNull(view.GetFile(phase)); NUnit.Framework.Assert.AreEqual(0.0f, view.GetPercentComplete(phase), 0.001f); NUnit.Framework.Assert.AreEqual(long.MinValue, view.GetSize(phase)); NUnit.Framework.Assert.AreEqual(Status.Pending, view.GetStatus(phase)); NUnit.Framework.Assert.AreEqual(0L, view.GetTotal(phase)); foreach (Step step in view.GetSteps(phase)) { NUnit.Framework.Assert.Fail(string.Format("unexpected step %s in phase %s at initial state" , step, phase)); } } Assert.AssertArrayEquals(Sharpen.Collections.ToArray(EnumSet.AllOf <Phase>()), Sharpen.Collections.ToArray (phases)); }
public virtual void TestFrozenAfterStartupCompletes() { // Do some updates and counter increments. startupProgress.BeginPhase(Phase.LoadingFsimage); startupProgress.SetFile(Phase.LoadingFsimage, "file1"); startupProgress.SetSize(Phase.LoadingFsimage, 1000L); Step step = new Step(StepType.Inodes); startupProgress.BeginStep(Phase.LoadingFsimage, step); startupProgress.SetTotal(Phase.LoadingFsimage, step, 10000L); StartupProgressTestHelper.IncrementCounter(startupProgress, Phase.LoadingFsimage, step, 100L); startupProgress.EndStep(Phase.LoadingFsimage, step); startupProgress.EndPhase(Phase.LoadingFsimage); // Force completion of phases, so that entire startup process is completed. foreach (Phase phase in EnumSet.AllOf <Phase>()) { if (startupProgress.GetStatus(phase) != Status.Complete) { startupProgress.BeginPhase(phase); startupProgress.EndPhase(phase); } } StartupProgressView before = startupProgress.CreateView(); // Attempt more updates and counter increments. startupProgress.BeginPhase(Phase.LoadingFsimage); startupProgress.SetFile(Phase.LoadingFsimage, "file2"); startupProgress.SetSize(Phase.LoadingFsimage, 2000L); startupProgress.BeginStep(Phase.LoadingFsimage, step); startupProgress.SetTotal(Phase.LoadingFsimage, step, 20000L); StartupProgressTestHelper.IncrementCounter(startupProgress, Phase.LoadingFsimage, step, 100L); startupProgress.EndStep(Phase.LoadingFsimage, step); startupProgress.EndPhase(Phase.LoadingFsimage); // Also attempt a whole new step that wasn't used last time. startupProgress.BeginPhase(Phase.LoadingEdits); Step newStep = new Step("file1"); startupProgress.BeginStep(Phase.LoadingEdits, newStep); StartupProgressTestHelper.IncrementCounter(startupProgress, Phase.LoadingEdits, newStep , 100L); startupProgress.EndStep(Phase.LoadingEdits, newStep); startupProgress.EndPhase(Phase.LoadingEdits); StartupProgressView after = startupProgress.CreateView(); // Expect that data was frozen after completion of entire startup process, so // second set of updates and counter increments should have had no effect. NUnit.Framework.Assert.AreEqual(before.GetCount(Phase.LoadingFsimage), after.GetCount (Phase.LoadingFsimage)); NUnit.Framework.Assert.AreEqual(before.GetCount(Phase.LoadingFsimage, step), after .GetCount(Phase.LoadingFsimage, step)); NUnit.Framework.Assert.AreEqual(before.GetElapsedTime(), after.GetElapsedTime()); NUnit.Framework.Assert.AreEqual(before.GetElapsedTime(Phase.LoadingFsimage), after .GetElapsedTime(Phase.LoadingFsimage)); NUnit.Framework.Assert.AreEqual(before.GetElapsedTime(Phase.LoadingFsimage, step) , after.GetElapsedTime(Phase.LoadingFsimage, step)); NUnit.Framework.Assert.AreEqual(before.GetFile(Phase.LoadingFsimage), after.GetFile (Phase.LoadingFsimage)); NUnit.Framework.Assert.AreEqual(before.GetSize(Phase.LoadingFsimage), after.GetSize (Phase.LoadingFsimage)); NUnit.Framework.Assert.AreEqual(before.GetTotal(Phase.LoadingFsimage), after.GetTotal (Phase.LoadingFsimage)); NUnit.Framework.Assert.AreEqual(before.GetTotal(Phase.LoadingFsimage, step), after .GetTotal(Phase.LoadingFsimage, step)); NUnit.Framework.Assert.IsFalse(after.GetSteps(Phase.LoadingEdits).GetEnumerator() .HasNext()); }