Ejemplo n.º 1
0
        public void KeyNotFoundIsNotCountingAsErrors()
        {
            var data = new ConcurrentDictionary <BobKey, byte[]>();

            data[BobKey.FromUInt64(1)] = new byte[] { 1, 2, 3 };

            var behaviour = new BobNodeClientMockHelper.MockClientBehaviour();
            var stat      = new BobNodeClientMockHelper.MockClientStat();

            using (var client = BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour, stat))
            {
                client.Open();
                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);

                try
                {
                    client.Get(BobKey.FromUInt64(100));
                }
                catch (BobKeyNotFoundException)
                {
                }

                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);
            }
        }
Ejemplo n.º 2
0
        public void NoInfiniteRetriesTest()
        {
            byte[] defaultData = new byte[] { 1, 2, 3 };
            var    data        = new ConcurrentDictionary <BobKey, byte[]>();

            var behaviour1 = new BobNodeClientMockHelper.MockClientBehaviour();
            var stat1      = new BobNodeClientMockHelper.MockClientStat();
            var behaviour2 = new BobNodeClientMockHelper.MockClientBehaviour();
            var stat2      = new BobNodeClientMockHelper.MockClientStat();

            BobNodeClient[] clients = new BobNodeClient[]
            {
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: behaviour1, stat: stat1),
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: behaviour2, stat: stat2)
            };

            behaviour1.ErrorStatus = new Grpc.Core.Status(Grpc.Core.StatusCode.Internal, "Internal error");
            behaviour2.ErrorStatus = new Grpc.Core.Status(Grpc.Core.StatusCode.Internal, "Internal error");

            using (var client = new BobClusterClient(clients, SequentialNodeSelectionPolicy.Factory, operationRetryCount: 10))
            {
                Assert.Throws <BobOperationException>(() => client.Put(BobKey.FromUInt64(1), defaultData));
                Assert.Throws <BobOperationException>(() => client.Get(BobKey.FromUInt64(1)));
            }
        }
Ejemplo n.º 3
0
        public void ConnectingStateTest()
        {
            var data = new ConcurrentDictionary <BobKey, byte[]>();

            data[BobKey.FromUInt64(1)] = new byte[] { 1, 2, 3 };

            var behaviour = new BobNodeClientMockHelper.MockClientBehaviour();

            using (var client = BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour))
            {
                Assert.Equal(BobNodeClientState.Idle, client.State);

                behaviour.Pause.Reset();

                Task asyncOp = Task.Factory.StartNew(() => client.Open(), TaskCreationOptions.LongRunning);

                for (int i = 0; i < 1000 && client.State == BobNodeClientState.Idle; i++)
                {
                    Thread.Sleep(10);
                }

                Assert.Equal(BobNodeClientState.Connecting, client.State);

                behaviour.Pause.Set();
                asyncOp.Wait();

                Assert.Equal(BobNodeClientState.Ready, client.State);
            }
        }
Ejemplo n.º 4
0
        public void ConnectingToFailedStateTest()
        {
            var data = new ConcurrentDictionary <BobKey, byte[]>();

            data[BobKey.FromUInt64(1)] = new byte[] { 1, 2, 3 };

            var behaviour = new BobNodeClientMockHelper.MockClientBehaviour();

            using (var client = BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour))
            {
                Assert.Equal(BobNodeClientState.Idle, client.State);

                behaviour.Pause.Reset();
                behaviour.ErrorStatus = new Grpc.Core.Status(Grpc.Core.StatusCode.Internal, "Internal error");

                Task asyncOp = Task.Factory.StartNew(() =>
                {
                    try { client.Open(); }
                    catch { }
                }, TaskCreationOptions.LongRunning);

                for (int i = 0; i < 1000 && client.State == BobNodeClientState.Idle; i++)
                {
                    Thread.Sleep(10);
                }

                Assert.Equal(BobNodeClientState.Connecting, client.State);

                behaviour.Pause.Set();
                asyncOp.Wait();

                Assert.Equal(BobNodeClientState.TransientFailure, client.State);
            }
        }
