Skip to content

Commit

Permalink
fix: trying to tame solr specs failures
Browse files Browse the repository at this point in the history
  • Loading branch information
jachro committed Jan 25, 2024
1 parent 35d2c6a commit 6661d53
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package io.renku.search.solr.client

import cats.effect.{IO, Resource}
import io.renku.search.solr.schema.Migrations
import io.renku.solr.client.SolrClient
import io.renku.solr.client.migration.SchemaMigrator
import io.renku.solr.client.util.SolrSpec

Expand All @@ -30,15 +31,15 @@ trait SearchSolrSpec extends SolrSpec:
new Fixture[Resource[IO, SearchSolrClient[IO]]]("search-solr"):

def apply(): Resource[IO, SearchSolrClient[IO]] =
withSolrClient()
.evalTap(SchemaMigrator[IO](_).migrate(Migrations.all))
SolrClient[IO](solrConfig.copy(core = server.searchCoreName))
.evalTap(SchemaMigrator[IO](_).migrate(Migrations.all).attempt.void)
.map(new SearchSolrClientImpl[IO](_))

override def beforeAll(): Unit =
withSolrClient.beforeAll()
server.start()

override def afterAll(): Unit =
withSolrClient.afterAll()
server.stop()

override def munitFixtures: Seq[Fixture[_]] =
List(withSearchSolrClient)
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ class SolrClientSpec extends CatsEffectSuite with SolrSpec with SolrTruncate:
)
withSolrClient().use { client =>
for {
_ <- truncateAll(client)(
Seq(
FieldName("roomName"),
FieldName("roomDescription"),
FieldName("roomSeats")
),
Seq(TypeName("roomText"), TypeName("roomInt"))
)
_ <- client.modifySchema(cmds)
_ <- client
.insert[Room](Seq(Room("meeting room", "room for meetings", 56)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SolrMigratorSpec extends CatsEffectSuite with SolrSpec with SolrTruncate:
SchemaMigration(-1, Add(Field(FieldName("testSeats"), TypeName("testInt"))))
)

def truncate(client: SolrClient[IO]): IO[Unit] =
private def truncate(client: SolrClient[IO]): IO[Unit] =
truncateAll(client)(
Seq(
FieldName("currentSchemaVersion"),
Expand All @@ -57,7 +57,7 @@ class SolrMigratorSpec extends CatsEffectSuite with SolrSpec with SolrTruncate:
} yield ()
}

test("run only remaining migrations".ignore):
test("run migrations"):
withSolrClient().use { client =>
val migrator = SchemaMigrator(client)
val first = migrations.take(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ class SolrServer(module: String, port: Int) {

private val containerName = s"$module-test-solr"
private val image = "solr:9.4.1-slim"
val coreName = "renku-search-test"
val genericCoreName = "core-test"
val searchCoreName = "search-core-test"
private val cores = Set(genericCoreName, searchCoreName)
private val startCmd = s"""|docker run --rm
|--name $containerName
|-p $port:8983
|-d $image""".stripMargin
private val isRunningCmd = s"docker container ls --filter 'name=$containerName'"
private val stopCmd = s"docker stop -t5 $containerName"
private val readyCmd =
s"curl http://localhost:8983/solr/$coreName/select?q=*:* --no-progress-meter --fail 1> /dev/null"
private val isReadyCmd = s"docker exec $containerName sh -c '$readyCmd'"
private val createCore = s"precreate-core $coreName"
private val createCoreCmd = s"docker exec $containerName sh -c '$createCore'"
private def readyCmd(core: String) =
s"curl http://localhost:8983/solr/$core/select?q=*:* --no-progress-meter --fail 1> /dev/null"
private def isReadyCmd(core: String) =
s"docker exec $containerName sh -c '${readyCmd(core)}'"
private def createCore(core: String) = s"precreate-core $core"
private def createCoreCmd(core: String) =
s"docker exec $containerName sh -c '${createCore(core)}'"
private val wasRunning = new AtomicBoolean(false)

def start(): Unit = synchronized {
Expand All @@ -57,19 +61,26 @@ class SolrServer(module: String, port: Int) {
else {
println(s"Starting Solr container for '$module' from '$image' image")
startContainer()
var rc = 1
while (rc != 0) {
Thread.sleep(500)
rc = isReadyCmd.!
if (rc == 0) println(s"Solr container for '$module' started on port $port")
}
waitForCoresToBeReady()
}
}

private def waitForCoresToBeReady(): Unit =
var rc = 1
while (rc != 0) {
Thread.sleep(500)
rc = checkCoresReady
if (rc == 0) println(s"Solr container for '$module' ready on port $port")
}

private def checkCoresReady =
cores.foldLeft(0)((rc, core) => if (rc == 0) isReadyCmd(core).! else rc)

private def checkRunning: Boolean = {
val out = isRunningCmd.lazyLines.toList
val isRunning = out.exists(_ contains containerName)
wasRunning.set(isRunning)
if (isRunning) waitForCoresToBeReady()
isRunning
}

Expand All @@ -80,8 +91,10 @@ class SolrServer(module: String, port: Int) {
case ex => throw ex
}
Try(startCmd.!!).fold(retryOnContainerFailedToRun, _ => ())
val rc = createCoreCmd.!
println(s"Created solr core $coreName ($rc)")
val rcs = cores.map(c => c -> createCoreCmd(c).!)
println(
s"Created solr cores: ${rcs.map { case (core, rc) => s"'$core' ($rc)" }.mkString(", ")}"
)
}

def stop(): Unit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ import scala.concurrent.duration.Duration
trait SolrSpec:
self: munit.Suite =>

private lazy val server: SolrServer = SolrServer
protected lazy val server: SolrServer = SolrServer
protected lazy val solrConfig: SolrConfig = SolrConfig(
server.url / "solr",
server.genericCoreName,
commitWithin = Some(Duration.Zero),
logMessageBodies = true
)

val withSolrClient: Fixture[Resource[IO, SolrClient[IO]]] =
new Fixture[Resource[IO, SolrClient[IO]]]("solr"):

def apply(): Resource[IO, SolrClient[IO]] =
SolrClient[IO](
SolrConfig(server.url / "solr", server.coreName, Some(Duration.Zero), true)
)
SolrClient[IO](solrConfig)

override def beforeAll(): Unit =
server.start()
Expand Down

0 comments on commit 6661d53

Please sign in to comment.