예제 #1
0
        public void TestThrowsTimeout()
        {
            StartToken        token = new StartToken();
            string            finalResult;
            ExceptionReceptor receptor = new ExceptionReceptor();
            DateTime          startedAt;

            using (var vault = _meth(TimeSpan.FromMilliseconds(250), () => new ResourceType()))
            {
                // ReSharper disable AccessToDisposedClosure
                Thread firstThread  = new Thread(() => DoThreadOne(vault, token));
                Thread secondThread = new Thread(() => DoThreadTwo(vault, token, receptor));
                // ReSharper restore AccessToDisposedClosure

                firstThread.Start();
                secondThread.Start();
                Thread.SpinWait(100_000);
                startedAt = token.SetOrThrow();
                secondThread.Join();
                firstThread.Join();

                {
                    using var lck = vault.Lock();
                    finalResult   = lck.ExecuteMixedOperation(MixedOp);
                }



                Assert.NotNull(receptor.SuppliedException);
                Assert.False(receptor.IsBadException);
                Assert.True(typeof(TimeoutException) == receptor.ExceptionType);
                Assert.True(receptor.Ts > startedAt);
            }
            Helper.WriteLine(finalResult);
            Helper.WriteLine($"Timeout thrown after {(receptor.Ts - startedAt).TotalMilliseconds:F3} milliseconds.");
예제 #2
0
            static void DoThreadOne(VaultType bmv, StartToken tkn)
            {
                HpTimeStamps.TimeStampSource.Calibrate();
                while (!tkn.IsSet)
                {
                }

                using var lck = bmv.Lock();
                Thread.Sleep(TimeSpan.FromMilliseconds(750));
                lck.Value += "Hi mom!";
            }
            static void DoThreadOne(VaultType bmv, StartToken tkn, string shouldBe)
            {
                HpTimeStamps.TimeStampSource.Calibrate();
                while (!tkn.IsSet)
                {
                }

                using var lck = bmv.RoLock();
                Thread.Sleep(TimeSpan.FromMilliseconds(750));
                Assert.Equal(lck.Value, shouldBe);
            }
예제 #4
0
 static void DoThreadTwo(VaultType bv, StartToken tkn, ExceptionReceptor receptor)
 {
     HpTimeStamps.TimeStampSource.Calibrate();
     while (!tkn.IsSet)
     {
     }
     Thread.Sleep(TimeSpan.FromMilliseconds(250));
     try
     {
         using var lck = bv.Lock(TimeSpan.FromMilliseconds(10));
         receptor.SetBadException();
     }
     catch (TimeoutException ex)
     {
         receptor.SupplyExceptionOrThrow(ex);
     }
     catch (Exception rx)
     {
         receptor.SupplyExceptionOrThrow(rx);
     }
 }
        public void TestThrowsTimeout()
        {
            string            text  = "Hello, world!";
            StartToken        token = new StartToken();
            ResourceType      finalResult;
            ExceptionReceptor receptor = new ExceptionReceptor();
            DateTime          startedAt;

            using (var vault = _meth(TimeSpan.FromMilliseconds(250)))
            {
                vault.SetCurrentValue(TimeSpan.FromMilliseconds(100), text);

                // ReSharper disable AccessToDisposedClosure
                Thread firstThread  = new Thread(() => DoThreadOne(vault, token, text));
                Thread secondThread = new Thread(() => DoThreadTwo(vault, token, receptor));
                // ReSharper restore AccessToDisposedClosure

                firstThread.Start();
                secondThread.Start();
                Thread.SpinWait(100_000);
                startedAt = token.SetOrThrow();
                secondThread.Join();
                firstThread.Join();

                {
                    using var lck = vault.Lock();
                    lck.Value    += "... it worked?!";
                    finalResult   = lck.Value;
                }

                Assert.NotNull(receptor.SuppliedException);
                Assert.False(receptor.IsBadException);
                Assert.True(typeof(TimeoutException) == receptor.ExceptionType);
                Assert.True(receptor.Ts > startedAt);
                Assert.Equal(finalResult, vault.CopyCurrentValue(TimeSpan.FromMilliseconds(10)));
            }
            Helper.WriteLine(finalResult);
            Helper.WriteLine($"Timeout thrown after {(receptor.Ts - startedAt).TotalMilliseconds:F3} milliseconds.");