<commit_before>package hercules.actors.demultiplexing

import org.scalatest.BeforeAndAfterAll
import org.scalatest.FlatSpecLike
import org.scalatest.Matchers
import akka.actor.ActorSystem
import akka.testkit.ImplicitSender
import akka.testkit.TestKit
import hercules.demultiplexing.Demultiplexer
import hercules.entities.illumina.HiSeqProcessingUnit
import hercules.entities.illumina.IlluminaProcessingUnit
import hercules.config.processingunit.IlluminaProcessingUnitConfig
import java.io.File
import java.net.URI
import akka.actor.Props
import hercules.test.utils.StepParent
import hercules.demultiplexing.DemultiplexingResult
import akka.actor.PoisonPill
import hercules.protocols.HerculesMainProtocol
import scala.concurrent.duration._
import java.io.PrintWriter

class SisyphusDemultiplexingExecutorActorTest extends TestKit(ActorSystem("SisyphusDemultiplexingExecutorActorTest"))
    with ImplicitSender
    with FlatSpecLike
    with BeforeAndAfterAll
    with Matchers {

  // The processing unit to send that we will return
  val processingUnit: IlluminaProcessingUnit =
    new HiSeqProcessingUnit(
      new IlluminaProcessingUnitConfig(
        new File("Samplesheet1"),
        new File("DefaultQC"),
        Some(new File("DefaultProg"))),
      new URI("/path/to/runfolder1"))

  val logFile = new File("fake.log")
  val writer = new PrintWriter(logFile)
  val logText = "To be or not to be, that is the question?"
  writer.println(logText)
  writer.close()

  // A fake fetcher class which will just return the processing untis
  // defined above.
  class FakeDemultiplexer(succeed: Boolean) extends Demultiplexer {
    var cleanUpRan: Boolean = false

    def cleanup(unit: hercules.entities.ProcessingUnit): Unit =
      cleanUpRan = true
    def demultiplex(unit: hercules.entities.ProcessingUnit): hercules.demultiplexing.DemultiplexingResult =
      new DemultiplexingResult(succeed, Some(logFile))
  }

  override def afterAll(): Unit = {
    system.shutdown()
    logFile.delete()
    Thread.sleep(1000)
  }

  "A SisyphusDemultiplexingExecutorActor" should " send success message if the demultiplexing was successful" in {

    val demultiplexer = new FakeDemultiplexer(succeed = true)

    val demultiplexerActor = system.actorOf(
      SisyphusDemultiplexingExecutorActor.props(demultiplexer))

    demultiplexerActor ! HerculesMainProtocol.StartDemultiplexingProcessingUnitMessage(processingUnit)
    expectMsg(3 second, HerculesMainProtocol.FinishedDemultiplexingProcessingUnitMessage(processingUnit))
    demultiplexerActor ! PoisonPill
  }

  it should "send a fail message if the demultiplexing failed " in {

    val demultiplexer = new FakeDemultiplexer(succeed = false)

    val demultiplexerActor = system.actorOf(
      SisyphusDemultiplexingExecutorActor.props(demultiplexer))

    demultiplexerActor ! HerculesMainProtocol.StartDemultiplexingProcessingUnitMessage(processingUnit)
    expectMsg(3 second, HerculesMainProtocol.FailedDemultiplexingProcessingUnitMessage(processingUnit, logText))
    demultiplexerActor ! PoisonPill

  }
}<commit_msg>adapt FakeDemultiplexer to return a future<commit_after>package hercules.actors.demultiplexing

import org.scalatest.BeforeAndAfterAll
import org.scalatest.FlatSpecLike
import org.scalatest.Matchers
import akka.actor.ActorSystem
import akka.testkit.ImplicitSender
import akka.testkit.TestKit
import hercules.demultiplexing.Demultiplexer
import hercules.entities.illumina.HiSeqProcessingUnit
import hercules.entities.illumina.IlluminaProcessingUnit
import hercules.config.processingunit.IlluminaProcessingUnitConfig
import java.io.File
import java.net.URI
import akka.actor.Props
import hercules.test.utils.StepParent
import hercules.demultiplexing.DemultiplexingResult
import akka.actor.PoisonPill
import hercules.protocols.HerculesMainProtocol
import scala.concurrent.{ duration, Future, ExecutionContext }
import java.io.PrintWriter

class SisyphusDemultiplexingExecutorActorTest extends TestKit(ActorSystem("SisyphusDemultiplexingExecutorActorTest"))
    with ImplicitSender
    with FlatSpecLike
    with BeforeAndAfterAll
    with Matchers {

  import duration._

  // The processing unit to send that we will return
  val processingUnit: IlluminaProcessingUnit =
    new HiSeqProcessingUnit(
      new IlluminaProcessingUnitConfig(
        new File("Samplesheet1"),
        new File("DefaultQC"),
        Some(new File("DefaultProg"))),
      new URI("/path/to/runfolder1"))

  val logFile = new File("fake.log")
  val writer = new PrintWriter(logFile)
  val logText = "To be or not to be, that is the question?"
  writer.println(logText)
  writer.close()

  // A fake fetcher class which will just return the processing untis
  // defined above.
  class FakeDemultiplexer(succeed: Boolean) extends Demultiplexer {
    var cleanUpRan: Boolean = false

    def cleanup(unit: hercules.entities.ProcessingUnit): Unit =
      cleanUpRan = true
    def demultiplex(unit: hercules.entities.ProcessingUnit)(implicit executor: ExecutionContext): Future[hercules.demultiplexing.DemultiplexingResult] =
      Future.successful(new DemultiplexingResult(succeed, Some(logFile)))
  }

  override def afterAll(): Unit = {
    system.shutdown()
    logFile.delete()
    Thread.sleep(1000)
  }

  "A SisyphusDemultiplexingExecutorActor" should " send success message if the demultiplexing was successful" in {

    val demultiplexer = new FakeDemultiplexer(succeed = true)

    val demultiplexerActor = system.actorOf(
      SisyphusDemultiplexingExecutorActor.props(demultiplexer))

    demultiplexerActor ! HerculesMainProtocol.StartDemultiplexingProcessingUnitMessage(processingUnit)
    expectMsg(3 second, HerculesMainProtocol.FinishedDemultiplexingProcessingUnitMessage(processingUnit))
    demultiplexerActor ! PoisonPill
  }

  it should "send a fail message if the demultiplexing failed " in {

    val demultiplexer = new FakeDemultiplexer(succeed = false)

    val demultiplexerActor = system.actorOf(
      SisyphusDemultiplexingExecutorActor.props(demultiplexer))

    demultiplexerActor ! HerculesMainProtocol.StartDemultiplexingProcessingUnitMessage(processingUnit)
    expectMsg(3 second, HerculesMainProtocol.FailedDemultiplexingProcessingUnitMessage(processingUnit, logText))
    demultiplexerActor ! PoisonPill

  }
}
{'fraction_non_alphanumeric': 0.06909667194928684, 'fraction_numerical': 0.0025356576862123614, 'mean_word_length': 5.223865877712032, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '144954', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1810, 'n_words': 404}