Skip to content

Commit

Permalink
Add tests for MessageSender (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jun 15, 2019
1 parent 9ceae8b commit eb05ad0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Emulsion.Tests/Emulsion.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Settings.fs" />
<Compile Include="MessageSystem.fs" />
<Compile Include="MessageSenderTests.fs" />
<Compile Include="MessageSystemTests.fs" />
<Compile Include="Actors/Core.fs" />
<Compile Include="Actors/SyncTaskWatcher.fs" />
<Compile Include="Actors/Telegram.fs" />
Expand Down
59 changes: 59 additions & 0 deletions Emulsion.Tests/MessageSenderTests.fs
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
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
Expand Down
1 change: 0 additions & 1 deletion Emulsion/MessageSender.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ let startActivity(ctx: MessageSenderContext, token: CancellationToken): Sender =
MailboxProcessor.Start(receiver ctx, token)

let send(activity: Sender): OutgoingMessage -> unit = activity.Post
// TODO[F]: Tests for this module.

0 comments on commit eb05ad0

Please sign in to comment.