Exemplo n.º 1
0
        /// <summary>
        /// Запросить у улья очередь поста охраны и начать работу.
        /// </summary>
        private void RequestGuardPostQueue()
        {
            var request = new BeeRequest <GuardPostQueue>
            {
                RequestType = BeeRequestType.RequestGuardPostQueue
            };

            this.SafeSendRequest(request);

            if (!request.Succeed ||
                request.TypedResponse == null)
            {
                if (!this.isWorking)
                {
                    return;
                }

                throw new InvalidOperationException(
                          "Не удалось получить очередь поста охраны пчёл.");
            }

            this.guardPostQueue = request.TypedResponse;

            this.CheckOneBee();
        }
        public void GuardBee_WorkExpectedTime()
        {
            GuardBee bee = new GuardBee();

            GuardPostQueue queue = new GuardPostQueue();

            for (int i = 0; i < 1000; i++)
            {
                queue.Enqueue(new EmptyBaseBee());
            }

            int operationsCount = 0;

            bee.ActionPerformed += (sender, args) =>
            {
                if (args.ActionType == BeeActionType.AcceptBeeToEnter)
                {
                    Interlocked.Increment(ref operationsCount);
                }
            };

            bee.RequestForBeehiveData += (sender, args) =>
            {
                if (args.RequestType == BeeRequestType.RequestGuardPostQueue)
                {
                    args.Response = queue;
                }

                args.Succeed = true;
            };

            bee.StartWork();

            Task.Delay((int)this.balance.GuardBalance.TimeToCheckOneBee.TotalMilliseconds * 50)
            .GetAwaiter().GetResult();

            bee.StopWork();

            Assert.IsTrue(Math.Abs(50 - operationsCount) <= (50 * GuardBeeTests.Inaccuracy));
        }