/// <summary> /// Examine the ResultCode in the given <paramref name="response"/> and if the ResultCode is not Ok, throw an exception that corresponds to the code. /// </summary> /// <param name="response">Response to check for error</param> private static void ThrowIfError(RequestResponse response) { if (response.ResultCode != (int)RingMasterException.Code.Ok) { Exception exception = RingMasterException.GetException(response); throw exception; } }
/// <summary> /// Examine the ResultCode in the given <paramref name="response"/> and if the ResultCode is not Ok, throw an exception that corresponds to the response. /// </summary> /// <param name="response">Response to check for error</param> private static void ThrowIfError(RequestResponse response) { if (response.ResultCode != (int)RingMasterException.Code.Ok) { Exception exception = RingMasterException.GetException(response); RingMasterClientEventSource.Log.CompleteWithException(exception.Message); throw exception; } }
/// <summary> /// Apply a set of delete operations as a multi. /// </summary> /// <param name="ringMaster">Interface to RingMaster</param> /// <param name="deleteOperations">Delete operations to apply as a batch</param> /// <returns>Async task to indicate the completion of the request</returns> private async Task DeleteMulti(IRingMasterRequestHandler ringMaster, IReadOnlyList <Op> deleteOperations) { var timer = Stopwatch.StartNew(); var multiRequest = new RequestMulti(deleteOperations, completeSynchronously: true); var response = await ringMaster.Request(multiRequest); if (response.ResultCode == (int)RingMasterException.Code.Ok) { this.deletedCount += deleteOperations.Count; this.instrumentation?.DeleteMultiSucceeded(this.deletedCount, deleteOperations.Count, timer.Elapsed); } else { this.instrumentation?.DeleteMultiFailed(this.deletedCount, deleteOperations.Count, timer.Elapsed); throw RingMasterException.GetException(response); } }
public void RetriableRingMasterClientStress() { int maxRequestCount = 1000; int initiallyWorkingCount = 500; var mockRequestHandler = Substitute.For <IRingMasterRequestHandler>(); var rnd = new Random(); int requestCount = 0; mockRequestHandler.Request(Arg.Any <IRingMasterRequest>()).Returns <Task <RequestResponse> >((callInfo2) => { Interlocked.Increment(ref requestCount); if (requestCount <= initiallyWorkingCount) { if (rnd.NextDouble() < 0.1) { return(Task.FromResult(new RequestResponse() { ResultCode = (int)RingMasterException.Code.Operationtimeout })); } else { return(Task.FromResult(new RequestResponse() { ResultCode = (int)RingMasterException.Code.Ok })); } } else if (rnd.NextDouble() < 0.5) { return(Task.FromResult(new RequestResponse() { ResultCode = (int)RingMasterException.Code.Operationtimeout })); } else { throw RingMasterException.GetException(new RequestResponse() { ResultCode = (int)RingMasterException.Code.OperationCancelled }); } }); var createClientFunc = Substitute.For <Func <string, IRingMasterRequestHandler> >(); createClientFunc(Arg.Any <string>()).Returns((callInfo) => { requestCount = 0; return(mockRequestHandler); }); var vegaServiceInfoReader = Substitute.For <IVegaServiceInfoReader>(); vegaServiceInfoReader.GetVegaServiceInfo().Returns((callInfo) => { Thread.Sleep(rnd.Next(1, 5) * 1000); return(Tuple.Create(Arg.Any <string>(), Arg.Any <string>())); }); var requestFunc = Substitute.For <Func <IRingMasterRequestHandler, Task <RequestResponse> > >(); requestFunc(Arg.Any <IRingMasterRequestHandler>()).Returns((callInfo) => mockRequestHandler.Request(Arg.Any <IRingMasterRequest>())); RetriableRingMasterClient theClient = new RetriableRingMasterClient(createClientFunc, Arg.Any <string>(), vegaServiceInfoReader, log); int taskCount = 0; int exceptionCount = 0; for (int i = 0; i < maxRequestCount; i++) { taskCount++; var unused = theClient.Request(requestFunc) .ContinueWith(t => { if (t.Exception != null) { Interlocked.Increment(ref exceptionCount); } Interlocked.Decrement(ref taskCount); }); } SpinWait.SpinUntil(() => taskCount == 0); Assert.IsTrue(exceptionCount < maxRequestCount - initiallyWorkingCount); log($"{exceptionCount}"); }