Ejemplo n.º 5
0
        public void TimeSinceLastOperationTest()
        {
            var data = new ConcurrentDictionary <BobKey, byte[]>();

            data[BobKey.FromUInt64(1)] = new byte[] { 1, 2, 3 };

            var behaviour = new BobNodeClientMockHelper.MockClientBehaviour();
            var stat      = new BobNodeClientMockHelper.MockClientStat();

            using (var client = BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour, stat))
            {
                client.Open();

                client.Put(BobKey.FromUInt64(10), new byte[] { 1, 2, 3 });

                int startTick = Environment.TickCount;
                for (int i = 0; i < 100; i++)
                {
                    Thread.Sleep(10);
                    int elapsed = unchecked (Environment.TickCount - startTick);
                    Assert.InRange(client.TimeSinceLastOperationMs, elapsed - 10, elapsed + 100);
                }

                client.Get(BobKey.FromUInt64(10));
                Assert.True(client.TimeSinceLastOperationMs < 100);
            }
        }
Ejemplo n.º 6
0
        public void PutGetExistOperationGuidTest()
        {
            byte[] defaultData = new byte[] { 1, 2, 3 };
            var    data        = new ConcurrentDictionary <BobKey, byte[]>();

            Guid guid1 = Guid.NewGuid();
            Guid guid2 = Guid.NewGuid();

            Guid[] guidArray = Enumerable.Range(0, 1000).Select(o => Guid.NewGuid()).ToArray();

            using (var client = new BobNodeClient <Guid>(BobNodeClientMockHelper.CreateMockedClientWithData(data), null, null))
            {
                client.Put(guid1, defaultData);

                Assert.Equal(defaultData, client.Get(guid1));
                Assert.Throws <BobKeyNotFoundException>(() => client.Get(guid2));

                Assert.Equal(new bool[] { true, false }, client.Exists(new Guid[] { guid1, guid2 }));

                for (int i = 0; i < guidArray.Length; i++)
                {
                    client.Put(guidArray[i], defaultData);
                }
                for (int i = 0; i < guidArray.Length; i++)
                {
                    Assert.Equal(defaultData, client.Get(guidArray[i]));
                }
                Assert.All(client.Exists(guidArray), res => Assert.True(res));
                Assert.All(client.Exists(Enumerable.Range(0, 100).Select(o => Guid.NewGuid()).ToArray()), res => Assert.False(res));
            }
        }
Ejemplo n.º 7
0
        public void TimeoutIsNotCountingAsErrors()
        {
            var data = new ConcurrentDictionary <BobKey, byte[]>();

            data[BobKey.FromUInt64(1)] = new byte[] { 1, 2, 3 };

            var behaviour = new BobNodeClientMockHelper.MockClientBehaviour();
            var stat      = new BobNodeClientMockHelper.MockClientStat();

            using (var client = BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour, stat))
            {
                client.Open();
                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);

                try
                {
                    behaviour.ErrorStatus = new Grpc.Core.Status(Grpc.Core.StatusCode.DeadlineExceeded, "Deadline");
                    client.Get(BobKey.FromUInt64(1));
                }
                catch (TimeoutException)
                {
                }

                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);
            }
        }
Ejemplo n.º 8
0
        public void CancellationIsNotCountingAsErrors()
        {
            var data = new ConcurrentDictionary <BobKey, byte[]>();

            data[BobKey.FromUInt64(1)] = new byte[] { 1, 2, 3 };

            var behaviour = new BobNodeClientMockHelper.MockClientBehaviour();
            var stat      = new BobNodeClientMockHelper.MockClientStat();

            using (var client = BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour, stat))
            {
                client.Open();
                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);

                try
                {
                    CancellationTokenSource cancelled = new CancellationTokenSource();
                    cancelled.Cancel();
                    client.Get(BobKey.FromUInt64(1), cancelled.Token);
                }
                catch (OperationCanceledException)
                {
                }

                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);
            }
        }
Ejemplo n.º 9
0
        public void DisposeMockWithoutStartTest()
        {
            var data = new ConcurrentDictionary <BobKey, byte[]>();

            using (var client = BobNodeClientMockHelper.CreateMockedClientWithData(data))
            {
            }
        }
