diff --git a/src/test/scala/service/base/EnvelopeTest.scala b/src/test/scala/service/base/EnvelopeTest.scala index 85c1e7b3c..e62b036a7 100644 --- a/src/test/scala/service/base/EnvelopeTest.scala +++ b/src/test/scala/service/base/EnvelopeTest.scala @@ -54,9 +54,9 @@ class EnvelopeTest extends Specification { "response" in { val input = MessageEnvelope("test", TestFilter, TestMessage(5)) - val output = input.response(TestStamp, StringWithSlashes) + val output = input.response(TestStamp) output match { - case reply @ GenericResponseEnvelope("/test/", TestFilter, TestMessage(5)) => + case reply @ GenericResponseEnvelope("/test/out", TestFilter, TestMessage(5)) => reply.stamp mustEqual TestStamp case _ => ko @@ -65,9 +65,9 @@ class EnvelopeTest extends Specification { "response (different from input)" in { val input = MessageEnvelope("test", TestFilter, TestInputMessage(5)) - val output = input.response(TestStamp, StringWithSlashes) + val output = input.response(TestStamp) output match { - case reply @ GenericResponseEnvelope("/test/", TestFilter, TestOutputEvent(7)) => + case reply @ GenericResponseEnvelope("/test/out", TestFilter, TestOutputEvent(7)) => reply.stamp mustEqual TestStamp case _ => ko diff --git a/src/test/scala/service/base/EventServiceCacheSupportTest.scala b/src/test/scala/service/base/EventServiceCacheSupportTest.scala index ae24bd796..860eccf49 100644 --- a/src/test/scala/service/base/EventServiceCacheSupportTest.scala +++ b/src/test/scala/service/base/EventServiceCacheSupportTest.scala @@ -1,20 +1,18 @@ // Copyright (c) 2026 PSForever package service.base -import akka.actor.Props +import akka.actor.{ActorRef, ActorSystem, Props} import akka.testkit.TestProbe import base.ActorTest import net.psforever.services.Service import net.psforever.services.base.message.EventMessage -import net.psforever.services.base.{CachedGenericEventEnvelope, EventServiceSupport, GenericEventServiceWithCacheAndSupport, GenericSupportEnvelope} +import net.psforever.services.base.{CachedEnvelope, CachedGenericEventEnvelope, EventServiceSupport, GenericEventServiceWithCacheAndSupport, GenericSupportEnvelope} import net.psforever.types.PlanetSideGUID +import service.base.EventServiceSupportTest.TestSupportService import scala.concurrent.duration._ object EventServiceCacheSupportTest { - class TestCacheService(eventSupportServices: List[EventServiceSupport]) - extends GenericEventServiceWithCacheAndSupport(EventServiceTestBase.TestStamp, eventSupportServices) - final case class CachedSupportTestEnvelope( guid: PlanetSideGUID, originalChannel: String, @@ -25,25 +23,31 @@ object EventServiceCacheSupportTest { def filter: PlanetSideGUID = Service.defaultPlayerGUID def supportLabel: String = "supportActor" } + + class TestCacheService(eventSupportServices: List[EventServiceSupport]) + extends GenericEventServiceWithCacheAndSupport(EventServiceTestBase.TestStamp, eventSupportServices) + + def SpawnTestSystem(eventSupportServices: List[EventServiceSupport])(implicit system: ActorSystem, self: ActorRef): ActorRef = { + val name = self.getClass.getSimpleName.replace("EventServiceCacheSupportTest", "") + system.actorOf(Props(classOf[TestSupportService], eventSupportServices), name = s"EventServiceCacheSupportTest.$name") + } } class EventServiceCacheSupportTestDefault extends ActorTest { - import EventServiceCacheSupportTest._ "GenericEventServiceWithCacheAndSupport" should { "construct" in { - system.actorOf(Props(classOf[TestCacheService], List()), name = "EventServiceCacheSupportTest.0") + EventServiceCacheSupportTest.SpawnTestSystem(List()) } } } class EventServiceCacheSupportTestSupportNormally extends ActorTest { - import EventServiceCacheSupportTest._ import EventServiceTestBase._ "GenericEventServiceWithCacheAndSupport" should { "send a valid message to both subscribed channel and support class, like normal GenericEventServiceWithSupport" in { val mainProbe = TestProbe("MainProbe") val supportProbe = TestProbe("SupportProbe") - val events = system.actorOf(Props(classOf[TestCacheService], List(TestSupportActorLoader)), name = "EventServiceCacheSupportTest.1") + val events = EventServiceCacheSupportTest.SpawnTestSystem(List(TestSupportActorLoader)) events.tell(Service.Join("test"), mainProbe.ref) val originalMessage = TestSupportEnvelope("test", SupportActorRepliesWith("hello world", supportProbe.ref)) events ! originalMessage @@ -66,12 +70,11 @@ class EventServiceCacheSupportTestSupportNormally extends ActorTest { } class EventServiceCacheSupportTestCachedMessages extends ActorTest { - import EventServiceCacheSupportTest._ import EventServiceTestBase._ "GenericEventServiceWithCacheAndSupport" should { "wait on sending designated cache-able messages after a few milliseconds" in { val mainProbe = TestProbe("MainProbe") - val events = system.actorOf(Props(classOf[TestCacheService], List()), name = "EventServiceCacheSupportTest.2") + val events = EventServiceCacheSupportTest.SpawnTestSystem(List()) events.tell(Service.Join("test"), mainProbe.ref) val firstMessage = CachedEnvelope(PlanetSideGUID(1), "test", TestMessage(1)) events ! firstMessage @@ -87,12 +90,11 @@ class EventServiceCacheSupportTestCachedMessages extends ActorTest { } class EventServiceCacheSupportTestMultipleCachedMessagesAtOnce extends ActorTest { - import EventServiceCacheSupportTest._ import EventServiceTestBase._ "GenericEventServiceWithCacheAndSupport" should { "send cache-able messages within a time span in bulk" in { val mainProbe = TestProbe("MainProbe") - val events = system.actorOf(Props(classOf[TestCacheService], List()), name = "EventServiceCacheSupportTest.3") + val events = EventServiceCacheSupportTest.SpawnTestSystem(List()) events.tell(Service.Join("test"), mainProbe.ref) val firstMessage = CachedEnvelope(PlanetSideGUID(1), "test", TestMessage(1)) val secondMessage = CachedEnvelope(PlanetSideGUID(2), "test", TestMessage(2)) @@ -123,12 +125,11 @@ class EventServiceCacheSupportTestMultipleCachedMessagesAtOnce extends ActorTest } class EventServiceCacheSupportTestMultipleCachedSameMessages extends ActorTest { - import EventServiceCacheSupportTest._ import EventServiceTestBase._ "GenericEventServiceWithCacheAndSupport" should { "only caches and dispatches the last message with a given filtering token" in { val mainProbe = TestProbe("MainProbe") - val events = system.actorOf(Props(classOf[TestCacheService], List()), name = "EventServiceCacheSupportTest.4") + val events = EventServiceCacheSupportTest.SpawnTestSystem(List()) events.tell(Service.Join("test"), mainProbe.ref) val firstMessage = CachedEnvelope(PlanetSideGUID(1), "test", TestMessage(1)) val secondMessage = CachedEnvelope(PlanetSideGUID(1), "test", TestMessage(2)) @@ -152,12 +153,11 @@ class EventServiceCacheSupportTestMultipleCachedSameMessages extends ActorTest { } class EventServiceCacheSupportTestMultipleCachedMessages extends ActorTest { - import EventServiceCacheSupportTest._ import EventServiceTestBase._ "GenericEventServiceWithCacheAndSupport" should { "send cache-able messages within a time span, separated if they are part of different caches" in { val mainProbe = TestProbe("MainProbe") - val events = system.actorOf(Props(classOf[TestCacheService], List()), name = "EventServiceCacheSupportTest.5") + val events = EventServiceCacheSupportTest.SpawnTestSystem(List()) events.tell(Service.Join("test"), mainProbe.ref) val firstMessage = CachedEnvelope(PlanetSideGUID(1), "test", TestMessage(1)) //first cache flush @@ -191,7 +191,7 @@ class EventServiceCacheSupportTestSupportDelayed extends ActorTest { "cache a message for a support actor, but only dispatch that message to the support actor when flushing the cache" in { val mainProbe = TestProbe("MainProbe") val supportProbe = TestProbe("SupportProbe") - val events = system.actorOf(Props(classOf[TestCacheService], List(TestSupportActorLoader)), name = "EventServiceCacheSupportTest.6") + val events = EventServiceCacheSupportTest.SpawnTestSystem(List(TestSupportActorLoader)) events.tell(Service.Join("test"), mainProbe.ref) val originalMessage = CachedSupportTestEnvelope( PlanetSideGUID(1), diff --git a/src/test/scala/service/base/EventServiceSupportTest.scala b/src/test/scala/service/base/EventServiceSupportTest.scala index 1b61e6eaf..53d96d3c7 100644 --- a/src/test/scala/service/base/EventServiceSupportTest.scala +++ b/src/test/scala/service/base/EventServiceSupportTest.scala @@ -1,7 +1,7 @@ // Copyright (c) 2026 PSForever package service.base -import akka.actor.Props +import akka.actor.{ActorRef, ActorSystem, Props} import akka.testkit.TestProbe import base.ActorTest import net.psforever.services.Service @@ -10,40 +10,42 @@ import net.psforever.services.base.{EventServiceSupport, GenericEventServiceWith import scala.concurrent.duration._ object EventServiceSupportTest { + final case class TestSupportOnlyEnvelope(supportLabel: String, supportMessage: EventServiceTestBase.SupportActorRepliesWith) + extends GenericSupportEnvelopeOnly + class TestSupportService(eventSupportServices: List[EventServiceSupport]) extends GenericEventServiceWithSupport(EventServiceTestBase.TestStamp, eventSupportServices) - final case class TestSupportOnlyEnvelope(supportLabel: String, supportMessage: EventServiceTestBase.SupportActorRepliesWith) - extends GenericSupportEnvelopeOnly + def SpawnTestSystem(eventSupportServices: List[EventServiceSupport])(implicit system: ActorSystem, self: ActorRef): ActorRef = { + val name = self.getClass.getSimpleName.replace("EventServiceSupportTest", "") + system.actorOf(Props(classOf[TestSupportService], eventSupportServices), name = s"EventServiceSupportTest.$name") + } } class EventServiceSupportTestDefault extends ActorTest { - import EventServiceSupportTest._ "GenericEventServiceWithSupport" should { "construct" in { - system.actorOf(Props(classOf[TestSupportService], List()), name = "EventServiceSupportTest.0") + EventServiceSupportTest.SpawnTestSystem(List()) } } } class EventServiceSupportTestLoadSupportClass extends ActorTest { - import EventServiceSupportTest._ import EventServiceTestBase._ "GenericEventServiceWithSupport" should { "construct with setting up a test support class" in { - system.actorOf(Props(classOf[TestSupportService], List(TestSupportActorLoader)), name = "EventServiceSupportTest.1") + EventServiceSupportTest.SpawnTestSystem(List(TestSupportActorLoader)) } } } class EventServiceSupportTestSendToSupportClass extends ActorTest { - import EventServiceSupportTest._ import EventServiceTestBase._ "GenericEventServiceWithSupport" should { "send a valid message to both subscribed channel and support class" in { val mainProbe = TestProbe("MainProbe") val supportProbe = TestProbe("SupportProbe") - val events = system.actorOf(Props(classOf[TestSupportService], List(TestSupportActorLoader)), name = "EventServiceSupportTest.2") + val events = EventServiceSupportTest.SpawnTestSystem(List(TestSupportActorLoader)) events.tell(Service.Join("test"), mainProbe.ref) val originalMessage = TestSupportEnvelope("test", SupportActorRepliesWith("hello world", supportProbe.ref)) events ! originalMessage @@ -66,13 +68,12 @@ class EventServiceSupportTestSendToSupportClass extends ActorTest { } class EventServiceSupportTestSendToSupportClassOnly1 extends ActorTest { - import EventServiceSupportTest._ import EventServiceTestBase._ "GenericEventServiceWithSupport" should { "send a valid message to support class (but not to subscribed channel)" in { val mainProbe = TestProbe("MainProbe") val supportProbe = TestProbe("SupportProbe") - val events = system.actorOf(Props(classOf[TestSupportService], List(TestSupportActorLoader)), name = "EventServiceSupportTest.3") + val events = EventServiceSupportTest.SpawnTestSystem(List(TestSupportActorLoader)) events.tell(Service.Join("test"), mainProbe.ref) val originalMessage = TestSupportEnvelope("notatest", SupportActorRepliesWith("hello world", supportProbe.ref)) events ! originalMessage @@ -96,7 +97,7 @@ class EventServiceSupportTestSendToSupportClassOnly2 extends ActorTest { "send a valid message to support class (skip subscribed channel)" in { val mainProbe = TestProbe("MainProbe") val supportProbe = TestProbe("SupportProbe") - val events = system.actorOf(Props(classOf[TestSupportService], List(TestSupportActorLoader)), name = "EventServiceSupportTest.4") + val events = EventServiceSupportTest.SpawnTestSystem(List(TestSupportActorLoader)) events.tell(Service.Join("test"), mainProbe.ref) val originalMessage = TestSupportOnlyEnvelope("supportActor", SupportActorRepliesWith("hello world", supportProbe.ref)) events ! originalMessage @@ -120,7 +121,7 @@ class EventServiceSupportTestSendToNoOne extends ActorTest { "send a valid message that neither support class nor subscribed channel handle" in { val mainProbe = TestProbe("MainProbe") val supportProbe = TestProbe("SupportProbe") - val events = system.actorOf(Props(classOf[TestSupportService], List(TestSupportActorLoader)), name = "EventServiceSupportTest.5") + val events = EventServiceSupportTest.SpawnTestSystem(List(TestSupportActorLoader)) events.tell(Service.Join("test"), mainProbe.ref) val originalMessage = TestSupportOnlyEnvelope("notASupportActor", SupportActorRepliesWith("hello world", supportProbe.ref)) events ! originalMessage diff --git a/src/test/scala/service/base/EventServiceTest.scala b/src/test/scala/service/base/EventServiceTest.scala index 022063dd1..86f169dd0 100644 --- a/src/test/scala/service/base/EventServiceTest.scala +++ b/src/test/scala/service/base/EventServiceTest.scala @@ -1,7 +1,7 @@ // Copyright (c) 2026 PSForever package service.base -import akka.actor.Props +import akka.actor.{ActorRef, ActorSystem, Props} import akka.testkit.TestProbe import base.ActorTest import net.psforever.services.Service @@ -12,36 +12,50 @@ import scala.concurrent.duration._ object EventServiceTest { class TestService() extends GenericEventService(EventServiceTestBase.TestStamp) + + def SpawnTestSystem()(implicit system: ActorSystem, self: ActorRef): ActorRef = { + val name = self.getClass.getSimpleName.replace("EventServiceTest", "") + system.actorOf(Props[TestService](), name = s"EventServiceTest.$name") + } } class EventServiceTestDefault extends ActorTest { - import EventServiceTest._ "GenericEventSystem" should { "construct" in { - system.actorOf(Props[TestService](), name = "EventServiceTest.0") + EventServiceTest.SpawnTestSystem() } } } -class EventServiceTest1 extends ActorTest { - import EventServiceTest._ +class EventServiceTestSubscribe extends ActorTest { "GenericEventSystem" should { "be subscribed to by channel name" in { val probe = TestProbe("testProbe") - val events = system.actorOf(Props[TestService](), name = "EventServiceTest.1") + val events = EventServiceTest.SpawnTestSystem() events.tell(Service.Join("test"), probe.ref) probe.expectNoMessage(100 milliseconds) } } } -class EventServiceTest2 extends ActorTest { - import EventServiceTest._ +class EventServiceTestSubscribeConfirm extends ActorTest { + "GenericEventSystem" should { + "be subscribed to by channel name and send a confirmation when prompted" in { + val probe = TestProbe("testProbe") + val events = EventServiceTest.SpawnTestSystem() + events.tell(Service.Join("test", sendJoinConfirmation = true), probe.ref) + val reply = probe.receiveOne(100 milliseconds) + assert(reply == Service.JoinConfirmation(events, "test"), "join confirmation expected but not received") + } + } +} + +class EventServiceTestSubscriptionMessage extends ActorTest { import EventServiceTestBase._ "GenericEventSystem" should { "receive messages from a subscribed channel" in { val probe = TestProbe("testProbe") - val events = system.actorOf(Props[TestService](), name = "EventServiceTest.2") + val events = EventServiceTest.SpawnTestSystem() events.tell(Service.Join("test"), probe.ref) events ! MessageEnvelope("test", Service.defaultPlayerGUID, TestMessage(5)) probe.receiveN(1, 100 milliseconds) @@ -49,16 +63,15 @@ class EventServiceTest2 extends ActorTest { } } -class EventServiceTest3 extends ActorTest { - import EventServiceTest._ +class EventServiceTestSubscriptionReply extends ActorTest { import EventServiceTestBase._ "GenericEventSystem" should { "receive messages that are responses to the original message from a subscribed channel" in { val probe = TestProbe("testProbe") - val events = system.actorOf(Props[TestService](), name = "EventServiceTest.3") + val events = EventServiceTest.SpawnTestSystem() val msg = MessageEnvelope("test", Service.defaultPlayerGUID, TestMessage(5)) // s => "/$s" is the default channel manipulation of the event system - val formalReply = msg.response(EventServiceTestBase.TestStamp, s => "/$s") + val formalReply = msg.response(EventServiceTestBase.TestStamp) events.tell(Service.Join("test"), probe.ref) events ! MessageEnvelope("test", Service.defaultPlayerGUID, TestMessage(5)) val reply = probe.receiveOne(100 milliseconds) @@ -67,14 +80,13 @@ class EventServiceTest3 extends ActorTest { } } -class EventServiceTest4 extends ActorTest { - import EventServiceTest._ +class EventServiceTestNotSubscribed extends ActorTest { import EventServiceTestBase._ "GenericEventSystem" should { "not receive messages from an unsubscribed channel" in { val probe = TestProbe("testProbe") val missedProbe = TestProbe("testProbe") - val events = system.actorOf(Props[TestService](), name = "EventServiceTest.4") + val events = EventServiceTest.SpawnTestSystem() events.tell(Service.Join("test"), probe.ref) events.tell(Service.Join("notATest"), missedProbe.ref) events ! MessageEnvelope("test", Service.defaultPlayerGUID, TestMessage(5)) @@ -88,13 +100,12 @@ class EventServiceTest4 extends ActorTest { } } -class EventServiceTest5 extends ActorTest { - import EventServiceTest._ +class EventServiceTestUnexpectedMessages extends ActorTest { import EventServiceTestBase._ "GenericEventSystem" should { "ignore unexpected messages" in { val probe = TestProbe("testProbe") - val events = system.actorOf(Props[TestService](), name = "EventServiceTest.5") + val events = EventServiceTest.SpawnTestSystem() events.tell(Service.Join("test"), probe.ref) events ! TestMessage(5) probe.expectNoMessage(250 milliseconds) @@ -103,13 +114,12 @@ class EventServiceTest5 extends ActorTest { } } -class EventServiceTest6 extends ActorTest { - import EventServiceTest._ +class EventServiceTestLeave extends ActorTest { import EventServiceTestBase._ "GenericEventSystem" should { "leave single channels" in { val probe = TestProbe("testProbe") - val events = system.actorOf(Props[TestService](), name = "EventServiceTest.6") + val events = EventServiceTest.SpawnTestSystem() events.tell(Service.Join("test"), probe.ref) events.tell(Service.Join("anotherTest"), probe.ref) @@ -125,7 +135,7 @@ class EventServiceTest6 extends ActorTest { case _ => assert(false, "(4) message expected but not received") } - events.tell(Service.Leave(Some("anotherTest")), probe.ref) + events.tell(Service.Leave("anotherTest"), probe.ref) events ! MessageEnvelope("test", Service.defaultPlayerGUID, TestMessage(5)) events ! MessageEnvelope("anotherTest", Service.defaultPlayerGUID, TestMessage(6)) val reply2 = probe.receiveOne(100 milliseconds) @@ -138,13 +148,12 @@ class EventServiceTest6 extends ActorTest { } } -class EventServiceTest7 extends ActorTest { - import EventServiceTest._ +class EventServiceTestLeaveAll1 extends ActorTest { import EventServiceTestBase._ "GenericEventSystem" should { "leave all channels (1)" in { val probe = TestProbe("testProbe") - val events = system.actorOf(Props[TestService](), name = "EventServiceTest.7") + val events = EventServiceTest.SpawnTestSystem() events.tell(Service.Join("test"), probe.ref) events.tell(Service.Join("anotherTest"), probe.ref) @@ -168,13 +177,12 @@ class EventServiceTest7 extends ActorTest { } } -class EventServiceTest8 extends ActorTest { - import EventServiceTest._ +class EventServiceTestLeaveAll2 extends ActorTest { import EventServiceTestBase._ "GenericEventSystem" should { "leave all channels" in { val probe = TestProbe("testProbe") - val events = system.actorOf(Props[TestService](), name = "EventServiceTest.8") + val events = EventServiceTest.SpawnTestSystem() events.tell(Service.Join("test"), probe.ref) events.tell(Service.Join("anotherTest"), probe.ref)