-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft of IMessageSystem, wrapRun utility for IMessageSystem imp…
…lementations (#29)
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Emulsion.Tests.MessageSystem | ||
|
||
open Xunit | ||
|
||
open System | ||
open System.Threading | ||
open Emulsion | ||
|
||
let private performTest expectedStage runBody = | ||
use cts = new CancellationTokenSource() | ||
let mutable stage = 0 | ||
let run ct = | ||
stage <- stage + 1 | ||
runBody cts ct stage | ||
MessageSystem.wrapRun cts.Token run ignore | ||
Assert.Equal(expectedStage, stage) | ||
|
||
[<Fact>] | ||
let ``wrapRun should restart the activity on error``() = | ||
performTest 2 (fun cts _ stage -> | ||
match stage with | ||
| 1 -> raise <| Exception() | ||
| 2 -> cts.Cancel() | ||
) | ||
|
||
[<Fact>] | ||
let ``wrapRun should not restart on OperationCanceledException``() = | ||
performTest 1 (fun cts ct _ -> | ||
cts.Cancel() | ||
ct.ThrowIfCancellationRequested() | ||
) | ||
|
||
[<Fact>] | ||
let ``wrapRun should not restart on token.Cancel()``() = | ||
performTest 4 (fun cts _ stage -> | ||
if stage > 3 then | ||
cts.Cancel() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Emulsion.MessageSystem | ||
|
||
open System | ||
open System.Threading | ||
|
||
type IncomingMessageReceiver = IncomingMessage -> unit | ||
|
||
/// The IM message queue. Manages the underlying connection, reconnects when necessary, stores the outgoing messages in | ||
/// a queue and sends them when possible. Redirects the incoming messages to a function passed when starting the queue. | ||
type IMessageSystem = | ||
/// Starts the IM connection, manages reconnects. On cancellation could either throw OperationCanceledException or | ||
/// return a unit. | ||
abstract member Run : IncomingMessageReceiver -> CancellationToken -> unit | ||
|
||
/// Queues the message to be sent to the IM system when possible. | ||
abstract member PutMessage : OutgoingMessage -> unit | ||
|
||
let internal wrapRun (token: CancellationToken) (run: CancellationToken -> unit) (log: Exception -> unit) : unit = | ||
while not token.IsCancellationRequested do | ||
try | ||
run token | ||
with | ||
| :? OperationCanceledException -> () | ||
| ex -> log ex |