Exemplo n.º 1
0
        public async Task IfNotEnoughBalance_ShouldFailWithdraw()
        {
            // arrange
            await AccountHelpers.EnsureAccountState(neededBalance : 123);

            (await AccountHelpers.GetAccount()).Balance.Should().Be(123);

            // act
            var operationId = await ClientUtil.AccountsApi.BeginWithdraw(AccountHelpers.AccountId,
                                                                         new AccountChargeRequest
            {
                OperationId = Guid.NewGuid().ToString(),
                AmountDelta = 124,
                Reason      = "integration tests: withdraw",
            });

            var eventTask = await Task.WhenAny(
                RabbitUtil.WaitForMessage <WithdrawalSucceededEvent>(m => m.OperationId == operationId),
                RabbitUtil.WaitForMessage <WithdrawalFailedEvent>(m => m.OperationId == operationId));

            eventTask.GetType().GetGenericArguments().First().Should().Be <WithdrawalFailedEvent>();
            //eventTask.Should().BeOfType<Task<WithdrawalFailedEvent>>();

            // assert
            (await AccountHelpers.GetAccount()).Balance.Should().Be(123);
        }
 /// <summary>
 /// 根据交换机获取消息队列信息
 /// </summary>
 /// <param name="exchange">交换机</param>
 /// <returns>消息队列信息</returns>
 public RabbitMessageQueueInfo ReaderByExchange(string exchange)
 {
     return(GetMessagQueueInfoByCondition(x =>
     {
         return RabbitUtil.IsTwoExchangeEqual(exchange, x.Exchange);
     }));
 }
        public async Task IfNotEnoughBalance_ShouldFailWithdraw()
        {
            // arrange
            await TestsHelpers.EnsureAccountState(needBalance : 123);

            (await TestsHelpers.GetAccount()).Balance.Should().Be(123);

            // act

            var operationId = await ClientUtil.AccountsApi.BeginWithdraw(TestsHelpers.ClientId,
                                                                         TestsHelpers.AccountId,
                                                                         new AccountChargeManuallyRequest
            {
                AmountDelta = 124,
                Reason      = "intergational tests: withdraw",
            });

            var eventTask = await Task.WhenAny(
                RabbitUtil.WaitForCqrsMessage <WithdrawalSucceededEvent>(m => m.OperationId == operationId),
                RabbitUtil.WaitForCqrsMessage <WithdrawalFailedEvent>(m => m.OperationId == operationId));

            eventTask.Should().BeOfType <Task <WithdrawalFailedEvent> >();

            // assert
            (await TestsHelpers.GetAccount()).Balance.Should().Be(123);
        }
        /// <summary>
        /// 根据交换机获取消息队列信息
        /// </summary>
        /// <param name="exchange">交换机</param>
        /// <param name="virtualPath">虚拟路径</param>
        /// <returns>消息队列信息</returns>
        public RabbitMessageQueueInfo ReaderByExchange(string exchange, string virtualPath = RabbitConnectionInfo.DEFAULT_VIRTUAL_PATH)
        {
            var extend = ConfigUtil.CreateContainerVirtualPathDic(virtualPath);

            return(GetMessagQueueInfoByCondition(x =>
            {
                return RabbitUtil.IsTwoExchangeEqual(exchange, x.Exchange);
            }, extend));
        }
        public static async Task ChargeManually(decimal delta)
        {
            var operationId = await ClientUtil.AccountsApi.BeginChargeManually(ClientId, AccountId,
                                                                               new AccountChargeManuallyRequest
            {
                AmountDelta = delta,
                Reason      = "intergational tests"
            });

            await RabbitUtil.WaitForCqrsMessage <AccountBalanceChangedEvent>(m => m.OperationId == operationId);
        }
        /// <summary>
        /// 根据交换机和队列获取消息队列信息
        /// </summary>
        /// <param name="exchange">交换机</param>
        /// <param name="queue">队列</param>
        /// <returns>消息队列信息</returns>
        public RabbitMessageQueueInfo ReaderByExchangeAndQueue(string exchange, string queue)
        {
            if (string.IsNullOrWhiteSpace(exchange) || string.IsNullOrWhiteSpace(queue))
            {
                return(null);
            }

            return(GetMessagQueueInfoByCondition(x =>
            {
                return RabbitUtil.IsTwoExchangeEqual(exchange, x.Exchange) && queue.Equals(x.Queue);
            }));
        }
        /// <summary>
        /// 根据交换机和队列获取消息队列信息
        /// </summary>
        /// <param name="exchange">交换机</param>
        /// <param name="queue">队列</param>
        /// <param name="virtualPath">虚拟路径</param>
        /// <returns>消息队列信息</returns>
        protected RabbitMessageQueueInfo ReaderByExchangeAndQueue(string exchange, string queue, string virtualPath = RabbitConnectionInfo.DEFAULT_VIRTUAL_PATH)
        {
            if (string.IsNullOrWhiteSpace(exchange) || string.IsNullOrWhiteSpace(queue))
            {
                return(null);
            }

            var extend = ConfigUtil.CreateContainerVirtualPathDic(virtualPath);

            return(GetMessagQueueInfoByCondition(x =>
            {
                return RabbitUtil.IsTwoExchangeEqual(exchange, x.Exchange) && queue.Equals(x.Queue);
            }, extend));
        }
        /// <summary>
        /// 发布消息
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="publishRoutingKey">发布路由键</param>
        public void Publish(byte[] message, string publishRoutingKey = null)
        {
            if (message.IsNullOrLength0())
            {
                return;
            }

            string key    = RabbitUtil.GetPublishKey(publishRoutingKey, rabbitMessageQueueInfo.RoutingKeys);
            string logMsg = string.Format("给key:{0},交换机:{1} 发送消息", key, rabbitMessageQueueInfo.Exchange);

            Log.DebugAsync(logMsg, null, typeof(RabbitProducer).Name, rabbitMessageQueueInfo.Exchange, key);

            channel.BasicPublish(rabbitMessageQueueInfo.Exchange, key, basicProperties, message);
        }
        public static async Task ChargeManually(decimal delta)
        {
            var operationId = await ClientUtil.AccountsApi.BeginChargeManually(AccountId,
                                                                               new AccountChargeManuallyRequest
            {
                OperationId   = Guid.NewGuid().ToString(),
                AmountDelta   = delta,
                Reason        = "integration tests",
                ReasonType    = AccountBalanceChangeReasonTypeContract.Manual,
                EventSourceId = Guid.NewGuid().ToString(),
            });

            await RabbitUtil.WaitForMessage <AccountChangedEvent>(m => m.BalanceChange.Id == operationId);
        }
