Exemple #1
0
        public void Timeout_LongMethod()
        {
            originalTimeout = RuntimeClient.Current.GetResponseTimeout();
            bool        finished  = false;
            var         grainName = typeof(ErrorGrain).FullName;
            IErrorGrain grain     = GrainClient.GrainFactory.GetGrain <IErrorGrain>(GetRandomGrainId(), grainName);
            TimeSpan    timeout   = TimeSpan.FromMilliseconds(1000);

            RuntimeClient.Current.SetResponseTimeout(timeout);

            Task promise = grain.LongMethod((int)timeout.Multiply(4).TotalMilliseconds);
            //promise = grain.LongMethodWithError(2000);

            // there is a race in the test here. If run in debugger, the invocation can actually finish OK
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            try
            {
                finished = promise.Wait(timeout.Multiply(3));
                Assert.Fail("Should have thrown");
            }
            catch (Exception exc)
            {
                stopwatch.Stop();
                Exception baseExc = exc.GetBaseException();
                if (!(baseExc is TimeoutException))
                {
                    Assert.Fail("Should not have got here " + exc);
                }
            }
            Console.WriteLine("Waited for " + stopwatch.Elapsed);
            Assert.IsTrue(!finished);
            Assert.IsTrue(stopwatch.Elapsed >= timeout.Multiply(0.9), "Waited less than " + timeout.Multiply(0.9) + ". Waited " + stopwatch.Elapsed);
            Assert.IsTrue(stopwatch.Elapsed <= timeout.Multiply(2), "Waited longer than " + timeout.Multiply(2) + ". Waited " + stopwatch.Elapsed);
            Assert.IsTrue(promise.Status == TaskStatus.Faulted);

            // try to re-use the promise and should fail immideately.
            try
            {
                stopwatch = new Stopwatch();
                promise.Wait();
                Assert.Fail("Should have thrown");
            }
            catch (Exception exc)
            {
                stopwatch.Stop();
                Exception baseExc = exc.GetBaseException();
                if (!(baseExc is TimeoutException))
                {
                    Assert.Fail("Should not have got here " + exc);
                }
            }
            Assert.IsTrue(stopwatch.Elapsed <= timeout.Multiply(0.1), "Waited longer than " + timeout.Multiply(0.1) + ". Waited " + stopwatch.Elapsed);
            Assert.IsTrue(promise.Status == TaskStatus.Faulted);
        }
        // check that premature wait finishes on time with false.
        public void ErrorHandlingTimedMethod()
        {
            var         grainFullName = typeof(ErrorGrain).FullName;
            IErrorGrain grain         = this.GrainFactory.GetGrain <IErrorGrain>(GetRandomGrainId(), grainFullName);

            Task promise = grain.LongMethod(2000);

            // there is a race in the test here. If run in debugger, the invocation can actually finish OK
            Stopwatch stopwatch = Stopwatch.StartNew();

            Assert.False(promise.Wait(1000), "The task shouldn't have completed yet.");

            // these asserts depend on timing issues and will be wrong for the sync version of OrleansTask
            Assert.True(stopwatch.ElapsedMilliseconds >= 900, $"Waited less than 900ms: ({stopwatch.ElapsedMilliseconds}ms)"); // check that we waited at least 0.9 second
            Assert.True(stopwatch.ElapsedMilliseconds <= 1300, $"Waited longer than 1300ms: ({stopwatch.ElapsedMilliseconds}ms)");

            promise.Wait(); // just wait for the server side grain invocation to finish

            Assert.True(promise.Status == TaskStatus.RanToCompletion);
        }
        // check that premature wait finishes on time with false.
        public void ErrorHandlingTimedMethod()
        {
            var         grainFullName = typeof(ErrorGrain).FullName;
            IErrorGrain grain         = GrainClient.GrainFactory.GetGrain <IErrorGrain>(GetRandomGrainId(), grainFullName);

            Task promise = grain.LongMethod(2000);

            // there is a race in the test here. If run in debugger, the invocation can actually finish OK
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            bool finished = promise.Wait(TimeSpan.FromMilliseconds(1000));

            stopwatch.Stop();

            // these asserts depend on timing issues and will be wrong for the sync version of OrleansTask
            Assert.True(!finished);
            Assert.True(stopwatch.ElapsedMilliseconds >= 900, "Waited less than 900ms"); // check that we waited at least 0.9 second
            Assert.True(stopwatch.ElapsedMilliseconds <= 1100, "Waited longer than 1100ms");

            promise.Wait(); // just wait for the server side grain invocation to finish

            Assert.True(promise.Status == TaskStatus.RanToCompletion);
        }