예제 #1
0
        public void TestBatch()
        {
            using (var requestHandler = new TestRequestHandler())
                using (var client = new RingMasterClient(requestHandler))
                {
                    string path       = this.GetRandomString();
                    var    version    = this.GetRandomInt();
                    var    operations = new List <Op>();
                    operations.Add(Op.Delete(path, version, recursive: false));

                    var expectedResults = new List <OpResult>();
                    expectedResults.Add(new OpResult.DeleteResult());

                    requestHandler.Implementation = request =>
                    {
                        Assert.IsTrue(request is RequestBatch);
                        var batchRequest = (RequestBatch)request;
                        Assert.IsTrue(batchRequest.CompleteSynchronously);
                        Assert.AreEqual(operations.Count, batchRequest.Requests.Count);
                        Assert.IsTrue(batchRequest.Requests[0] is RequestDelete);

                        return(new RequestResponse()
                        {
                            ResultCode = (int)RingMasterException.Code.Ok,
                            Content = expectedResults
                        });
                    };

                    var results = client.Batch(operations, mustCompleteSynchronously: true).Result;
                    Assert.AreEqual(results.Count, expectedResults.Count);
                    Assert.IsTrue(results[0] is OpResult.DeleteResult);

                    try
                    {
                        client.Batch(null, mustCompleteSynchronously: false).Wait();
                        Assert.Fail("Batch call must have thrown ArgumentNullException");
                    }
                    catch (ArgumentException)
                    {
                    }
                }
        }
예제 #2
0
        public void TestConcurrentDelete()
        {
            TestAsync().GetAwaiter().GetResult();

            async Task TestAsync()
            {
                const int    ChildrenCount = 1000;
                const string RootName      = nameof(this.TestConcurrentDelete);
                const int    threadCount   = 64;

                using (var client = new RingMasterClient(serverAddress, null, null, 10000))
                {
                    await client.Create($"/{RootName}", null, null, CreateMode.PersistentAllowPathCreation).ConfigureAwait(false);

                    var ops = new List <Op>(ChildrenCount);
                    for (int count = 0; count < ChildrenCount; count++)
                    {
                        ops.Add(Op.Create($"/{RootName}/{count}", null, null, CreateMode.PersistentAllowPathCreation));
                    }

                    await client.Batch(ops).ConfigureAwait(false);
                }

                for (int i = 0; i < threadCount; i++)
                {
                    var deleteChildTask = this.DeleteChild(RootName, ChildrenCount);
                    var deleteParent    = Task.Run(async() =>
                    {
                        using (var client = new RingMasterClient(serverAddress, null, null, 10000))
                        {
                            await client.Delete($"/{RootName}", -1, DeleteMode.None);
                        }
                    });
                }
            }
        }
예제 #3
0
        public void TestGetFullSubtreeWhileUpdating()
        {
            TestAsync().GetAwaiter().GetResult();

            async Task TestAsync()
            {
                const int    InitialNodeData = 1;
                const int    NewNodeData     = 2;
                const int    ChildrenCount   = 50000;
                const string RootName        = nameof(this.TestGetFullSubtreeWhileUpdating);

                using (var client = new RingMasterClient(serverAddress, null, null, 100000))
                {
                    byte[] data = BitConverter.GetBytes(InitialNodeData);
                    await client.Create($"/{RootName}/node1", data, null, CreateMode.PersistentAllowPathCreation).ConfigureAwait(false);

                    await client.Create($"/{RootName}/node2", data, null, CreateMode.PersistentAllowPathCreation).ConfigureAwait(false);

                    await client.Create($"/{RootName}/node3", data, null, CreateMode.PersistentAllowPathCreation).ConfigureAwait(false);

                    var ops = new List <Op>(ChildrenCount);
                    for (int count = 0; count < ChildrenCount; count++)
                    {
                        ops.Add(Op.Create($"/{RootName}/node2/{count}", data, null, CreateMode.PersistentAllowPathCreation));
                    }

                    await client.Batch(ops).ConfigureAwait(false);
                }

                ManualResetEvent manualResetEvent = new ManualResetEvent(false);
                Task <TreeNode>  getSubtreeTask   = new Task <TreeNode>(() =>
                {
                    using (var client = new RingMasterClient(serverAddress, null, null, 10000))
                    {
                        return(client.GetFullSubtree($"/{RootName}").Result);
                    }
                });

                Task updateDataTask = Task.Run(async() =>
                {
                    using (var client = new RingMasterClient(serverAddress, null, null, 10000))
                    {
                        var ops        = new List <Op>(2);
                        byte[] newData = BitConverter.GetBytes(NewNodeData);

                        ops.Add(Op.SetData($"/{RootName}/node1", newData, -1));
                        ops.Add(Op.SetData($"/{RootName}/node3", newData, -1));

                        manualResetEvent.WaitOne();

                        // this is to make sure the set data occurs after get full substree started.
                        Thread.Sleep(20);
                        await client.Batch(ops).ConfigureAwait(false);
                    }
                });

                getSubtreeTask.Start();
                manualResetEvent.Set();

                await Task.WhenAll(getSubtreeTask, updateDataTask);

                var tree      = getSubtreeTask.Result;
                int node1Data = BitConverter.ToInt32(tree.Children[0].Data, 0);
                int node3Data = BitConverter.ToInt32(tree.Children[2].Data, 0);

                Assert.IsTrue(node1Data >= node3Data);
            }
        }