-
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.
- Loading branch information
Showing
4 changed files
with
62 additions
and
3 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,59 @@ | ||
module Emulsion.Tests.MessageSenderTests | ||
|
||
open System | ||
open System.Threading | ||
|
||
open Xunit | ||
open Emulsion | ||
open Emulsion.MessageSender | ||
|
||
let private testContext = { | ||
send = fun _ -> async { return () } | ||
logError = ignore | ||
cooldown = TimeSpan.Zero | ||
} | ||
|
||
[<Fact>] | ||
let ``Message sender sends the messages sequentially``() = | ||
use cts = new CancellationTokenSource() | ||
let messagesReceived = ResizeArray() | ||
let context = { | ||
testContext with | ||
send = fun m -> async { | ||
lock messagesReceived (fun () -> | ||
messagesReceived.Add m | ||
) | ||
} | ||
} | ||
let sender = MessageSender.startActivity(context, cts.Token) | ||
|
||
let messagesSent = [| 1..100 |] |> Array.map (fun i -> | ||
OutgoingMessage { | ||
author = "author" | ||
text = string i | ||
} | ||
) | ||
messagesSent |> Array.iter(MessageSender.send sender) | ||
|
||
SpinWait.SpinUntil((fun () -> messagesReceived.Count = messagesSent.Length), TimeSpan.FromSeconds 30.0) | ||
|> Assert.True | ||
|
||
Assert.Equal(messagesSent, messagesReceived) | ||
|
||
[<Fact>] | ||
let ``Message sender should be cancellable``() = | ||
use cts = new CancellationTokenSource() | ||
let errors = ResizeArray() | ||
let context = { | ||
testContext with | ||
send = fun _ -> failwith "Should not be called" | ||
logError = fun e -> lock errors (fun () -> errors.Add e) | ||
} | ||
let sender = MessageSender.startActivity(context, cts.Token) | ||
cts.Cancel() | ||
|
||
let msg = OutgoingMessage { author = "author"; text = "xx" } | ||
MessageSender.send sender msg | ||
|
||
SpinWait.SpinUntil((fun () -> errors.Count > 0), TimeSpan.FromMilliseconds 100.0) |> ignore | ||
Assert.Empty errors |
2 changes: 1 addition & 1 deletion
2
Emulsion.Tests/MessageSystem.fs → Emulsion.Tests/MessageSystemTests.fs
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Emulsion.Tests.MessageSystem | ||
module Emulsion.Tests.MessageSystemTests | ||
|
||
open System | ||
open System.Threading | ||
|
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