Exemplo n.º 10
0
        public void OneTimeSetUp()
        {
            var sett             = SettingsUtil.Settings.IntegrationTestSettings;
            var connectionString = sett.Cqrs.ConnectionString;
            var eventsExchange   = $"{sett.Cqrs.EnvironmentName}.{sett.Cqrs.ContextNames.AccountsManagement}.events.exchange";

            //cqrs messages subscription
            RabbitUtil.ListenCqrsMessages <AccountChangedEvent>(connectionString, eventsExchange);

            //other messages subscription
            RabbitUtil.ListenJsonMessages <OrderHistoryEvent>(connectionString, sett.RabbitMqQueues.OrderHistory.ExchangeName);

            // RabbitMqSubscriber does not wait for a reader thread to start before returning from the Start() method
            Thread.Sleep(sett.MessagingDelay);
        }
Exemplo n.º 11
0
        public void RunBeforeAnyTests()
        {
            var sett             = SettingsUtil.Settings.MarginTradingAccountManagement.Cqrs;
            var connectionString = sett.ConnectionString;
            var eventsExchange   = $"{sett.EnvironmentName}.{sett.ContextNames.AccountsManagement}.events.exchange";

            RabbitUtil.ListenCqrsMessages <AccountBalanceChangedEvent>(connectionString, eventsExchange);
            RabbitUtil.ListenCqrsMessages <DepositSucceededEvent>(connectionString, eventsExchange);
            RabbitUtil.ListenCqrsMessages <WithdrawalSucceededEvent>(connectionString, eventsExchange);
            RabbitUtil.ListenCqrsMessages <WithdrawalFailedEvent>(connectionString, eventsExchange);

            // todo: register other messages

            // RabbitMqSubscriber does not wait for a reader thread to start before returning from the Start() method
            Thread.Sleep(500);
        }
        /// <summary>
        /// 根据交换机获取消息队列信息
        /// </summary>
        /// <param name="exchange">交换机</param>
        /// <param name="allList">所有列表</param>
        /// <returns>消息队列信息</returns>
        public static RabbitMessageQueueInfo ReaderByExchange(string exchange, IList <RabbitMessageQueueInfo> allList)
        {
            if (allList.IsNullOrCount0())
            {
                return(null);
            }

            foreach (RabbitMessageQueueInfo item in allList)
            {
                if (RabbitUtil.IsTwoExchangeEqual(exchange, item.Exchange))
                {
                    return(item);
                }
            }

            return(null);
        }
        /// <summary>
        /// 根据交换机获取消息队列信息
        /// </summary>
        /// <param name="exchange">交换机</param>
        /// <returns>消息队列信息</returns>
        public RabbitMessageQueueInfo ReaderByExchange(string exchange)
        {
            IList <RabbitMessageQueueInfo> list = dicCaches.Count == 0 ? ReaderAll() : dicCaches.ValuesToList();

            if (list.IsNullOrCount0())
            {
                return(null);
            }

            foreach (RabbitMessageQueueInfo item in list)
            {
                if (RabbitUtil.IsTwoExchangeEqual(exchange, item.Exchange))
                {
                    return(item);
                }
            }

            return(null);
        }