Ejemplo n.º 10
0
        public void PutGetExistOperationWithRetriesTest()
        {
            byte[] defaultData = new byte[] { 1, 2, 3 };
            var    data        = new ConcurrentDictionary <BobKey, byte[]>();

            var behaviour1 = new BobNodeClientMockHelper.MockClientBehaviour();
            var behaviour2 = new BobNodeClientMockHelper.MockClientBehaviour();

            BobNodeClient[] clients = new BobNodeClient[]
            {
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: behaviour1, stat: null),
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: behaviour2, stat: null)
            };

            behaviour1.ErrorStatus = new Grpc.Core.Status(Grpc.Core.StatusCode.Internal, "Internal error");

            using (var client = new BobClusterClient <ulong>(clients, SequentialNodeSelectionPolicy.Factory, operationRetryCount: 1, keySerializer: null, keySerializationPoolSize: null))
            {
                client.Put(1, defaultData);
                client.Put(ulong.MaxValue, defaultData);

                Assert.Equal(defaultData, client.Get(1));
                Assert.Equal(defaultData, client.Get(ulong.MaxValue));
                Assert.Throws <BobKeyNotFoundException>(() => client.Get(2));

                Assert.Equal(new bool[] { true, false }, client.Exists(new ulong[] { 1, 2 }));

                for (ulong i = 100; i < 1000; i++)
                {
                    client.Put(i, defaultData);
                }
                for (ulong i = 100; i < 1000; i++)
                {
                    Assert.Equal(defaultData, client.Get(i));
                }
                for (ulong i = 100; i < 1000; i++)
                {
                    Assert.Equal(defaultData, client.Get(i, fullGet: true, new CancellationToken()));
                }

                Assert.All(client.Exists(Enumerable.Range(100, 1000 - 100).Select(o => (ulong)o).ToArray()), res => Assert.True(res));
                Assert.All(client.Exists(Enumerable.Range(20000, 1000).Select(o => (ulong)o).ToArray(), fullGet: true, new CancellationToken()), res => Assert.False(res));

                Assert.All(client.Exists(Enumerable.Range(100, 1000 - 100).Select(o => (ulong)o).ToList()), res => Assert.True(res));
                Assert.All(client.Exists(Enumerable.Range(20000, 1000).Select(o => (ulong)o).ToList(), fullGet: true, new CancellationToken()), res => Assert.False(res));


                for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                {
                    client.Put(i, defaultData);
                }
                for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                {
                    Assert.Equal(defaultData, client.Get(i));
                }
            }
        }
Ejemplo n.º 11
0
        public async Task BasicStateTransitionTestAsync()
        {
            var data = new ConcurrentDictionary <BobKey, byte[]>();

            data[BobKey.FromUInt64(1)] = new byte[] { 1, 2, 3 };

            var stat = new BobNodeClientMockHelper.MockClientStat();

            using (var client = BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: null, stat: stat))
            {
                Assert.Equal(BobNodeClientState.Idle, client.State);
                Assert.Equal(0, client.SequentialErrorCount);

                await client.OpenAsync();

                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);
                Assert.True(client.TimeSinceLastOperationMs < 10000);

                await client.PutAsync(BobKey.FromUInt64(2), new byte[] { 1, 2, 3 });

                Assert.Equal(1, stat.PutRequestCount);
                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);
                Assert.True(client.TimeSinceLastOperationMs < 10000);

                var testDataArray = await client.GetAsync(BobKey.FromUInt64(2));

                Assert.Equal(1, stat.GetRequestCount);
                Assert.Equal(new byte[] { 1, 2, 3 }, testDataArray);
                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);
                Assert.True(client.TimeSinceLastOperationMs < 10000);

                var existsResult = await client.ExistsAsync(new BobKey[] { BobKey.FromUInt64(1), BobKey.FromUInt64(2), BobKey.FromUInt64(3) });

                Assert.Equal(1, stat.ExistsRequestCount);
                Assert.Equal(new bool[] { true, true, false }, existsResult);
                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);
                Assert.True(client.TimeSinceLastOperationMs < 10000);

                var existsResult2 = await client.ExistsAsync(new List <BobKey>() { BobKey.FromUInt64(1), BobKey.FromUInt64(2), BobKey.FromUInt64(3) });

                Assert.Equal(2, stat.ExistsRequestCount);
                Assert.Equal(new bool[] { true, true, false }, existsResult2);
                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);
                Assert.True(client.TimeSinceLastOperationMs < 10000);

                await client.CloseAsync();

                Assert.Equal(BobNodeClientState.Shutdown, client.State);
                Assert.Equal(0, client.SequentialErrorCount);
            }
        }
