public async void Connect() { if (!IsConnected) { IsConnected = true; try { await WithRetry.InvokeAsync(_connectionRetryPolicy, () => { if (!IsNetworkAvailable) { Debug.WriteLine("NetworkUnavailableException"); throw new NetworkUnavailableException(); } _client = new UdpClient(); _client.Connect(_endPoint); }); Connected?.Invoke(this, EventArgs.Empty); } catch (Exception ex) { IsConnected = false; Debug.WriteLine(ex); throw; } } }
public async Task WithRetry_InvokeAsync() { var ac = new WithRetryTestClass(); WithRetry.SetDefaultOptions(new RetryOptions(3, TimeSpan.FromSeconds(15))); var fr2 = await WithRetry.InvokeAsync(async () => await ac.FunctionAsync(1)); await Assert.ThrowsExceptionAsync<ExceededMaxAttemptsException>(async () => { var fr = await WithRetry.InvokeAsync(() => ac.ExceptionAsync(1)); }); WithRetry.SetDefaultOptions(new RetryOptions(100000, TimeSpan.FromMilliseconds(10))); await Assert.ThrowsExceptionAsync<ExceededMaxWaitTimeException>(async () => { var fr = await WithRetry.InvokeAsync(() => ac.ExceptionAsync(1)); }); }
public async Task WithRetry_Invoke() { var ac = new WithRetryTestClass(); WithRetry.SetDefaultPolicy(new RetryPolicy(3, TimeSpan.FromMinutes(15))); await WithRetry.InvokeAsync(() => ac.Action(1)); var fr1 = await WithRetry.InvokeAsync(() => ac.Function(1)); await Assert.ThrowsExceptionAsync <ExceededMaxAttemptsException>(async() => { var fr = await WithRetry.InvokeAsync(() => ac.Exception(1)); }); WithRetry.SetDefaultPolicy(new RetryPolicy(int.MaxValue, TimeSpan.FromMilliseconds(1))); await Assert.ThrowsExceptionAsync <ExceededMaxWaitTimeException>(async() => { var fr = await WithRetry.InvokeAsync(() => ac.Exception(1)); }); }