Exemplo n.º 14
0
        private static void Demo()
        {
            // 设置配置以支持文档加载
            var    config   = Configuration.Default.WithDefaultLoader();
            string mainSite = "https://www.cnblogs.com/";

            // 请求博客园首页
            var mainSiteDocument = BrowsingContext.New(config).OpenAsync(mainSite);

            var lastPageSelector = mainSiteDocument.Result.QuerySelector("#paging_block > .pager > a.last");
            var totlaPage        = 0;

            if (null != lastPageSelector)
            {
                Int32.TryParse(lastPageSelector.TextContent, out totlaPage);
            }

            if (totlaPage == 0)
            {
                return;
            }

            for (var i = 1; i <= totlaPage; i++)
            {
                var eachPageUrl      = $"https://www.cnblogs.com/#p{i}";
                var eachPageDocument = BrowsingContext.New(config).OpenAsync(eachPageUrl);

                var itemSelector = eachPageDocument.Result.QuerySelectorAll("#post_list > .post_item");
                foreach (var item in itemSelector)
                {
                    CNBlog cNBlog = GetCNBlog(item);
                    if (null == cNBlog)
                    {
                        continue;
                    }

                    string message = Newtonsoft.Json.JsonConvert.SerializeObject(cNBlog);
                    Console.WriteLine(message);
                    RabbitUtil.Send(message);
                }
            }
        }
Exemplo n.º 15
0
        public async Task IfEnoughBalance_ShouldWithdraw()
        {
            // arrange
            await AccountHelpers.EnsureAccountState(neededBalance : 123M);

            // act
            var operationId = await ClientUtil.AccountsApi.BeginWithdraw(AccountHelpers.AccountId,
                                                                         new AccountChargeRequest
            {
                OperationId = Guid.NewGuid().ToString(),
                AmountDelta = 123M,
                Reason      = "integration tests: withdraw",
            });

            await Task.WhenAll(
                RabbitUtil.WaitForMessage <AccountChangedEvent>(m => m.BalanceChange.Id == operationId),
                RabbitUtil.WaitForMessage <WithdrawalSucceededEvent>(m => m.OperationId == operationId));

            // assert
            (await AccountHelpers.GetAccount()).Balance.Should().Be(0);
        }
Exemplo n.º 16
0
        public async Task PositionClose_ShouldUpdateBalance(decimal delta)
        {
            // arrange
            await AccountHelpers.EnsureAccountState();

            var operationId = Guid.NewGuid().ToString();

            // act
            //todo use specific command
            CqrsUtil.SendEventToAccountManagement(new Backend.Contracts.Events.PositionClosedEvent(
                                                      accountId: AccountHelpers.AccountId,
                                                      clientId: AccountHelpers.ClientId,
                                                      positionId: operationId,
                                                      assetPairId: "testAssetPair",
                                                      balanceDelta: delta));

            await RabbitUtil.WaitForMessage <AccountChangedEvent>(m =>
                                                                  m.BalanceChange.Id == operationId + "-update-balance");

            // assert
            (await AccountHelpers.GetAccount()).Balance.Should().Be(0 + delta);
        }
Exemplo n.º 17
0
        public async Task Deposit_Success()
        {
            // arrange
            await AccountHelpers.EnsureAccountState();

            // act
            var operationId = await ClientUtil.AccountsApi.BeginDeposit(AccountHelpers.AccountId,
                                                                        new AccountChargeRequest
            {
                OperationId = Guid.NewGuid().ToString(),
                AmountDelta = 123,
                Reason      = "integration tests: deposit",
            });

            var messagesReceivedTask = Task.WhenAll(
                RabbitUtil.WaitForMessage <AccountChangedEvent>(m => m.BalanceChange.Id == operationId),
                RabbitUtil.WaitForMessage <DepositSucceededEvent>(m => m.OperationId == operationId));

            await messagesReceivedTask;

            // assert
            (await AccountHelpers.GetAccount()).Balance.Should().Be(123);
        }
        public async Task IfEnoughBalance_ShouldWithdraw()
        {
            // arrange
            await TestsHelpers.EnsureAccountState(needBalance : 123);

            // act

            var operationId = await ClientUtil.AccountsApi.BeginWithdraw(TestsHelpers.ClientId,
                                                                         TestsHelpers.AccountId,
                                                                         new AccountChargeManuallyRequest
            {
                AmountDelta = 123,
                Reason      = "intergational tests: withdraw",
            });

            var messagesReceivedTask = Task.WhenAll(
                RabbitUtil.WaitForCqrsMessage <AccountBalanceChangedEvent>(m => m.OperationId == operationId),
                RabbitUtil.WaitForCqrsMessage <WithdrawalSucceededEvent>(m => m.OperationId == operationId));

            await messagesReceivedTask;

            // assert
            (await TestsHelpers.GetAccount()).Balance.Should().Be(0);
        }