예제 #1
0
        public async Task await_enqueue_signal()
        {
            var mockQueueAwaiter = new Mock <IQueueAwaiter>();
            var queue            = new SortingQueue(mockQueueAwaiter.Object, 1);

            await queue.AwaitEnqueueSignalAsync();

            mockQueueAwaiter.Verify(x => x.AwaitEnqueueSignalAsync());
        }
예제 #2
0
        public async Task honor_max_queue_size()
        {
            var cts                    = new CancellationTokenSource(10000);
            var maxQueueSize           = 2;
            var mockQueueAwaiter       = new Mock <IQueueAwaiter>();
            var queue                  = new SortingQueue(mockQueueAwaiter.Object, maxQueueSize);
            var subscriberEvent1       = new SubscriberEvent(null, null, 0, null, null);
            var subscriberEvent2       = new SubscriberEvent(null, null, 0, null, null);
            var subscriberEvent3       = new SubscriberEvent(null, null, 0, null, null);
            var enqueueuSignalSetCount = 0;
            var awaitingDequeueSignal  = new ManualResetEventSlim(true);
            var mockDequeueSignal      = new ManualResetEventSlim(false);

            mockQueueAwaiter.Setup(x => x.SetEnqueueSignal()).Callback(() => enqueueuSignalSetCount++);
            mockQueueAwaiter.Setup(x => x.AwaitDequeueSignalAsync()).Callback(() => awaitingDequeueSignal.Set()).Returns(mockDequeueSignal.WaitHandle.AsTask());

            await queue.EnqueueWithWaitAsync(subscriberEvent1, cts.Token);

            await queue.EnqueueWithWaitAsync(subscriberEvent2, cts.Token);

            var enqueueTask = queue.EnqueueWithWaitAsync(subscriberEvent3, cts.Token);

            await Task.WhenAny(new[] { awaitingDequeueSignal.WaitHandle.AsTask(), cts.Token.WaitHandle.AsTask() });

            if (cts.Token.IsCancellationRequested)
            {
                throw new TimeoutException();
            }

            Assert.Equal(2, enqueueuSignalSetCount);
            Assert.Equal(maxQueueSize, queue.QueueCount);

            SubscriberEvent se1;

            queue.TryDequeue(out se1);
            Assert.Equal(maxQueueSize - 1, queue.QueueCount);

            mockDequeueSignal.Set();

            await Task.WhenAny(new[] { enqueueTask, cts.Token.WaitHandle.AsTask() });

            if (cts.Token.IsCancellationRequested)
            {
                throw new TimeoutException();
            }

            Assert.Equal(maxQueueSize, queue.QueueCount);
        }
예제 #3
0
        public async Task enqueue_and_dequeue_single_item()
        {
            var cts              = new CancellationTokenSource(10000);
            var maxQueueSize     = 1;
            var mockQueueAwaiter = new Mock <IQueueAwaiter>();
            var queue            = new SortingQueue(mockQueueAwaiter.Object, maxQueueSize);
            var subscriberEvent  = new SubscriberEvent(null, null, 0, null, null);

            await queue.EnqueueWithWaitAsync(subscriberEvent, cts.Token);

            SubscriberEvent dequeuedSubscriberEvent;

            var tryResult = queue.TryDequeue(out dequeuedSubscriberEvent);

            Assert.True(tryResult);
            Assert.Equal(subscriberEvent, dequeuedSubscriberEvent);
        }
예제 #4
0
        public async Task set_queue_signals()
        {
            var cts              = new CancellationTokenSource(10000);
            var maxQueueSize     = 1;
            var mockQueueAwaiter = new Mock <IQueueAwaiter>();
            var queue            = new SortingQueue(mockQueueAwaiter.Object, maxQueueSize);
            var subscriberEvent  = new SubscriberEvent(null, null, 0, null, null);

            SubscriberEvent se1;

            queue.TryDequeue(out se1);

            mockQueueAwaiter.VerifyNoOtherCalls();

            await queue.EnqueueWithWaitAsync(subscriberEvent, cts.Token);

            mockQueueAwaiter.Verify(x => x.SetEnqueueSignal());
            mockQueueAwaiter.VerifyNoOtherCalls();

            SubscriberEvent se2;

            queue.TryDequeue(out se2);
            mockQueueAwaiter.Verify(x => x.SetDequeueSignal());
        }
예제 #5
0
 public LibrarySorter(Library library, AnimeParser parser)
 {
     library_ = library;
     parser_ = parser;
     queue_ = new SortingQueue();
 }
예제 #6
0
    public static ArrayList FindPath(Node startNode, Node endNode)
    {
        int findCount = 0;

        // 탐색을 위한 Node를 담을 Queue 설정
        openQueue = new SortingQueue <Node>();
        openQueue.Enqueue(startNode);

        startNode.gScore = 0f;
        startNode.hScore = GetPostionScore(startNode, endNode);

        //탐색이 끝난 Node를 담을 Queue 설정, 자기가 있는 중심축
        closedQueue = new SortingQueue <Node>();

        Node node = null;

        while (openQueue.Count != 0)
        {
            //Queue는 먼저 들어간게 먼저 나온다.
            node = openQueue.Dequeue();

            // 목적지를 찾았다면
            if (node == endNode)
            {
                Debug.Log("Find: " + findCount);
                return(GetReverseResult(node));
            }

            // Node를 기준으로 갈수있는 주변 길 찾기
            ArrayList availableNodes = GameController.Instance.GetAvailableNodes(node);

            foreach (Node availableNode in availableNodes)
            {
                //이미 찾았던 길이 아니라면 openQueue에 추가
                if (!closedQueue.Contains(availableNode))
                {
                    //다른애가 오픈큐에 넣었었는데 다시 똑같은 길을 찾아서 오픈큐에 넣으려는 중복을 방지하도록 처리
                    if (openQueue.Contains(availableNode))
                    {
                        float score     = GetPostionScore(node, availableNode);
                        float newGScore = node.gScore + score;

                        //더 가까운 길이라면
                        if (availableNode.gScore > newGScore)
                        {
                            availableNode.gScore = newGScore;
                            availableNode.parent = node;
                        }
                    }
                    else
                    {
                        float score = GetPostionScore(node, availableNode);

                        float newGScore = node.gScore + score;
                        float newHScore = GetPostionScore(availableNode, endNode);

                        availableNode.gScore = newGScore;
                        availableNode.hScore = newHScore;
                        //GScore에 따라 부모가 바뀜
                        availableNode.parent = node;

                        openQueue.Enqueue(availableNode);
                        findCount++;
                    }
                }
            }
            closedQueue.Enqueue(node);
        }

        if (node == endNode)
        {
            Debug.Log("Find: " + findCount);
            return(GetReverseResult(node));
        }

        return(null);
    }
예제 #7
0
    public static ArrayList FindPath(Node startNode, Node endNode)
    {
        int findCount = 0;

        // 탐색을 위한 Node를 담을 Queue 설정
        openQueue = new SortingQueue <Node>();
        openQueue.Enqueue(startNode);

        startNode.gScore = 0f;
        startNode.hScore = GetPostionScore(startNode, endNode);

        // 탐색이 끝난 Node를 담을 Queue 설정
        closedQueue = new SortingQueue <Node>();

        Node node = null;

        while (openQueue.Count != 0)
        {
            node = openQueue.Dequeue();

            // 목적지를 찾았다면
            if (node == endNode)
            {
                Debug.Log("Find: " + findCount);
                return(GetReverseResult(node));
            }

            // Node를 기준으로 갈수있는 주변 길 찾기
            ArrayList availableNodes = GameController.Instance.GetAvailableNodes(node);

            foreach (Node availableNode in availableNodes)
            {
                if (!closedQueue.Contains(availableNode))
                {
                    if (openQueue.Contains(availableNode))
                    {
                        float score     = GetPostionScore(node, availableNode);
                        float newGScore = node.gScore + score;

                        if (availableNode.gScore > newGScore)
                        {
                            availableNode.gScore = newGScore;
                            availableNode.parent = node;
                        }
                    }
                    else
                    {
                        float score = GetPostionScore(node, availableNode);

                        float newGScore = node.gScore + score;
                        float newHScore = GetPostionScore(availableNode, endNode);

                        availableNode.gScore = newGScore;
                        availableNode.hScore = newHScore;
                        availableNode.parent = node;

                        openQueue.Enqueue(availableNode);
                        findCount++;
                    }
                }
            }
            closedQueue.Enqueue(node);
        }

        if (node == endNode)
        {
            Debug.Log("Find: " + findCount);
            return(GetReverseResult(node));
        }

        return(null);
    }