-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAttributeEventInvokerTests.fs
More file actions
84 lines (69 loc) · 3.51 KB
/
AttributeEventInvokerTests.fs
File metadata and controls
84 lines (69 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace ModularPipelines.UnitTests.FSharp.Attributes
open Microsoft.Extensions.Logging
open ModularPipelines.Attributes.Events
open ModularPipelines.Context
open ModularPipelines.Engine.Attributes
open Moq
open System
open TUnit.Assertions
open TUnit.Assertions.Extensions
open TUnit.Assertions.FSharp.Operations
open TUnit.Core
module AttributeEventInvokerTests =
type private SuccessfulHandler() =
member val WasCalled = false with get, private set
interface IModuleStartHandler with
member _.ContinueOnError = false
member this.OnModuleStartAsync(_: ModularPipelines.Context.IModuleHookContext): System.Threading.Tasks.Task =
task {
this.WasCalled <- true
}
type private FailingHandler() =
interface IModuleStartHandler with
member _.ContinueOnError = false
member _.OnModuleStartAsync(_: ModularPipelines.Context.IModuleHookContext): System.Threading.Tasks.Task =
raise (InvalidOperationException("Test exception"))
type private FailingHandlerWithContinue() =
interface IModuleStartHandler with
member _.ContinueOnError = true
member _.OnModuleStartAsync(_: ModularPipelines.Context.IModuleHookContext): System.Threading.Tasks.Task =
raise (InvalidOperationException("Test exception"))
type AttributeEventInvokerTests() =
[<Test>]
member _.InvokeAsync_CallsAllHandlers() = async {
let handler1 = SuccessfulHandler()
let handler2 = SuccessfulHandler()
let handlers = [ handler1 :> IModuleStartHandler; handler2 :> IModuleStartHandler ]
let invoker = AttributeEventInvoker(Mock.Of<ILogger<AttributeEventInvoker>>())
let context = Mock.Of<IModuleHookContext>()
do! invoker.InvokeStartHandlersAsync(handlers, context) |> Async.AwaitTask
do! check(Assert.That(handler1.WasCalled).IsTrue())
do! check(Assert.That(handler2.WasCalled).IsTrue())
}
[<Test>]
member _.InvokeAsync_HandlerThrows_ContinueOnErrorFalse_Propagates() = async {
let handler = FailingHandler()
let handlers = [ handler :> IModuleStartHandler ]
let invoker = AttributeEventInvoker(Mock.Of<ILogger<AttributeEventInvoker>>())
let context = Mock.Of<IModuleHookContext>()
let mutable thrownException = None
try
do! invoker.InvokeStartHandlersAsync(handlers, context) |> Async.AwaitTask
with ex ->
thrownException <- Some ex
do! check(Assert.That(thrownException.IsSome).IsTrue())
match thrownException with
| Some ex ->
do! check(StringEqualsAssertionExtensions.IsEqualTo(Assert.That(ex.GetBaseException().Message), "Test exception"))
| None -> ()
}
[<Test>]
member _.InvokeAsync_HandlerThrows_ContinueOnErrorTrue_Continues() = async {
let failingHandler = FailingHandlerWithContinue()
let successHandler = SuccessfulHandler()
let handlers = [ failingHandler :> IModuleStartHandler; successHandler :> IModuleStartHandler ]
let invoker = AttributeEventInvoker(Mock.Of<ILogger<AttributeEventInvoker>>())
let context = Mock.Of<IModuleHookContext>()
do! invoker.InvokeStartHandlersAsync(handlers, context) |> Async.AwaitTask
do! check(Assert.That(successHandler.WasCalled).IsTrue())
}