/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * license agreements; and to You under the Apache License, version 2.0:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * This file is part of the Apache Pekko project, which was derived from Akka.
 */

/*
 * Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
 */

package org.apache.pekko.cluster.routing

import scala.concurrent.Await

import org.apache.pekko
import pekko.actor.{ Actor, ActorRef, Props }
import pekko.cluster.MultiNodeClusterSpec
import pekko.pattern.ask
import pekko.remote.testkit.MultiNodeConfig
import pekko.routing.{ Broadcast, ConsistentHashingGroup, GetRoutees, Routees }
import pekko.routing.ConsistentHashingRouter.ConsistentHashMapping
import pekko.testkit._

object ClusterConsistentHashingGroupMultiJvmSpec extends MultiNodeConfig {

  // using Java serialization because of `Any` in `Collected` (don't want to spend time on rewriting test)
  case object Get extends JavaSerializable
  final case class Collected(messages: Set[Any]) extends JavaSerializable

  class Destination extends Actor {
    var receivedMessages = Set.empty[Any]
    def receive = {
      case Get => sender() ! Collected(receivedMessages)
      case m   => receivedMessages += m
    }
  }

  val first = role("first")
  val second = role("second")
  val third = role("third")

  commonConfig(debugConfig(on = false).withFallback(MultiNodeClusterSpec.clusterConfig))

}

class ClusterConsistentHashingGroupMultiJvmNode1 extends ClusterConsistentHashingGroupSpec
class ClusterConsistentHashingGroupMultiJvmNode2 extends ClusterConsistentHashingGroupSpec
class ClusterConsistentHashingGroupMultiJvmNode3 extends ClusterConsistentHashingGroupSpec

abstract class ClusterConsistentHashingGroupSpec
    extends MultiNodeClusterSpec(ClusterConsistentHashingGroupMultiJvmSpec)
    with ImplicitSender
    with DefaultTimeout {
  import ClusterConsistentHashingGroupMultiJvmSpec._

  def currentRoutees(router: ActorRef) =
    Await.result(router ? GetRoutees, timeout.duration).asInstanceOf[Routees].routees

  "A cluster router with a consistent hashing group" must {
    "start cluster with 3 nodes" taggedAs LongRunningTest in {
      system.actorOf(Props[Destination](), "dest")
      awaitClusterUp(first, second, third)
      enterBarrier("after-1")
    }

    "send to same destinations from different nodes" taggedAs LongRunningTest in {
      def hashMapping: ConsistentHashMapping = {
        case s: String => s
      }
      val paths = List("/user/dest")
      val router = system.actorOf(
        ClusterRouterGroup(
          local = ConsistentHashingGroup(paths, hashMapping = hashMapping),
          settings = ClusterRouterGroupSettings(totalInstances = 10, paths, allowLocalRoutees = true)).props(),
        "router")
      // it may take some time until router receives cluster member events
      awaitAssert { currentRoutees(router).size should ===(3) }
      val keys = List("A", "B", "C", "D", "E", "F", "G")
      for (_ <- 1 to 10; k <- keys) { router ! k }
      enterBarrier("messages-sent")
      router ! Broadcast(Get)
      val a = expectMsgType[ClusterConsistentHashingGroupMultiJvmSpec.Collected].messages
      val b = expectMsgType[ClusterConsistentHashingGroupMultiJvmSpec.Collected].messages
      val c = expectMsgType[ClusterConsistentHashingGroupMultiJvmSpec.Collected].messages

      a.intersect(b) should ===(Set.empty)
      a.intersect(c) should ===(Set.empty)
      b.intersect(c) should ===(Set.empty)

      (a.size + b.size + c.size) should ===(keys.size)
      enterBarrier("after-2")
    }

  }
}