Ejemplo n.º 12
0
        public void PutGetExistOperationUInt64ConcurrentTest(int threadCount)
        {
            byte[] defaultData = new byte[] { 1, 2, 3 };
            var    data        = new ConcurrentDictionary <BobKey, byte[]>();

            data[BobKey.FromUInt64(1)] = defaultData;
            data[BobKey.FromUInt64(ulong.MaxValue)] = defaultData;

            using (var client = new BobNodeClient <ulong>(BobNodeClientMockHelper.CreateMockedClientWithData(data), null, null))
            {
                Barrier bar = new Barrier(threadCount + 1);

                void MainAction()
                {
                    bar.SignalAndWait(10000);

                    Assert.Equal(defaultData, client.Get(1));
                    Assert.Equal(defaultData, client.Get(ulong.MaxValue));
                    Assert.Throws <BobKeyNotFoundException>(() => client.Get(2));

                    Assert.Equal(new bool[] { true, false }, client.Exists(new ulong[] { 1, 2 }));

                    for (ulong i = 100; i < 10000; i++)
                    {
                        client.Put(i, defaultData);
                    }
                    for (ulong i = 100; i < 10000; i++)
                    {
                        Assert.Equal(defaultData, client.Get(i));
                    }
                    Assert.All(client.Exists(Enumerable.Range(100, 10000 - 100).Select(o => (ulong)o).ToArray()), res => Assert.True(res));
                    Assert.All(client.Exists(Enumerable.Range(20000, 1000).Select(o => (ulong)o).ToArray()), res => Assert.False(res));


                    for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                    {
                        client.Put(i, defaultData);
                    }
                    for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                    {
                        Assert.Equal(defaultData, client.Get(i));
                    }
                }

                Task[] tasks = new Task[threadCount];
                for (int i = 0; i < tasks.Length; i++)
                {
                    tasks[i] = Task.Factory.StartNew(MainAction, TaskCreationOptions.LongRunning);
                }

                bar.SignalAndWait(10000);

                Task.WaitAll(tasks);
            }
        }
Ejemplo n.º 13
0
        public void NegativeOperationsRetryCountConvertsToNumberOfNodes()
        {
            var data = new ConcurrentDictionary <BobKey, byte[]>();

            BobNodeClient[] clients = new BobNodeClient[]
            {
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: null, stat: null),
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: null, stat: null)
            };

            using (var client = new BobClusterClient(clients, SequentialNodeSelectionPolicy.Factory, operationRetryCount: -1))
            {
                Assert.Equal(clients.Length - 1, client.OperationRetryCount);
            }
        }
Ejemplo n.º 14
0
        public async Task RetriesWorksTestAsync()
        {
            byte[] defaultData = new byte[] { 1, 2, 3 };
            var    data        = new ConcurrentDictionary <BobKey, byte[]>();

            var behaviour1 = new BobNodeClientMockHelper.MockClientBehaviour();
            var stat1      = new BobNodeClientMockHelper.MockClientStat();
            var behaviour2 = new BobNodeClientMockHelper.MockClientBehaviour();
            var stat2      = new BobNodeClientMockHelper.MockClientStat();

            BobNodeClient[] clients = new BobNodeClient[]
            {
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: behaviour1, stat: stat1),
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: behaviour2, stat: stat2)
            };

            behaviour1.ErrorStatus = new Grpc.Core.Status(Grpc.Core.StatusCode.Internal, "Internal error");

            using (var client = new BobClusterClient(clients, SequentialNodeSelectionPolicy.Factory, operationRetryCount: 1))
            {
                await client.PutAsync(BobKey.FromUInt64(1), defaultData);

                Assert.Equal(defaultData, await client.GetAsync(BobKey.FromUInt64(1)));
                Assert.True((await client.ExistsAsync(new BobKey[] { BobKey.FromUInt64(1) }))[0]);

                for (int i = 10; i < 100; i++)
                {
                    await client.PutAsync(BobKey.FromUInt64((ulong)i), defaultData);
                }

                for (int i = 10; i < 100; i++)
                {
                    Assert.Equal(defaultData, await client.GetAsync(BobKey.FromUInt64((ulong)i)));
                }

                for (int i = 10; i < 100; i++)
                {
                    Assert.True((await client.ExistsAsync(new BobKey[] { BobKey.FromUInt64((ulong)i) }))[0]);
                }
            }
        }
Ejemplo n.º 15
0
        public void PutGetExistOperationUInt64Test()
        {
            byte[] defaultData = new byte[] { 1, 2, 3 };
            var    data        = new ConcurrentDictionary <BobKey, byte[]>();

            data[BobKey.FromUInt64(1)] = defaultData;
            data[BobKey.FromUInt64(ulong.MaxValue)] = defaultData;

            using (var client = new BobNodeClient <ulong>(BobNodeClientMockHelper.CreateMockedClientWithData(data), null, null))
            {
                Assert.Equal(defaultData, client.Get(1));
                Assert.Equal(defaultData, client.Get(ulong.MaxValue));
                Assert.Throws <BobKeyNotFoundException>(() => client.Get(2));

                Assert.Equal(new bool[] { true, false }, client.Exists(new ulong[] { 1, 2 }));

                for (ulong i = 100; i < 10000; i++)
                {
                    client.Put(i, defaultData);
                }
                for (ulong i = 100; i < 10000; i++)
                {
                    Assert.Equal(defaultData, client.Get(i));
                }
                Assert.All(client.Exists(Enumerable.Range(100, 10000 - 100).Select(o => (ulong)o).ToArray()), res => Assert.True(res));
                Assert.All(client.Exists(Enumerable.Range(20000, 1000).Select(o => (ulong)o).ToArray()), res => Assert.False(res));

                Assert.All(client.Exists(Enumerable.Range(100, 10000 - 100).Select(o => (ulong)o).ToList()), res => Assert.True(res));
                Assert.All(client.Exists(Enumerable.Range(20000, 1000).Select(o => (ulong)o).ToList()), res => Assert.False(res));

                for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                {
                    client.Put(i, defaultData);
                }
                for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                {
                    Assert.Equal(defaultData, client.Get(i));
                }
            }
        }
Ejemplo n.º 16
0
        public async Task PutGetExistOperationTestAsync()
        {
            byte[] defaultData = new byte[] { 1, 2, 3 };
            var    data        = new ConcurrentDictionary <BobKey, byte[]>();

            data[BobKey.FromUInt64(1)] = defaultData;
            data[BobKey.FromUInt64(ulong.MaxValue)] = defaultData;

            using (var client = BobNodeClientMockHelper.CreateMockedClientWithData(data))
            {
                Assert.Equal(defaultData, await client.GetAsync(BobKey.FromUInt64(1)));
                Assert.Equal(defaultData, await client.GetAsync(BobKey.FromUInt64(ulong.MaxValue)));
                Assert.Throws <BobKeyNotFoundException>(() => client.GetAsync(BobKey.FromUInt64(2)).GetAwaiter().GetResult());

                Assert.Equal(new bool[] { true, false }, await client.ExistsAsync(new BobKey[] { BobKey.FromUInt64(1), BobKey.FromUInt64(2) }));

                for (ulong i = 100; i < 10000; i++)
                {
                    await client.PutAsync(BobKey.FromUInt64(i), defaultData);
                }
                for (ulong i = 100; i < 10000; i++)
                {
                    Assert.Equal(defaultData, await client.GetAsync(BobKey.FromUInt64(i)));
                }
                Assert.All(await client.ExistsAsync(Enumerable.Range(100, 10000 - 100).Select(o => BobKey.FromUInt64((ulong)o)).ToArray()), res => Assert.True(res));
                Assert.All(await client.ExistsAsync(Enumerable.Range(20000, 1000).Select(o => BobKey.FromUInt64((ulong)o)).ToArray()), res => Assert.False(res));


                for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                {
                    await client.PutAsync(BobKey.FromUInt64(i), defaultData);
                }
                for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                {
                    Assert.Equal(defaultData, await client.GetAsync(BobKey.FromUInt64(i)));
                }
            }
        }
Ejemplo n.º 17
0
        public void SequentialErrorCountTest()
        {
            var data = new ConcurrentDictionary <BobKey, byte[]>();

            data[BobKey.FromUInt64(1)] = new byte[] { 1, 2, 3 };

            var behaviour = new BobNodeClientMockHelper.MockClientBehaviour();
            var stat      = new BobNodeClientMockHelper.MockClientStat();

            using (var client = BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour, stat))
            {
                client.Open();
                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);

                behaviour.ErrorStatus = new Grpc.Core.Status(Grpc.Core.StatusCode.Internal, "Internal error");

                for (int i = 1; i <= 100; i++)
                {
                    try
                    {
                        client.Get(BobKey.FromUInt64(1));
                    }
                    catch (BobOperationException)
                    {
                    }

                    Assert.Equal(BobNodeClientState.TransientFailure, client.State);
                    Assert.Equal(i, client.SequentialErrorCount);
                }

                behaviour.ErrorStatus = Grpc.Core.Status.DefaultSuccess;

                client.Get(BobKey.FromUInt64(1));
                Assert.Equal(BobNodeClientState.Ready, client.State);
                Assert.Equal(0, client.SequentialErrorCount);
            }
        }
Ejemplo n.º 18
0
        public void NoRetryForKeyNotFoundTest()
        {
            byte[] defaultData = new byte[] { 1, 2, 3 };
            var    data        = new ConcurrentDictionary <BobKey, byte[]>();

            var behaviour1 = new BobNodeClientMockHelper.MockClientBehaviour();
            var stat1      = new BobNodeClientMockHelper.MockClientStat();
            var behaviour2 = new BobNodeClientMockHelper.MockClientBehaviour();
            var stat2      = new BobNodeClientMockHelper.MockClientStat();

            BobNodeClient[] clients = new BobNodeClient[]
            {
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: behaviour1, stat: stat1),
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: behaviour2, stat: stat2)
            };

            using (var client = new BobClusterClient(clients, SequentialNodeSelectionPolicy.Factory, operationRetryCount: 10))
            {
                Assert.Throws <BobKeyNotFoundException>(() => client.Get(BobKey.FromUInt64(1)));
                Assert.Equal(1, stat1.GetRequestCount + stat2.GetRequestCount);

                Assert.Throws <BobKeyNotFoundException>(() => client.Get(BobKey.FromUInt64(2)));
                Assert.Equal(2, stat1.GetRequestCount + stat2.GetRequestCount);

                Assert.Throws <BobKeyNotFoundException>(() => client.Get(BobKey.FromUInt64(3)));
                Assert.Equal(3, stat1.GetRequestCount + stat2.GetRequestCount);


                Assert.Throws <BobKeyNotFoundException>(() => client.GetAsync(BobKey.FromUInt64(1)).GetAwaiter().GetResult());
                Assert.Equal(4, stat1.GetRequestCount + stat2.GetRequestCount);

                Assert.Throws <BobKeyNotFoundException>(() => client.GetAsync(BobKey.FromUInt64(2)).GetAwaiter().GetResult());
                Assert.Equal(5, stat1.GetRequestCount + stat2.GetRequestCount);

                Assert.Throws <BobKeyNotFoundException>(() => client.GetAsync(BobKey.FromUInt64(3)).GetAwaiter().GetResult());
                Assert.Equal(6, stat1.GetRequestCount + stat2.GetRequestCount);
            }
        }
