我在执行 transient 故障处理应用程序 block 的简单测试时遇到了困难,所以我一定错过了一些愚蠢的东西。
基于http://msdn.microsoft.com/en-us/library/hh680927(v=pandp.50).aspx中的信息和 http://azuretable.blogspot.com/2012/08/unit-testing-transient-errors-in-azure.html我创建了下面的模拟类:
Public Class DBUtils_TestRetryPolicy
' see http://msdn.microsoft.com/en-us/library/hh680927(v=pandp.50).aspx
' see http://azuretable.blogspot.com/2012/08/unit-testing-transient-errors-in-azure.html
Public Class TransientMock
Implements ITransientErrorDetectionStrategy
Public Function IsTransient(ex As System.Exception) As Boolean Implements Microsoft.Practices.TransientFaultHandling.ITransientErrorDetectionStrategy.IsTransient
Return (True)
End Function
End Class
Private Shared mockExceptionCounter As Integer = 0
Private Shared retryLog As String = ""
Private Sub ThrowException()
mockExceptionCounter += 1
Throw New Exception("This is as mock exception to test retries")
End Sub
Public Function TestRetryLogic() As String
Dim theRetryStrategy = New Incremental(6, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)) ' should result in retries at 0, 1, 3, 5, 7, 9 seconds (25 seconds total)
theRetryStrategy.FastFirstRetry = True
Dim theRetryPolicy = New RetryPolicy(Of TransientMock)(theRetryStrategy)
AddHandler theRetryPolicy.Retrying, AddressOf OnMockConnectionRetry
mockExceptionCounter = 0
retryLog = ""
Try
theRetryPolicy.ExecuteAction(New System.Action(AddressOf ThrowException))
Catch ex As Exception
' here we should have the last exception thrown after all the retries
Dim a As Integer = 231234234
End Try
Return (retryLog)
End Function
Private Sub OnMockConnectionRetry(sender As Object, e As RetryingEventArgs)
retryLog += DateTime.UtcNow.ToString + " [" + mockExceptionCounter.ToString() + "] -> CurrentRetryCount [" + Cstr(e.CurrentRetryCount) + "]" + "Delay (ms) [" + CStr(e.Delay.TotalMilliseconds) + "]" + "LastException [" + e.LastException.Message + "]"
End Sub
End Class
我在代码中所做的就是实例化此类并调用 TestRetryLogic()。
我运行了 Visual Studio 调试器,希望看到几次重试,但我得到的是 Visual Studio 的弹出窗口,显示“异常未由用户代码处理”。当我抛出 ThrowException() 方法时,就会发生这种情况。当然,似乎没有重试发生。
我错过了什么?
编辑:我无法在 OnMockConnectionRetry 内转换为字符串,所以我假设我在(已经“正在运行”)异常处理 block 内抛出异常。通过使用 Petar 的提示,我能够看到(并修复)这个小问题,现在重试可以按预期进行。
最佳答案
您需要通过取消选中“用户未处理”选项来关闭公共(public)语言运行时异常,以免被 VS 中断。它位于“调试/异常”菜单下。
关于azure - 无法测试 Azure 的 transient 故障处理应用程序 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13161710/