Skip to content

Instantly share code, notes, and snippets.

@Horusiath
Last active November 3, 2020 03:38
Show Gist options
  • Save Horusiath/3cdb63c28a7a1b5065c7719cd8d6ec8b to your computer and use it in GitHub Desktop.
Save Horusiath/3cdb63c28a7a1b5065c7719cd8d6ec8b to your computer and use it in GitHub Desktop.
// wrapper function over TestKit.ActorOfAsTestActorRef
let spawnAsTestRef (tck:Tck) (props: Props<'T>) : IActorRef<'T> =
typed (tck.ActorOfAsTestActorRef<FunActor<'T>>(props.ToProps(), tck.TestActor) :> IActorRef)
[<Fact>]
let ``Parent should create child`` () = testDefault <| fun tck ->
// child actor definition
let child (ctx: Actor<_>) msg = ctx.Sender() <! "hello" |> ignored
// parent actor defintion
let rec parent (ctx: Actor<_>) =
// spawn child
let childRef = spawnAnonymous ctx (props (actorOf2 child))
let rec loop () = actor {
let! msg = ctx.Receive()
childRef <<! msg // forward message to child
return loop()
}
loop ()
let testedParent = spawnAsTestRef tck (props parent)
testedParent <! "this should be forwarded to the child"
expectMsg tck "hello" |> ignore
@Horusiath
Copy link
Author

Akka.FSharp version:

open Akka.FSharp.TestKit

// wrapper function over TestKit.ActorOfAsTestActorRef
let spawnAsTestRef (tck:Tck) (f : Actor<_> -> Cont<_, _>) : IActorRef =
    let e = Linq.Expression.ToExpression(fun () -> new FunActor<_, _>(f))
    tck.ActorOfAsTestActorRef<FunActor<_, _>>(Props.Create e, tck.TestActor) :> IActorRef

[<Fact>]
let ``Parent should create child`` () = testDefault <| fun tck ->
    // child actor definition
    let child (ctx: Actor<_>) msg = ctx.Sender() <! "hello"
    // parent actor defintion
    let rec parent (ctx: Actor<_>): Cont<obj, obj> = 
        // spawn child
        let childRef = spawn ctx null (actorOf2 child)
        let rec loop () = actor {
            let! msg = ctx.Receive()
            childRef.Forward msg
            return! loop()
        }
        loop ()

    let testedParent = spawnAsTestRef tck parent
    testedParent <! "this should be forwarded to the child"
    expectMsg tck "hello" |> ignore

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