Ejemplo n.º 19
0
        public async Task PutGetExistOperationInt32TestAsync()
        {
            byte[] defaultData = new byte[] { 1, 2, 3 };
            var    data        = new ConcurrentDictionary <BobKey, byte[]>();

            using (var client = new BobNodeClient <int>(BobNodeClientMockHelper.CreateMockedClientWithData(data), null, null))
            {
                client.Put(1, defaultData);
                client.Put(int.MaxValue, defaultData);

                Assert.Equal(defaultData, await client.GetAsync(1));
                Assert.Equal(defaultData, await client.GetAsync(int.MaxValue));
                Assert.Throws <BobKeyNotFoundException>(() => client.GetAsync(2).GetAwaiter().GetResult());

                Assert.Equal(new bool[] { true, false }, await client.ExistsAsync(new int[] { 1, 2 }));

                for (int i = 100; i < 10000; i++)
                {
                    await client.PutAsync(i, defaultData);
                }
                for (int i = 100; i < 10000; i++)
                {
                    Assert.Equal(defaultData, await client.GetAsync(i));
                }
                Assert.All(await client.ExistsAsync(Enumerable.Range(100, 10000 - 100).Select(o => o).ToArray()), res => Assert.True(res));
                Assert.All(await client.ExistsAsync(Enumerable.Range(20000, 1000).Select(o => o).ToArray()), res => Assert.False(res));


                for (int i = int.MaxValue - 1000; i < int.MaxValue - 1; i++)
                {
                    await client.PutAsync(i, defaultData);
                }
                for (int i = int.MaxValue - 1000; i < int.MaxValue - 1; i++)
                {
                    Assert.Equal(defaultData, await client.GetAsync(i));
                }
            }
        }
Ejemplo n.º 20
0
        public async Task PutGetExistOperationTestAsync()
        {
            byte[] defaultData = new byte[] { 1, 2, 3 };
            var    data        = new ConcurrentDictionary <BobKey, byte[]>();

            var stat1 = new BobNodeClientMockHelper.MockClientStat();
            var stat2 = new BobNodeClientMockHelper.MockClientStat();

            BobNodeClient[] clients = new BobNodeClient[]
            {
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: null, stat: stat1),
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: null, stat: stat2)
            };

            using (var client = new BobClusterClient(clients, SequentialNodeSelectionPolicy.Factory, 0))
            {
                await client.PutAsync(BobKey.FromUInt64(1), defaultData);

                await client.PutAsync(BobKey.FromUInt64(ulong.MaxValue), defaultData);

                Assert.Equal(2, stat1.PutRequestCount + stat2.PutRequestCount);

                Assert.Equal(defaultData, await client.GetAsync(BobKey.FromUInt64(1)));
                Assert.Equal(defaultData, await client.GetAsync(BobKey.FromUInt64(ulong.MaxValue)));
                await Assert.ThrowsAsync <BobKeyNotFoundException>(() => client.GetAsync(BobKey.FromUInt64(2)));

                Assert.Equal(3, stat1.GetRequestCount + stat2.GetRequestCount);

                Assert.Equal(new bool[] { true, false }, await client.ExistsAsync(new BobKey[] { BobKey.FromUInt64(1), BobKey.FromUInt64(2) }));
                Assert.Equal(1, stat1.ExistsRequestCount + stat2.ExistsRequestCount);

                for (ulong i = 100; i < 10000; i++)
                {
                    await client.PutAsync(BobKey.FromUInt64(i), defaultData);
                }
                for (ulong i = 100; i < 10000; i++)
                {
                    Assert.Equal(defaultData, await client.GetAsync(BobKey.FromUInt64(i)));
                }
                for (ulong i = 100; i < 10000; i++)
                {
                    Assert.Equal(defaultData, await client.GetAsync(BobKey.FromUInt64(i), fullGet: true, new CancellationToken()));
                }
                Assert.Equal(10000 - 100, stat1.RequestsWithFullGet + stat2.RequestsWithFullGet);

                Assert.All(await client.ExistsAsync(Enumerable.Range(100, 10000 - 100).Select(o => BobKey.FromUInt64((ulong)o)).ToArray()), res => Assert.True(res));
                Assert.All(await client.ExistsAsync(Enumerable.Range(20000, 1000).Select(o => BobKey.FromUInt64((ulong)o)).ToArray(), fullGet: true, new CancellationToken()), res => Assert.False(res));
                Assert.Equal(10000 - 100 + 1, stat1.RequestsWithFullGet + stat2.RequestsWithFullGet);

                for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                {
                    await client.PutAsync(BobKey.FromUInt64(i), defaultData);
                }
                for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                {
                    Assert.Equal(defaultData, await client.GetAsync(BobKey.FromUInt64(i)));
                }

                Assert.True(stat1.RequestsWithFullGet + stat2.RequestsWithFullGet > 0);
                Assert.True(stat1.TotalRequestCount > 0);
                Assert.True(stat2.TotalRequestCount > 0);
                Assert.True(Math.Abs(stat1.TotalRequestCount - stat2.TotalRequestCount) <= 1);
            }
        }
