无法测试Azure的瞬态故障处理应用程序块
我在执行瞬态故障处理应用程序块的简单测试时遇到问题,所以我肯定错过了一些愚蠢的东西 根据和中的信息 我在下面创建了模拟类:无法测试Azure的瞬态故障处理应用程序块,azure,Azure,我在执行瞬态故障处理应用程序块的简单测试时遇到问题,所以我肯定错过了一些愚蠢的东西 根据和中的信息 我在下面创建了模拟类: 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-
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()
我运行了VisualStudio调试器,希望看到一些重试,但我从VisualStudio中得到一个弹出窗口,显示“用户代码未处理异常”。当我将ThrowException()抛出到方法内部时,就会发生这种情况。当然,似乎没有发生重试
我错过了什么
编辑:我未能在OnMockConnectionRetry中强制转换为字符串,所以我假设我在(已经“运行”)异常处理块中引发了一个异常。通过使用Petar的提示,我能够看到(并修复)这个小问题,现在重试工作正常。您需要通过取消选中“用户未处理”选项来关闭公共语言运行时异常,以免被VS中断。该选项位于“调试/异常”菜单下
测试SQL Azure的瞬时故障处理非常简单。请参阅链接以了解各种选项,通过这些选项,您可以确认EF 6瞬态故障处理的重试逻辑正在工作。请参阅帮助的链接-!VisualStudio在OnMockConnectionRetry中隐藏了我的代码的一个问题(我忘记将一个参数转换为字符串)。我会更正我的帖子,以防其他人想用它进行测试。