From 39ed6f52fcc4ae55ca75150ae711819076dc6390 Mon Sep 17 00:00:00 2001 From: lauener Date: Thu, 18 May 2023 15:23:21 +0200 Subject: [PATCH] Add FileMonitor() tests --- .../epfl/pop/decentralized/MonitorSuite.scala | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/be2-scala/src/test/scala/ch/epfl/pop/decentralized/MonitorSuite.scala b/be2-scala/src/test/scala/ch/epfl/pop/decentralized/MonitorSuite.scala index 05d6396abe..46262b85d7 100644 --- a/be2-scala/src/test/scala/ch/epfl/pop/decentralized/MonitorSuite.scala +++ b/be2-scala/src/test/scala/ch/epfl/pop/decentralized/MonitorSuite.scala @@ -1,8 +1,10 @@ package ch.epfl.pop.decentralized -import akka.actor.ActorSystem +import akka.actor.{ActorRef, ActorSystem} import akka.stream.scaladsl.Source import akka.testkit.{TestKit, TestProbe} +import ch.epfl.pop.config.RuntimeEnvironment +import ch.epfl.pop.config.RuntimeEnvironmentTestingHelper.{deleteTestConfig, testWriteToServerPeersConfig} import ch.epfl.pop.model.network.method.ParamsWithMap import ch.epfl.pop.model.network.{JsonRpcRequest, MethodType} import ch.epfl.pop.pubsub.graph.validators.RpcValidator @@ -10,6 +12,9 @@ import org.scalatest.BeforeAndAfterAll import org.scalatest.funsuite.{AnyFunSuiteLike => FunSuiteLike} import org.scalatest.matchers.should.Matchers import util.examples.JsonRpcRequestExample + +import java.io.{File, PrintWriter} +import java.nio.file.Path import scala.concurrent.duration.DurationInt class MonitorSuite extends TestKit(ActorSystem("MonitorSuiteActorSystem")) with FunSuiteLike with Matchers with BeforeAndAfterAll { @@ -99,4 +104,40 @@ class MonitorSuite extends TestKit(ActorSystem("MonitorSuiteActorSystem")) with testProbe.send(monitorRef, Monitor.AtLeastOneServerConnected) testProbe.expectMsgType[Monitor.GenerateAndSendHeartbeat](timeout) } + + test("monitor should send ConnectTo() requests to ConnectionMediator upon relevant config file change") { + val mockConnectionMediator = TestProbe() + val monitorRef = system.actorOf(Monitor.props(ActorRef.noSender)) + + // Ping monitor to inform it of ConnectionMediatorRef + mockConnectionMediator.send(monitorRef, ConnectionMediator.Ping()) + + // Expect no message as long as the server peers list is untouched + mockConnectionMediator.expectNoMessage(timeout) + + val newContent = List("some", "strings") + testWriteToServerPeersConfig(newContent) + + mockConnectionMediator.expectMsgType[ConnectionMediator.ConnectTo](timeout) + deleteTestConfig() + } + + test( "monitor should not react upon non relevant events in config directory") { + val mockConnectionMediator = TestProbe() + val monitorRef = system.actorOf(Monitor.props(ActorRef.noSender)) + + // Ping monitor to inform it of ConnectionMediatorRef + mockConnectionMediator.send(monitorRef, ConnectionMediator.Ping()) + + // Create new file in the directory + val filePath = Path.of(RuntimeEnvironment.serverPeersListPath).getParent.toString + File.separator + "DELETE_ME" + val file = new PrintWriter(filePath) + file.write("Hello") + file.close() + + // Set the file we created to delete itself after the jvm shutdown + new File(filePath).deleteOnExit() + + mockConnectionMediator.expectNoMessage(timeout) + } }