<commit_before>package core.events.support

import com.typesafe.scalalogging.StrictLogging
import core.events._
import org.scalatest.concurrent.Eventually._
import org.scalatest.time.{Millis, Seconds, Span}
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Matchers}
import org.slf4j.LoggerFactory

trait EventAssertions extends Matchers with EventMatchers with BeforeAndAfterEach with BeforeAndAfterAll with StrictLogging {
  self: org.scalatest.Suite =>

  EventPublisherRef.ref = new TestEventPublisher()
  CtxSystemRef.ref = EvtSystem("test")

  def clearEvents() =
    EventPublisherRef.ref.asInstanceOf[TestEventPublisher].clear()

  def clearComponentEvents(componentId: String) =
    EventPublisherRef.ref.asInstanceOf[TestEventPublisher].clearComponentEvents(componentId)

  override protected def beforeAll(): Unit = {
    logger.warn("**** > Starting " + this.getClass)
    super.beforeAll()
  }

  override protected def afterAll() {
    super.afterAll()
    logger.warn("**** > Finished " + this.getClass)
  }


  override protected def afterEach(): Unit = {
    clearEvents()
    super.afterEach()
  }

  def events = EventPublisherRef.ref.asInstanceOf[TestEventPublisher].events

  override protected def beforeEach(): Unit = {
    super.beforeEach()
  }

  implicit val patienceConfig =
    PatienceConfig(timeout = scaled(Span(5, Seconds)), interval = scaled(Span(15, Millis)))


  def collectAndPrintEvents() = {
    Thread.sleep(10000)
    printRaisedEvents()
  }

  def printRaisedEvents() = {
    val log = LoggerFactory.getLogger("history")
    log.error("*" * 60 + " RAISED EVENTS: " + "*" * 60)
    EventPublisherRef.ref.asInstanceOf[TestEventPublisher].withOrderedEvents { events =>
      events.foreach { next =>
        LoggerEventPublisherWithDateHelper.log(next.timestamp, next.event, CtxSystemRef.ref, next.values, s => log.error(s))
      }
    }
    log.error("*" * 120 + "\n\n\n\n")
  }

  def report(x: Throwable) = {
    val log = LoggerFactory.getLogger("history")
    log.error("Test failed", x)
    log.error("*" * 60 + " RAISED EVENTS: " + "*" * 60)
    log.error("Raised events:")
    EventPublisherRef.ref.asInstanceOf[TestEventPublisher].withOrderedEvents { events =>
      events.foreach { next =>
        LoggerEventPublisherWithDateHelper.log(next.timestamp, next.event, CtxSystemRef.ref, next.values, s => log.error(s))
      }
    }
    log.error("*" * 120 + "\n\n\n\n")
  }

  def waitWithTimeout(millis: Long)(f: => Unit) = {
    val startedAt = System.currentTimeMillis()
    var success = false
    while (!success && System.currentTimeMillis() - startedAt < millis) {
      try {
        f
        success = true
      } catch {
        case x: Throwable => Thread.sleep(15)
      }
    }
    try {
      f
    } catch {
      case x: Throwable =>
        report(x)
        throw x
    }
  }

  def locateAllEvents(event: Event) = {
    expectSomeEvents(event)
    events.get(event)
  }

  def locateLastEvent(event: Event) = locateAllEvents(event).map(_.head)

  def locateFirstEvent(event: Event) = locateAllEvents(event).map(_.head)

  def locateFirstEventFieldValue(event: Event, field: String) = {
    val maybeValue = for (
      all <- locateAllEvents(event);
      first = all.head;
      (f, v) <- first.find { case (f, v) => f.name == field}<commit_msg>test improvements<commit_after>package core.events.support

import com.typesafe.scalalogging.StrictLogging
import core.events._
import org.scalatest.concurrent.Eventually._
import org.scalatest.time.{Millis, Seconds, Span}
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Matchers}
import org.slf4j.LoggerFactory

trait EventAssertions extends Matchers with EventMatchers with BeforeAndAfterEach with BeforeAndAfterAll with StrictLogging {
  self: org.scalatest.Suite =>

  EventPublisherRef.ref = new TestEventPublisher()
  CtxSystemRef.ref = EvtSystem("test")

  def clearEvents() =
    EventPublisherRef.ref.asInstanceOf[TestEventPublisher].clear()

  def clearComponentEvents(componentId: String) =
    EventPublisherRef.ref.asInstanceOf[TestEventPublisher].clearComponentEvents(componentId)

  override protected def beforeAll(): Unit = {
    logger.warn("**** > Starting " + this.getClass)
    super.beforeAll()
  }

  override protected def afterAll() {
    super.afterAll()
    logger.warn("**** > Finished " + this.getClass)
  }


  override protected def afterEach(): Unit = {
    clearEvents()
    super.afterEach()
  }

  def events = EventPublisherRef.ref.asInstanceOf[TestEventPublisher].events

  override protected def beforeEach(): Unit = {
    super.beforeEach()
  }

  implicit val patienceConfig =
    PatienceConfig(timeout = scaled(Span(5, Seconds)), interval = scaled(Span(15, Millis)))


  def collectAndPrintEvents() = {
    Thread.sleep(10000)
    printRaisedEvents()
  }

  def printRaisedEvents() = {
    val log = LoggerFactory.getLogger("history")
    log.error("*" * 60 + " RAISED EVENTS: " + "*" * 60)
    EventPublisherRef.ref.asInstanceOf[TestEventPublisher].withOrderedEvents { events =>
      events.foreach { next =>
        LoggerEventPublisherWithDateHelper.log(next.timestamp, next.event, CtxSystemRef.ref, next.values, s => log.error(s))
      }
    }
    log.error("*" * 120 + "\n\n\n\n")
  }

  def report(x: Throwable) = {
    val log = LoggerFactory.getLogger("history")
    log.error("Test failed", x)
    log.error("*" * 60 + " RAISED EVENTS: " + "*" * 60)
    log.error("Raised events:")
    EventPublisherRef.ref.asInstanceOf[TestEventPublisher].withOrderedEvents { events =>
      events.foreach { next =>
        LoggerEventPublisherWithDateHelper.log(next.timestamp, next.event, CtxSystemRef.ref, next.values, s => log.error(s))
      }
    }
    log.error("*" * 120 + "\n\n\n\n")
  }

  def waitWithTimeout(millis: Long)(f: => Unit) = {
    val allowedAttempts = millis / 15
    var attempt = 0
    var success = false
    while (!success && attempt < allowedAttempts) {
      try {
        f
        success = true
      } catch {
        case x: Throwable =>
          Thread.sleep(15)
          attempt = attempt + 1
      }
    }
    try {
      f
    } catch {
      case x: Throwable =>
        report(x)
        throw x
    }
  }

  def locateAllEvents(event: Event) = {
    expectSomeEvents(event)
    events.get(event)
  }

  def locateLastEvent(event: Event) = locateAllEvents(event).map(_.head)

  def locateFirstEvent(event: Event) = locateAllEvents(event).map(_.head)

  def locateFirstEventFieldValue(event: Event, field: String) = {
    val maybeValue = for (
      all <- locateAllEvents(event);
      first = all.head;
      (f, v) <- first.find { case (f, v) => f.name == field}
{'fraction_non_alphanumeric': 0.12666367578884402, 'fraction_numerical': 0.008075370121130552, 'mean_word_length': 3.391332895600788, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, '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': '7060131', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2061, 'n_words': 422}