我在执行瞬态故障处理应用程序块的简单测试时遇到问题,所以我必须遗漏一些愚蠢的东西。
基于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中的信息 {{3}}我在下面创建了模拟类:
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的弹出窗口,说“Exception未被用户代码处理”。一旦我抛出ThrowException()方法,就会发生这种情况。当然,似乎没有重演。
我错过了什么?
编辑:我无法在OnMockConnectionRetry中强制转换为字符串,因此我假设我在(已经“正在运行”)异常处理块中抛出异常。通过使用Petar的提示,我能够看到(并修复)这个小问题,现在重试正在按预期工作。答案 0 :(得分:3)
您需要通过取消选中User-unhandled选项来关闭公共语言运行时异常,以免被VS中断。它位于Debug / Exceptions菜单下。
答案 1 :(得分:1)
测试SQL Azure的瞬态故障处理非常简单。请参阅链接了解各种选项,通过这些选项可以确认您的EF 6瞬态故障处理重试逻辑是否正常。请参阅链接 - http://sanganakauthority.blogspot.in/2014/07/transient-fault-handling-with-sql-azure.html