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))); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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)); } } }
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]); } } }
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); } }
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); } }