Transient Fault Handling Application 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 内で文字列へのキャストに失敗していたので、(既に「実行中」の) 例外処理ブロック内で例外をスローしていたと思います。Petar からのヒントを使用して、この小さな問題を確認 (および修正) し、再試行が期待どおりに機能するようになりました。