public void TestReciveSendMessageBlockWcf()
        {
            const string BaseAddress     = "http://*****:*****@"<s:Envelope xmlns:s=""http://www.w3.org/2003/05/soap-envelope"">
                                <s:Header />
                                <s:Body>
                                    <AcceptMessageResponse xmlns=""http://tempuri.org/"" />
                                </s:Body>
                            </s:Envelope>"));
                        });
                    }))
                    {
                        int timeout = sendingManager.ScanningPeriodMilliseconds + SendTimeout;

                        var recievingThread = new Thread(asyncReciever.AsyncRecieve);

                        sendingManager.Prepare();
                        sendingManager.Start();

                        try
                        {
                            var sw = new Stopwatch();

                            bool sendingStarted = false;
                            int  n = 0;

                            recievingThread.Start();

                            while (true)
                            {
                                var now = DateTime.Now;

                                // wait for the callback
                                resetEvent.WaitOne(timeout);

                                // synchronous waiting time measurement
                                double waitingTime = (DateTime.Now - now).TotalMilliseconds;

                                if (!sendingStarted)
                                {
                                    timeout        = SendTimeout;
                                    sendingStarted = true;
                                    sw.Start();
                                }

                                if (exception != null)
                                {
                                    throw exception;
                                }

                                var sendedCount = sendedMessages.Count;

                                if (sendedCount > BlockSize)
                                {
                                    // the test will fail at Assert block
                                    break;
                                }

                                if (sendedCount == n)
                                {
                                    if (waitingTime < SendTimeout)
                                    {
                                        continue;
                                    }

                                    if (sendedCount < BlockSize)
                                    {
                                        throw new TimeoutException($"{dataService.ToString()}. Время ожидания callback-вызова превысило {SendTimeout} мсек.");
                                    }

                                    // the test result will be determined at Assert block
                                    break;
                                }

                                n = sendedCount;
                            }

                            sw.Stop();
                            sendingTime = sw.Elapsed.TotalMilliseconds - SendTimeout;
                        }
                        catch (Exception ex)
                        {
                            asyncReciever.SetStopFlag();
                            exception = exception ?? ex;
                        }
                        finally
                        {
                            sendingManager.Stop();
                            sendingManager.AfterStop();
                        }

                        recievingThread.Join();

                        if (exception != null)
                        {
                            throw exception;
                        }
                    }

                // Assert.
                Assert.False(sendedMessages.Count > BlockSize, $"{ dataService.ToString()}. Количество отправленных сообщений превысило количество полученных сообщений.");

                var diagnosticMessage = $"{dataService.ToString()}:";
                diagnosticMessage += $"{Environment.NewLine}  Recieving time {asyncReciever.RecievingTime.ToString()} ms.";
                diagnosticMessage += $"{Environment.NewLine}  Sending time {sendingTime.ToString()} ms.";
                Console.WriteLine(diagnosticMessage);
            }
        }
        public void TestDeletingUndeliveredMessageWithGroup()
        {
            foreach (var dataService in DataServices)
            {
                // Arrange.
                var messageType = new MessageType()
                {
                    ID = "messageTypeId"
                };
                var sender = new Client()
                {
                    ID = "senderId"
                };
                var recipient = new Client()
                {
                    ID = "recipientId", Address = "http://localhost:2525/Message"
                };
                var messageWithoutSubscription = new Message()
                {
                    Recipient = sender, MessageType = messageType, SendingTime = DateTime.Now, Group = "group"
                };
                var firstMessage = new Message()
                {
                    Recipient = recipient, MessageType = messageType, SendingTime = DateTime.Now, Group = "group"
                };
                var lastMessage = new Message()
                {
                    Recipient = recipient, MessageType = messageType, SendingTime = DateTime.Now.AddSeconds(1), Group = "group"
                };
                var subscription = new Subscription()
                {
                    Client = recipient, MessageType = messageType, IsCallback = true, TransportType = TransportType.HTTP, ExpiryDate = DateTime.Now.AddDays(1)
                };
                var dataObjects = new DataObject[] { messageType, sender, recipient, messageWithoutSubscription, firstMessage, lastMessage, subscription };

                dataService.UpdateObjects(ref dataObjects);

                var statisticsService = GetMockStatisticsService();
                var component         = new OptimizedSendingManager(
                    new DefaultSubscriptionsManager(dataService, statisticsService),
                    statisticsService,
                    dataService,
                    GetMockLogger())
                {
                    MaxTasks = 1,
                    ScanningPeriodMilliseconds = 5000
                };

                // Act.
                using (WebApp.Start("http://localhost:2525/Message", builder => builder.Run(context => throw new Exception())))
                {
                    component.Prepare();
                    component.Start();
                    Thread.Sleep(component.ScanningPeriodMilliseconds);
                    component.Stop();
                    component.AfterStop();
                }

                // Assert.
                Assert.Equal(2, ((SQLDataService)dataService).Query <Message>().Count());
            }
        }