Ejemplo n.º 21
0
        public void PutGetExistOperationTest()
        {
            byte[] defaultData = new byte[] { 1, 2, 3 };
            var    data        = new ConcurrentDictionary <BobKey, byte[]>();

            var stat1 = new BobNodeClientMockHelper.MockClientStat();
            var stat2 = new BobNodeClientMockHelper.MockClientStat();

            BobNodeClient[] clients = new BobNodeClient[]
            {
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: null, stat: stat1),
                BobNodeClientMockHelper.CreateMockedClientWithData(data, behaviour: null, stat: stat2)
            };

            using (var client = new BobClusterClient <ulong>(clients, SequentialNodeSelectionPolicy.Factory, operationRetryCount: 0, keySerializer: null, keySerializationPoolSize: null))
            {
                client.Put(1, defaultData);
                client.Put(ulong.MaxValue, defaultData);
                Assert.Equal(2, stat1.PutRequestCount + stat2.PutRequestCount);

                Assert.Equal(defaultData, client.Get(1));
                Assert.Equal(defaultData, client.Get(ulong.MaxValue));
                Assert.Throws <BobKeyNotFoundException>(() => client.Get(2));
                Assert.Equal(3, stat1.GetRequestCount + stat2.GetRequestCount);

                Assert.Equal(new bool[] { true, false }, client.Exists(new ulong[] { 1, 2 }));
                Assert.Equal(1, stat1.ExistsRequestCount + stat2.ExistsRequestCount);

                for (ulong i = 100; i < 10000; i++)
                {
                    client.Put(i, defaultData);
                }
                for (ulong i = 100; i < 10000; i++)
                {
                    Assert.Equal(defaultData, client.Get(i));
                }
                for (ulong i = 100; i < 10000; i++)
                {
                    Assert.Equal(defaultData, client.Get(i, fullGet: true, new CancellationToken()));
                }
                Assert.Equal(10000 - 100, stat1.RequestsWithFullGet + stat2.RequestsWithFullGet);

                Assert.All(client.Exists(Enumerable.Range(100, 10000 - 100).Select(o => (ulong)o).ToArray()), res => Assert.True(res));
                Assert.All(client.Exists(Enumerable.Range(20000, 1000).Select(o => (ulong)o).ToArray(), fullGet: true, new CancellationToken()), res => Assert.False(res));

                Assert.All(client.Exists(Enumerable.Range(100, 10000 - 100).Select(o => (ulong)o).ToList()), res => Assert.True(res));
                Assert.All(client.Exists(Enumerable.Range(20000, 1000).Select(o => (ulong)o).ToList(), fullGet: true, new CancellationToken()), res => Assert.False(res));

                Assert.Equal(10000 - 100 + 2, stat1.RequestsWithFullGet + stat2.RequestsWithFullGet);

                for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                {
                    client.Put(i, defaultData);
                }
                for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++)
                {
                    Assert.Equal(defaultData, client.Get(i));
                }

                Assert.True(stat1.RequestsWithFullGet + stat2.RequestsWithFullGet > 0);
                Assert.True(stat1.TotalRequestCount > 0);
                Assert.True(stat2.TotalRequestCount > 0);
                Assert.True(Math.Abs(stat1.TotalRequestCount - stat2.TotalRequestCount) <= 1);
            }
        }