Represent a raising phoenix. Calling reborn many time will not actually do anything until the method reborn complete and it will return AlivePhoenix
Inheritance: IPhoenixState
        public void Reborn_should_not_call_reborn_on_result_if_task_completed_and_just_return_the_returned_phoenixState()
        {
            IPhoenixState reborn = Substitute.For<IPhoenixState>();
            Func<Task<IPhoenixState>> action = () => Task.FromResult(reborn);
            var state = new RaisingPhoenix();
            IPhoenixState rebornState;
            do
            {
                rebornState = state.Reborn(action);
            } while (object.ReferenceEquals(rebornState, state));

            reborn.Received(0).Reborn(Arg.Any<Func<Task<IPhoenixState>>>());
        }
 public void Reborn_should_return_same_instance_if_there_is_exception()
 {
     Func<Task<IPhoenixState>> action = () =>
     {
         Func<IPhoenixState> func = () => { throw new Exception(); };
         return Task.Run(func);
     };
     var state = new RaisingPhoenix();
     for (var i = 0; i < 10000; i++)
     {
         Assert.AreSame(state, state.Reborn(action));
     }
 }
 public void GetState_should_return_status()
 {
     IPhoenixState state = new RaisingPhoenix();
     Assert.AreEqual("wait to raise", state.GetState());
     
     var wait = new AutoResetEvent(false);
     var wait2 = new AutoResetEvent(false);
     Func<Task<IPhoenixState>> action = () =>
     {
         wait.WaitOne();
         state = Substitute.For<IPhoenixState>();
         state.GetState().Returns("some other state");
         wait2.Set();
         return Task.FromResult(state);
     };
     state.Reborn(action);
     Assert.AreEqual("raising", state.GetState());
     wait.Set();
     wait2.WaitOne();
     Assert.AreEqual("some other state", state.GetState());
 }
        public void Reborn_should_execute_the_task_once()
        {
            var hitCount = 2000;
            var wait = new AutoResetEvent(false);
            var count = 0;
            Func<Task<IPhoenixState>> action = () =>
            {
                count ++;
                wait.Set();
                IPhoenixState phoenixState = Substitute.For<IPhoenixState>();
                return Task.FromResult(phoenixState);
            };

            var state = new RaisingPhoenix();
            for (var i = 0; i < hitCount; i++)
            {
                state.Reborn(action);
            }
            Assert.IsTrue(wait.WaitOne(1000));
            Assert.AreEqual(1, count);
        }
示例#5
0
 /// <summary>
 /// Change the state to RaisingPhoenix and call methdo Reborn
 /// </summary>
 /// <param name="rebornAction"></param>
 /// <returns></returns>
 public IPhoenixState Reborn(Func<IPhoenixState> rebornAction)
 {
     IPhoenixState phoenixState = new RaisingPhoenix();
     return phoenixState.Reborn(rebornAction);
 }
示例#6
0
        /// <summary>
        /// Change the state to RaisingPhoenix and call methdo Reborn
        /// </summary>
        /// <param name="rebornAction"></param>
        /// <returns></returns>
        public IPhoenixState Reborn(Func <Task <IPhoenixState> > rebornAction)
        {
            IPhoenixState phoenixState = new RaisingPhoenix();

            return(phoenixState.Reborn(rebornAction));
        }