Exemplo n.º 1
0
        public async void Handle_MultipleUncompletedChargesLessThanMaxRetriesShouldWork()
        {
            var expected = true;
            var customer = new Mock <ICustomer>();

            customer.SetupGet(x => x.Balance).Returns(500);
            customer.SetupSequence(x => x.Charge(It.IsAny <double>()))
            .Returns(async() =>
            {
                await Task.Delay(TimeSpan.FromSeconds(0.5));
                return(false);
            })
            .Returns(async() =>
            {
                await Task.Delay(TimeSpan.FromSeconds(0.5));
                return(false);
            })
            .Returns(async() =>
            {
                await Task.Delay(TimeSpan.FromSeconds(0.5));
                return(true);
            });
            ChargePatientHandler chargePatientHandler = new ChargePatientHandler(customer.Object, 50, 3);
            ChargePatientCommand chargePatientCommand = new ChargePatientCommand(5);
            CancellationToken    cancellationToken    = new CancellationToken();

            var actual = await chargePatientHandler.Handle(chargePatientCommand, cancellationToken);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 2
0
        public async void Handle_TaskTooLongShouldFail()
        {
            var chargingSystem = new Mock <IDummyChargingSystem>();

            chargingSystem.Setup(cs => cs.SimulateCharge(It.IsAny <double>())).Returns(true);
            var DB = new Mock <IDummyDB>();

            DB.Setup(db => db.PersistTransaction(It.IsAny <double>())).Returns(true);
            var customer = new Customer(chargingSystem.Object, DB.Object);

            customer.Balance = 500;
            //customer.SetupGet(x => x.Balance).Returns(500);
            //customer.Setup(x => x.Charge(It.IsAny<double>())).Returns(Task.FromResult(true));
            ChargePatientHandler chargePatientHandler = new ChargePatientHandler(customer, 1);
            ChargePatientCommand chargePatientCommand = new ChargePatientCommand(5);
            CancellationToken    cancellationToken    = new CancellationToken();


            Func <Task> action = () => chargePatientHandler.Handle(chargePatientCommand, cancellationToken);
            //Func<Task<bool>> functionWrapper = async () =>
            //{
            //    var res = await chargePatientHandler.Handle(chargePatientCommand, cancellationToken);
            //    return res;
            //};

            await Assert.ThrowsAsync <TimeoutRejectedException>(action);
        }
Exemplo n.º 3
0
        public async void Handle_MockedTaskTooLongShouldFail()
        {
            var customer = new Mock <ICustomer>();

            customer.SetupGet(x => x.Balance).Returns(500);
            customer.Setup(x => x.Charge(It.IsAny <double>())).Returns(async() =>
            {
                await Task.Delay(TimeSpan.FromSeconds(2));
                return(true);
            });
            ChargePatientHandler chargePatientHandler = new ChargePatientHandler(customer.Object, 1);
            ChargePatientCommand chargePatientCommand = new ChargePatientCommand(5);
            CancellationToken    cancellationToken    = new CancellationToken();

            Func <Task> action = () => chargePatientHandler.Handle(chargePatientCommand, cancellationToken);

            await Assert.ThrowsAsync <TimeoutRejectedException>(action);
        }
Exemplo n.º 4
0
        public async void Handle_MultipleUncompletedChargersGreaterThanMaxRetriesShouldFail()
        {
            var customer = new Mock <ICustomer>();

            customer.SetupGet(x => x.Balance).Returns(500);
            customer.Setup(x => x.Charge(It.IsAny <double>()))
            .Returns(async() =>
            {
                await Task.Delay(TimeSpan.FromSeconds(0.5));
                return(false);
            });
            ChargePatientHandler chargePatientHandler = new ChargePatientHandler(customer.Object, 50, 3);
            ChargePatientCommand chargePatientCommand = new ChargePatientCommand(5);
            CancellationToken    cancellationToken    = new CancellationToken();

            Func <Task> action = () => chargePatientHandler.Handle(chargePatientCommand, cancellationToken);

            await Assert.ThrowsAsync <HttpRequestException>(action);
        }