3

我在执行瞬态故障处理应用程序块的简单测试时遇到了麻烦,所以我一定错过了一些愚蠢的东西。

基于http://msdn.microsoft.com/en-us/library/hh680927(v=pandp.50).aspxhttp://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 中强制转换为字符串,所以我假设我在(已经“运行”的)异常处理块中抛出了一个异常。通过使用 Petar 的提示,我能够看到(并修复)这个小问题,现在重试按预期工作。

4

1 回答 1

3

您需要通过取消选中用户未处理选项来关闭公共语言运行时异常,以免被 VS 中断。这位于调试/异常菜单下。

在此处输入图像描述

于 2012-10-31T16:46:27.157 回答