Skip to content

Instantly share code, notes, and snippets.

@Horusiath
Created May 29, 2016 19:58
Show Gist options
  • Save Horusiath/ffa4151de46843940e9a82f352f9bb00 to your computer and use it in GitHub Desktop.
Save Horusiath/ffa4151de46843940e9a82f352f9bb00 to your computer and use it in GitHub Desktop.
open System
open Akka.Actor
open Akka.FSharp
open Akka.TestKit
open Akka.TestKit.Xunit2
type Tck = TestKit
/// <summary>
/// Runs a test case function in context of TestKit aware actor system.
/// </summary>
/// <param name="config">Configuration used for actor system initialization.</param>
/// <param name="fn">Test case function</param>
let test (config : Akka.Configuration.Config) (fn : Tck -> unit) =
use system = System.create "test-system" config
use tck = new TestKit(system)
fn tck
/// <summary>
/// Runs a test case function using default configuration.
/// </summary>
let testDefault t = test (Akka.TestKit.Configs.TestConfigs.TestSchedulerConfig) t
let inline probe (tck: Tck) : TestProbe = tck.CreateTestProbe()
let inline barrier (tck: Tck) (count: int) : TestBarrier = tck.CreateTestBarrier count
let inline latch (tck: Tck) (count: int) : TestLatch = tck.CreateTestLatch count
let inline testActor (tck: Tck) (name: string) : IActorRef = tck.CreateTestActor name
let inline monitor (tck : Tck) (ref : IActorRef) : unit = tck.Watch ref |> ignore
let inline demonitor (tck: Tck) (ref: IActorRef) : unit = tck.Unwatch ref |> ignore
let expectMsg (tck : Tck) (msg : 't) : 't option =
let reply = tck.ExpectMsg<'t>(msg, Nullable(), "")
if reply <> Unchecked.defaultof<'t> then Some reply
else None
let expectMsgWithin (tck : Tck) (timeout: TimeSpan) (msg : 't) : 't option =
let reply = tck.ExpectMsg<'t>(msg, Nullable(timeout), "")
if reply <> Unchecked.defaultof<'t> then Some reply
else None
let expectMsgFilter (tck: Tck) (predicate: 't->bool) : 't option =
let reply = tck.ExpectMsg<'t>(Predicate<'t>(predicate))
if reply <> Unchecked.defaultof<'t> then Some reply
else None
let expectMsgFilterWithin (tck: Tck) (timeout: TimeSpan) (predicate: 't->bool) : 't option =
let reply = tck.ExpectMsg<'t>(Predicate<'t>(predicate), Nullable(timeout))
if reply <> Unchecked.defaultof<'t> then Some reply
else None
let inline expectNoMsg (tck : Tck) : unit = tck.ExpectNoMsg()
let inline expectNoMsgWithin (tck: Tck) (timeout: TimeSpan) : unit = tck.ExpectNoMsg timeout
let inline expectMsgAllOf (tck : Tck) (messages : 't seq) : unit =
Array.ofSeq messages
|> tck.ExpectMsgAllOf
|> ignore
let inline expectMsgAnyOf (tck : Tck) (messages : 't seq) : unit = tck.ExpectMsgAnyOf(Array.ofSeq messages) |> ignore
let inline expectTerminated (tck : Tck) (ref : IActorRef) : bool =
tck.ExpectTerminated(ref, Nullable(), "").AddressTerminated
let inline expectTerminatedWithin (timeout : TimeSpan) (tck : Tck) (ref : IActorRef) : bool =
tck.ExpectTerminated(ref, Nullable(timeout), "").AddressTerminated
let inline ignoreMessages (tck: Tck) (predicate: obj -> bool) : unit = tck.IgnoreMessages(System.Func<obj, bool>(predicate))
let inline receiveN (tck: Tck) (n: int) : obj seq = tck.ReceiveN(n) :> System.Collections.Generic.IEnumerable<obj>
let inline receiveOne (tck: Tck) : 't = tck.ReceiveOne() :?> 't
let inline receiveOneWithin (tck: Tck) (timeout: TimeSpan) : 't = tck.ReceiveOne(Nullable(timeout)) :?> 't
let inline within (tck: Tck) (max: TimeSpan) (fn: unit->'t) : 't = tck.Within(max, System.Func<'t>(fn))
let inline deadLettersEvents<'t> (tck: Tck) : IEventFilterApplier = tck.EventFilter.DeadLetter(typeof<'t>)
let inline exnEvents<'t when 't :> exn> (tck: Tck) : IEventFilterApplier = tck.EventFilter.Exception(typeof<'t>)
let inline errorEvents (tck: Tck) : IEventFilterApplier = tck.EventFilter.Error()
let inline warningEvents (tck: Tck) : IEventFilterApplier = tck.EventFilter.Warning()
let inline infoEvents (tck: Tck) : IEventFilterApplier = tck.EventFilter.Info()
let inline debugEvents (tck: Tck) : IEventFilterApplier = tck.EventFilter.Debug()
let inline expectEvent (n:int) (applier: IEventFilterApplier) (fn: unit->'t): 't = applier.Expect(n, System.Func<'t>(fn))
let inline expectEventWithin (timeout: TimeSpan) (n:int) (applier: IEventFilterApplier) (fn: unit->'t): 't = applier.Expect(n, timeout, System.Func<'t>(fn))
@parthopdas
Copy link

Can you please add a "module Akka.FSharp.TestKit" at the start of this file so I can consume it directly from paket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment