Exemplo n.º 1
0
        public async Task HandleEventAsync()
        {
            decimal       oldBalance;
            decimal       nowBalance;
            const decimal diff   = 100M;
            const int     times  = 10;
            const string  testId = "testId";

            using (var lifetimeScope = BuildService().CreateScope())
            {
                var      factory          = lifetimeScope.ServiceProvider.GetRequiredService <Account.Factory>();
                var      claptrapIdentity = new ClaptrapIdentity(testId, Codes.Account);
                IAccount account          = factory.Invoke(claptrapIdentity);
                await account.ActivateAsync();

                oldBalance = await account.GetBalanceAsync();

                await Task.WhenAll(Enumerable.Range(0, times)
                                   .Select(i => account.ChangeBalanceAsync(diff)));

                var balance = await account.GetBalanceAsync();

                await account.DeactivateAsync();

                nowBalance = oldBalance + times * 100;
                balance.Should().Be(nowBalance);
            }

            Console.WriteLine($"balance change: {oldBalance} + {diff} = {nowBalance}");

            using (var lifetimeScope = BuildService().CreateScope())
            {
                var factory          = lifetimeScope.ServiceProvider.GetService <AccountBalanceMinion.Factory>();
                var claptrapIdentity = new ClaptrapIdentity(testId, Codes.AccountBalanceMinion);
                IAccountBalanceMinion accountBalance = factory.Invoke(claptrapIdentity);
                await accountBalance.ActivateAsync();

                var balance = await accountBalance.GetBalanceAsync();

                balance.Should().Be(nowBalance);
                Console.WriteLine($"balance from minion {balance}");
            }

            await OnStopHost(Host);

            await Host.StopAsync();
        }