<commit_before>package redis

import org.specs2.mutable.{SpecificationLike, Tags}
import akka.util.Timeout
import org.specs2.time.NoTimeConversions
import akka.testkit.TestKit
import org.specs2.specification.{Step, Fragments}
import akka.actor.ActorSystem
import java.util.concurrent.atomic.AtomicInteger
import scala.sys.process.Process
import scala.util.Try
import scala.reflect.io.File


abstract class RedisHelper extends TestKit(ActorSystem()) with SpecificationLike with Tags with NoTimeConversions {

  import scala.concurrent.duration._

  implicit val executionContext = system.dispatcher

  implicit val timeout = Timeout(10 seconds)
  val timeOut = 10 seconds
  val longTimeOut = 100 seconds

  override def map(fs: => Fragments) = {
    Step(setup()) ^
      fs ^
      Step({
        system.shutdown()
        cleanup()
      })
  }

  def setup() = {}

  def cleanup() = {}

  val redisServerCmd = "redis-server"
  val redisServerLogLevel = "--loglevel verbose"
}

abstract class RedisSpec extends RedisHelper {

  val redis = RedisClient()

  def withRedisServer[T](block: (Int) => T): T = {
    val serverPort = RedisServerHelper.portNumber.getAndIncrement()
    val serverProcess = Process(s"$redisServerCmd --port $serverPort $redisServerLogLevel").run()

    val result = Try(block(serverPort)).get

    serverProcess.destroy()

    result
  }
}


abstract class RedisClusterClients(val masterName: String = "mymaster") extends RedisHelper {

  import RedisServerHelper._

  val masterPort = portNumber.getAndIncrement()
  val slavePort = portNumber.getAndIncrement()
  val sentinelPort = portNumber.getAndIncrement()

  lazy val redisClient = RedisClient(port = masterPort)
  lazy val sentinelClient = SentinelClient(port = sentinelPort)
  lazy val sentinelMonitoredRedisClient = SentinelMonitoredRedisClient(sentinelPort = sentinelPort, master = masterName)

  var master: Process = null
  var slave: Process = null
  var sentinel: Process = null

  override def setup() = {
    val sentinelConf =
      s"""
        |port $sentinelPort
        |sentinel monitor $masterName 127.0.0.1 $masterPort 1
        |sentinel down-after-milliseconds $masterName 5000
        |sentinel can-failover $masterName yes
        |sentinel parallel-syncs $masterName 1
        |sentinel failover-timeout $masterName 10000
      """.stripMargin<commit_msg>fix test<commit_after>package redis

import org.specs2.mutable.{SpecificationLike, Tags}
import akka.util.Timeout
import org.specs2.time.NoTimeConversions
import akka.testkit.TestKit
import org.specs2.specification.{Step, Fragments}
import akka.actor.ActorSystem
import java.util.concurrent.atomic.AtomicInteger
import scala.sys.process.Process
import scala.util.Try
import scala.reflect.io.File


abstract class RedisHelper extends TestKit(ActorSystem()) with SpecificationLike with Tags with NoTimeConversions {

  import scala.concurrent.duration._

  implicit val executionContext = system.dispatcher

  implicit val timeout = Timeout(10 seconds)
  val timeOut = 10 seconds
  val longTimeOut = 100 seconds

  override def map(fs: => Fragments) = {
    Step(setup()) ^
      fs ^
      Step({
        system.shutdown()
        cleanup()
      })
  }

  def setup() = {}

  def cleanup() = {}

  val redisServerCmd = "redis-server"
  val redisServerLogLevel = "--loglevel verbose"
}

abstract class RedisSpec extends RedisHelper {

  val redis = RedisClient()

  def withRedisServer[T](block: (Int) => T): T = {
    val serverPort = RedisServerHelper.portNumber.getAndIncrement()
    val serverProcess = Process(s"$redisServerCmd --port $serverPort $redisServerLogLevel").run()

    val result = Try(block(serverPort))

    serverProcess.destroy()

    result.get
  }
}


abstract class RedisClusterClients(val masterName: String = "mymaster") extends RedisHelper {

  import RedisServerHelper._

  val masterPort = portNumber.getAndIncrement()
  val slavePort = portNumber.getAndIncrement()
  val sentinelPort = portNumber.getAndIncrement()

  lazy val redisClient = RedisClient(port = masterPort)
  lazy val sentinelClient = SentinelClient(port = sentinelPort)
  lazy val sentinelMonitoredRedisClient = SentinelMonitoredRedisClient(sentinelPort = sentinelPort, master = masterName)

  var master: Process = null
  var slave: Process = null
  var sentinel: Process = null

  override def setup() = {
    val sentinelConf =
      s"""
        |port $sentinelPort
        |sentinel monitor $masterName 127.0.0.1 $masterPort 1
        |sentinel down-after-milliseconds $masterName 5000
        |sentinel can-failover $masterName yes
        |sentinel parallel-syncs $masterName 1
        |sentinel failover-timeout $masterName 10000
      """.stripMargin
{'fraction_non_alphanumeric': 0.08996392955654572, 'fraction_numerical': 0.011457670273711012, 'mean_word_length': 3.941299790356394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6798606', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1404, 'n_words': 360}