Skip to content

Instantly share code, notes, and snippets.

@object
Created April 25, 2024 17:01
Show Gist options
  • Select an option

  • Save object/1a5f9e169ddb8a90f8256e2573c56952 to your computer and use it in GitHub Desktop.

Select an option

Save object/1a5f9e169ddb8a90f8256e2573c56952 to your computer and use it in GitHub Desktop.
F# test for Akka.NET cluster pool router
namespace UnitTests
module ClusterPoolTests =
open System.Threading.Tasks
open Akka.Routing
open Akka.Cluster
open Akka.FSharp
open Xunit
open Xunit.Abstractions
let configWithPort port =
Configuration.parse (
"""
akka
{
actor
{
provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
# serializers {
# hyperion = "Akka.Serialization.HyperionSerializer, Akka.Serialization.Hyperion"
# }
# serialization-bindings {
# "System.Object" = hyperion
# }
# serialization-identifiers {
# "Akka.Serialization.HyperionSerializer, Akka.Serialization.Hyperion" = -5
# "Akka.Serialization.NewtonSoftJsonSerializer, Akka" = 1
# }
deployment
{
/echo
{
router = broadcast-pool
cluster
{
enabled = on
max-nr-of-instances-per-node = 2
max-total-nr-of-instances = 6
allow-local-routees = on
use-role = Upload
}
}
}
}
remote
{
dot-netty.tcp
{
public-hostname = localhost
hostname = localhost
port = """
+ port.ToString()
+ """
}
}
cluster
{
roles = ["Upload"]
seed-nodes = [ "akka.tcp://cluster-system@localhost:5000" ]
min-nr-of-members = 3
}
}
"""
)
type ClusterPoolTests(output: ITestOutputHelper) =
[<Fact>]
member _.``Should broacast messages to all routees``() =
let system1 = System.create "cluster-system" (configWithPort 5000)
let _system2 = System.create "cluster-system" (configWithPort 5001)
let _system3 = System.create "cluster-system" (configWithPort 5002)
let testKit = new Akka.TestKit.Xunit2.TestKit(system1, output);
let echoActor (mailbox: Actor<string>) =
let nodeAddress = Cluster.Get(mailbox.Context.System).SelfUniqueAddress
let rec loop () =
actor {
let! msg = mailbox.Receive()
match msg with
| "ping" ->
logDebug mailbox $"Received ping on {nodeAddress} from {mailbox.Sender().Path}"
testKit.TestActor <! "pong"
| _ -> ()
return! loop()
}
loop ()
let cluster = Cluster.Get system1
let tcs = TaskCompletionSource<Akka.Done>()
cluster.RegisterOnMemberUp(fun () -> tcs.SetResult(Akka.Done.Instance))
tcs.Task |> Async.AwaitTask |> Async.RunSynchronously |> ignore
let pool = spawnOpt system1 "echo" echoActor [SpawnOption.Router(FromConfig.Instance)]
Async.Sleep 1000 |> Async.RunSynchronously
pool <! "ping"
testKit.ReceiveN 6 |> ignore
testKit.ExpectNoMsg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment