public void TestDistributedQueue()
        {
            ZKDistributedQueue <long> distributedQueue = new ZKDistributedQueue <long>(_zkClient, "/queue");

            distributedQueue.Offer(17);
            distributedQueue.Offer(18);
            distributedQueue.Offer(19);

            Assert.True((17 == distributedQueue.Poll()));
            Assert.True((18 == distributedQueue.Poll()));
            Assert.True((19 == distributedQueue.Poll()));
            Assert.Zero(distributedQueue.Poll());
        }
Пример #2
0
        public async Task TestMultipleReadingThreads()
        {
            using (_zkClient = new ZKClient(TestUtil.zkServers))
            {
                await TestUtil.ReSetPathUnCreate(_zkClient, queuePath);

                await _zkClient.CreateRecursiveAsync(queuePath, null, CreateMode.PERSISTENT);

                var queue = new ZKDistributedQueue <long>(_zkClient, queuePath);

                // insert 100 elements
                for (int i = 0; i < 100; i++)
                {
                    await queue.OfferAsync(i);
                }
                // 3 reading threads
                ConcurrentHashSet <long?> readElements = new ConcurrentHashSet <long?>();
                var tasks = new Task[3];
                List <Exception> exceptions = new List <Exception>();
                for (int i = 0; i < tasks.Length; i++)
                {
                    tasks[i] = Task.Run(async() =>
                    {
                        try
                        {
                            while (true)
                            {
                                long?value = await queue.PollAsync();
                                if (!value.HasValue)
                                {
                                    return;
                                }
                                readElements.Add(value);
                            }
                        }
                        catch (Exception e)
                        {
                            exceptions.Add(e);
                        }
                    });
                }

                Task.WaitAll(tasks);

                Assert.True((0 == exceptions.Count));
                Assert.True((100 == readElements.Count));
            }
        }
        public void TestMultipleReadingThreads()
        {
            ZKDistributedQueue <long?> distributedQueue = new ZKDistributedQueue <long?>(_zkClient, "/queue");

            // insert 100 elements
            for (int i = 0; i < 100; i++)
            {
                distributedQueue.Offer(i);
            }
            // 3 reading threads
            ConcurrentHashSet <long?> readElements = new ConcurrentHashSet <long?>();
            List <Thread>             threads      = new List <Thread>();
            List <Exception>          exceptions   = new List <Exception>();

            for (int i = 0; i < 3; i++)
            {
                Thread thread = new Thread(() =>
                {
                    try
                    {
                        while (true)
                        {
                            long?value = distributedQueue.Poll();
                            if (!value.HasValue)
                            {
                                return;
                            }
                            readElements.Add(value);
                        }
                    }
                    catch (Exception e)
                    {
                        exceptions.Add(e);
                    }
                });
                threads.Add(thread);
                thread.Start();
            }

            foreach (Thread thread in threads)
            {
                thread.Join();
            }

            Assert.True((0 == exceptions.Count));
            Assert.True((100 == readElements.Count));
        }
Пример #4
0
        public async Task TestPeek()
        {
            using (_zkClient = new ZKClient(TestUtil.zkServers))
            {
                await TestUtil.ReSetPathUnCreate(_zkClient, queuePath);

                await _zkClient.CreateRecursiveAsync(queuePath, null, CreateMode.PERSISTENT);

                var queue = new ZKDistributedQueue <long>(_zkClient, queuePath);;
                await queue.OfferAsync(17);

                await queue.OfferAsync(18);

                Assert.True((17 == await queue.PeekAsync()));
                Assert.True((17 == await queue.PeekAsync()));
                Assert.True((17 == await queue.PollAsync()));
                Assert.True((18 == await queue.PeekAsync()));
                Assert.True((18 == await queue.PollAsync()));
                Assert.True(0 == await queue.PollAsync());
            }
        }