Esempio n. 1
0
        private async Task createNremoveMelementTest(string dir, int n, int m)
        {
            const string testString  = "Hello World";
            const int    num_clients = 2;

            ZooKeeper[]        clients      = new ZooKeeper[num_clients];
            DistributedQueue[] queueHandles = new DistributedQueue[num_clients];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i] = await createClient();

                queueHandles[i] = new DistributedQueue(clients[i], dir, null);
            }

            for (int i = 0; i < n; i++)
            {
                string offerString = testString + i;
                await  queueHandles[0].offer(offerString.UTF8getBytes());
            }

            for (int i = 0; i < m; i++)
            {
                await queueHandles[1].remove();
            }
            Assert.assertEquals((await queueHandles[1].element()).UTF8bytesToString(), testString + m);
        }
Esempio n. 2
0
        private async Task createNremoveMtest(string dir, int n, int m)
        {
            const string testString  = "Hello World";
            const int    num_clients = 2;

            ZooKeeper[]        clients      = new ZooKeeper[num_clients];
            DistributedQueue[] queueHandles = new DistributedQueue[num_clients];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i] = await createClient();

                queueHandles[i] = new DistributedQueue(clients[i], dir, null);
            }

            for (int i = 0; i < n; i++)
            {
                string offerString = testString + i;
                await  queueHandles[0].offer(offerString.UTF8getBytes());
            }

            byte[] data = null;
            for (int i = 0; i < m; i++)
            {
                data = await queueHandles[1].remove();
            }
            // ReSharper disable once AssignNullToNotNullAttribute
            Assert.assertEquals(data.UTF8bytesToString(), testString + (m - 1));
        }
Esempio n. 3
0
        private void createNremoveMelementTest(string dir, int n, int m)
        {
            const string testString  = "Hello World";
            const int    num_clients = 2;

            ZooKeeper[]        clients      = new ZooKeeper[num_clients];
            DistributedQueue[] queueHandles = new DistributedQueue[num_clients];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i]      = createClient();
                queueHandles[i] = new DistributedQueue(clients[i], dir, null);
            }

            for (int i = 0; i < n; i++)
            {
                string offerString = testString + i;
                queueHandles[0].offer(offerString.getBytes()).GetAwaiter().GetResult();
            }

            for (int i = 0; i < m; i++)
            {
                queueHandles[1].remove().GetAwaiter().GetResult();
            }
            Assert.assertEquals(Encoding.UTF8.GetString(queueHandles[1].element().GetAwaiter().GetResult()), testString + m);
        }
Esempio n. 4
0
        public void testTakeWait2()
        {
            const string dir         = "/testTakeWait2";
            const string testString  = "Hello World";
            const int    num_clients = 1;

            ZooKeeper[]        clients      = new ZooKeeper[num_clients];
            DistributedQueue[] queueHandles = new DistributedQueue[num_clients];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i]      = createClient();
                queueHandles[i] = new DistributedQueue(clients[i], dir, null);
            }
            const int num_attempts = 2;

            for (int i = 0; i < num_attempts; i++)
            {
                byte[][] takeResult       = new byte[1][];
                string   threadTestString = testString + i;
                Thread   takeThread       = new Thread(() =>
                {
                    try {
                        takeResult[0] = queueHandles[0].take().GetAwaiter().GetResult();
                    }
                    catch (KeeperException) {
                    }
                    catch (ThreadInterruptedException) {
                    }
                });
                takeThread.Start();

                Thread.Sleep(1000);
                Thread offerThread = new Thread(() =>
                {
                    try {
                        queueHandles[0].offer(threadTestString.getBytes()).GetAwaiter().GetResult();
                    }
                    catch (KeeperException) {
                    }
                    catch (ThreadInterruptedException) {
                    }
                });
                offerThread.Start();
                offerThread.Join();

                takeThread.Join();

                Assert.assertTrue(takeResult[0] != null);
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.assertEquals(Encoding.UTF8.GetString(takeResult[0]), threadTestString);
            }
        }
Esempio n. 5
0
        public async Task testTakeWait2()
        {
            const string dir         = "/testTakeWait2";
            const string testString  = "Hello World";
            const int    num_clients = 1;

            ZooKeeper[]        clients      = new ZooKeeper[num_clients];
            DistributedQueue[] queueHandles = new DistributedQueue[num_clients];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i] = await createClient();

                queueHandles[i] = new DistributedQueue(clients[i], dir, null);
            }
            const int num_attempts = 2;

            for (int i = 0; i < num_attempts; i++)
            {
                byte[][] takeResult       = new byte[1][];
                string   threadTestString = testString + i;
                Task     takeTask         = Task.Run(async() =>
                {
                    try {
                        takeResult[0] = await queueHandles[0].take();
                    }
                    catch (KeeperException) {
                    }
                });

                await Task.Delay(1000);

                Task offerTask = Task.Run(async() =>
                {
                    try {
                        await queueHandles[0].offer(threadTestString.UTF8getBytes());
                    }
                    catch (KeeperException) {
                    }
                });
                await offerTask;

                await takeTask;

                Assert.assertTrue(takeResult[0] != null);
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.assertEquals(takeResult[0].UTF8bytesToString(), threadTestString);
            }
        }
Esempio n. 6
0
        public void testTake1()
        {
            const string dir         = "/testTake1";
            const string testString  = "Hello World";
            const int    num_clients = 1;

            ZooKeeper[]        clients      = new ZooKeeper[num_clients];
            DistributedQueue[] queueHandles = new DistributedQueue[num_clients];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i]      = createClient();
                queueHandles[i] = new DistributedQueue(clients[i], dir, null);
            }

            queueHandles[0].offer(testString.getBytes()).GetAwaiter().GetResult();

            byte[] dequeuedBytes = queueHandles[0].take().GetAwaiter().GetResult();
            Assert.assertEquals(Encoding.UTF8.GetString(dequeuedBytes), testString);
        }
Esempio n. 7
0
        public async Task testTake1()
        {
            const string dir         = "/testTake1";
            const string testString  = "Hello World";
            const int    num_clients = 1;

            ZooKeeper[]        clients      = new ZooKeeper[num_clients];
            DistributedQueue[] queueHandles = new DistributedQueue[num_clients];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i] = await createClient();

                queueHandles[i] = new DistributedQueue(clients[i], dir, null);
            }

            await queueHandles[0].offer(testString.UTF8getBytes());

            byte[] dequeuedBytes = await queueHandles[0].take();
            Assert.assertEquals(dequeuedBytes.UTF8bytesToString(), testString);
        }
Esempio n. 8
0
        public void testRemove1()
        {
            const string dir         = "/testRemove1";
            const int    num_clients = 1;

            ZooKeeper[]        clients      = new ZooKeeper[num_clients];
            DistributedQueue[] queueHandles = new DistributedQueue[num_clients];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i]      = createClient();
                queueHandles[i] = new DistributedQueue(clients[i], dir, null);
            }

            try {
                queueHandles[0].remove().GetAwaiter().GetResult();
            }
            catch (InvalidOperationException) {
                return;
            }
            Assert.assertTrue(false);
        }