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 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 RetriesWorksTest() { 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)) { client.Put(BobKey.FromUInt64(1), defaultData); Assert.Equal(defaultData, client.Get(BobKey.FromUInt64(1))); Assert.True(client.Exists(new BobKey[] { BobKey.FromUInt64(1) })[0]); for (int i = 10; i < 100; i++) { client.Put(BobKey.FromUInt64((ulong)i), defaultData); } for (int i = 10; i < 100; i++) { Assert.Equal(defaultData, client.Get(BobKey.FromUInt64((ulong)i))); } for (int i = 10; i < 100; i++) { Assert.True(client.Exists(new BobKey[] { BobKey.FromUInt64((ulong)i) })[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(clients, SequentialNodeSelectionPolicy.Factory, 0)) { client.Put(BobKey.FromUInt64(1), defaultData); client.Put(BobKey.FromUInt64(ulong.MaxValue), defaultData); Assert.Equal(2, stat1.PutRequestCount + stat2.PutRequestCount); Assert.Equal(defaultData, client.Get(BobKey.FromUInt64(1))); Assert.Equal(defaultData, client.Get(BobKey.FromUInt64(ulong.MaxValue))); Assert.Throws <BobKeyNotFoundException>(() => client.Get(BobKey.FromUInt64(2))); Assert.Equal(3, stat1.GetRequestCount + stat2.GetRequestCount); Assert.Equal(new bool[] { true, false }, client.Exists(new BobKey[] { BobKey.FromUInt64(1), BobKey.FromUInt64(2) })); Assert.Equal(1, stat1.ExistsRequestCount + stat2.ExistsRequestCount); for (ulong i = 100; i < 10000; i++) { client.Put(BobKey.FromUInt64(i), defaultData); } for (ulong i = 100; i < 10000; i++) { Assert.Equal(defaultData, client.Get(BobKey.FromUInt64(i))); } for (ulong i = 100; i < 10000; i++) { Assert.Equal(defaultData, client.Get(BobKey.FromUInt64(i), fullGet: true, new System.Threading.CancellationToken())); } Assert.Equal(10000 - 100, stat1.RequestsWithFullGet + stat2.RequestsWithFullGet); Assert.All(client.Exists(Enumerable.Range(100, 10000 - 100).Select(o => BobKey.FromUInt64((ulong)o)).ToArray()), res => Assert.True(res)); Assert.All(client.Exists(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++) { client.Put(BobKey.FromUInt64(i), defaultData); } for (ulong i = uint.MaxValue; i < (ulong)uint.MaxValue + 1000; i++) { Assert.Equal(defaultData, client.Get(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); } }
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; 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)) { 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)); 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)); } } 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); } }