Skip to content

refactor(tests): share test containers across parallel JVM forks#979

Open
jnatten wants to merge 4 commits into
masterfrom
refactor/test-container-sharing
Open

refactor(tests): share test containers across parallel JVM forks#979
jnatten wants to merge 4 commits into
masterfrom
refactor/test-container-sharing

Conversation

@jnatten
Copy link
Copy Markdown
Contributor

@jnatten jnatten commented May 28, 2026

Stacker på denne #977

Gir det et nytt forsøk på å kjøre delte containere under testing.
Dette gjør at vi bruker drastisk mye mindre minne under kjøring av tester vha at vi kjører kun en elasticsearch container og en kun en postgres container etc.

Krever bittelitt mer tunga rett i munnen når man skriver tester pga man kan i teorien lage kolliderende tester.
Denne fikser så vi appender pid'en (siden jvmen blir forka) til schema og index navn. Men jeg tror det lett er worth.

@jnatten jnatten requested a review from a team May 28, 2026 21:54
Copy link
Copy Markdown
Member

@gunnarvelle gunnarvelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Det ser ut som om du bruker standardporter 5432 og 9200. Vil det sei at det ikkje brukes randomporter som testene gjør i dag? Altså at det ikkje kan kjøre postgres og es lokal på maskina?

@jnatten
Copy link
Copy Markdown
Contributor Author

jnatten commented May 29, 2026

Det ser ut som om du bruker standardporter 5432 og 9200. Vil det sei at det ikkje brukes randomporter som testene gjør i dag? Altså at det ikkje kan kjøre postgres og es lokal på maskina?

Uhm nei? Hvor da tenker du på?
Jeg har kjørt lokale containere ved siden av når jeg har kjørt testene.

@jnatten jnatten requested a review from amatho June 1, 2026 06:02
Base automatically changed from fix/search-engine-container to master June 1, 2026 06:37
Introduce SharedContainer, a file-based coordination mechanism that
allows parallel jvm test forks to reuse a single Postgres, Elasticsearch,
and Redis container via cross-process refcounting and file locks.

- Add SharedContainer with acquire/release lifecycle, health checks,
  stale container cleanup, and shutdown hooks
- Refactor DatabaseIntegrationSuite, ElasticsearchIntegrationSuite, and
  RedisIntegrationSuite to use SharedContainer instead of spawning
  individual containers per test suite
- Append PID to search index names and database schema names to isolate
  parallel forks sharing the same container
- Make search index properties lazy so they pick up test environment
  overrides
@jnatten jnatten force-pushed the refactor/test-container-sharing branch 2 times, most recently from e6530f6 to c839c5e Compare June 1, 2026 09:25
Comment thread scalatestsuite/src/main/scala/no/ndla/scalatestsuite/SharedContainer.scala Outdated
This flag wasn't really in use because it was never false, and even if
it was the rest of the suite would throw so this changes that to keep
things simple.

The original idea was to maybe support running tests without docker
environment and keeping the suite green while cancelling the other
tests, but since that isn't really a good idea now we don't need this.
@jnatten jnatten force-pushed the refactor/test-container-sharing branch from c839c5e to 4d7ab83 Compare June 1, 2026 11:29
@jnatten jnatten force-pushed the refactor/test-container-sharing branch from 3cf954f to 81eaa19 Compare June 1, 2026 11:48
@jnatten jnatten force-pushed the refactor/test-container-sharing branch from 81eaa19 to dea2de3 Compare June 1, 2026 11:56
val EnablePostgresContainer: Boolean = true
val PostgresqlVersion: String = "17.5"
lazy val schemaName: String = "testschema"
lazy val schemaName: String = s"testschema_${ProcessHandle.current().pid()}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kunne gjort noe sånt som det her for å slippe å legge inn PID i alle subklasser. Ser f.eks. at ConfigRepositoryTest mangler PID greiene

Suggested change
lazy val schemaName: String = s"testschema_${ProcessHandle.current().pid()}"
lazy val schemaNamePrefix: String = "testschema"
private lazy val schemaName: String = s"${schemaNamePrefix}_${ProcessHandle.current().pid()}"

}
}
private def esImageName: DockerImageName = {
val imgName = env.getOrElse("SEARCH_ENGINE_IMAGE", ElasticsearchImage)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Har vi lyst til å fortsette å støtte dette egentlig?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tja, koster veldig lite å tåle dette spørru meg men er jo ikke så fryktelig farlig heller.
🤷

Comment thread scalatestsuite/src/main/scala/no/ndla/scalatestsuite/RedisIntegrationSuite.scala Outdated
Comment thread scalatestsuite/src/main/scala/no/ndla/scalatestsuite/SharedContainer.scala Outdated
private def stopContainer(containerId: String): Unit = {
Try {
val process = new ProcessBuilder("docker", "rm", "-f", containerId).redirectErrorStream(true).start()
process.waitFor()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bør vi ha timeout her?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tja, kanskje?
tanker om hvor lenge haha? et minutt bare sånn for sikkerhetsskyld type?

Files.writeString(path, lines.mkString("\n"), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING): Unit
}

private def readInfoFile(path: Path): Option[SharedContainerInfo] = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hadde det ikke vært lettere å bare bruke ObjectInputStream og ObjectOutputStream for å lese/skrive info greiene?

Copy link
Copy Markdown
Contributor Author

@jnatten jnatten Jun 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Etter å ha kløna litt så vurderer jeg sterkt den refaktoreringen jeg styra med på jobb i dag.
La også på litt bedre typing for den infoen og da endrer jeg på disse også.

Men det kan hende jeg er litt blind etter å ha jobbet med det. Hva tenker du?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Og jeg gjør fortsatt streng skriving da, men føler ikke de var særlig komplekse lenger. I alle fall ikke sammenlignet med denne branchen hehe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants