Skip to content

Commit

Permalink
Add FileMonitor() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
K1li4nL committed May 18, 2023
1 parent 572d331 commit 39ed6f5
Showing 1 changed file with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
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
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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 39ed6f5

Please sign in to comment.