Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Convert IntegrationSpec to use an embedded PostgreSQL database. #186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,70 @@ lazy val publishSettings = Seq(
lazy val allSettings = baseSettings ++ buildSettings ++ publishSettings

lazy val shapelessRef = LocalProject("finagle-postgres-shapeless")
lazy val integrationTestRef = LocalProject("finagle-postgres-integration")

lazy val `finagle-postgres` = project
.in(file("."))
.settings(moduleName := "finagle-postgres")
.settings(allSettings)
.configs(IntegrationTest)
.aggregate(shapelessRef)
.aggregate(
shapelessRef,
integrationTestRef
)

lazy val `finagle-postgres-integration` = project
.settings(moduleName := "finagle-postgres-integration")
.settings(allSettings)
.configs(IntegrationTest)
.settings(
libraryDependencies ++= Seq(
"io.zonky.test" % "embedded-postgres" % "1.2.6" % "test"
)
)
.dependsOn(`finagle-postgres` % "test->test")
.aggregate(
test9,
test10,
test11
)

lazy val test9 = integrationTests("9.6.17")
lazy val test10 = integrationTests("10.11.0") // 10.12.0 fails to find libz for some reason
lazy val test11 = integrationTests("11.6.0")

def integrationTests(v: String) = {
val majorVersion = v.split('.') match {
case Array(major, _, _) => major
case _ => sys.error(s"unexpected version number. Expected major.minor.patch, got $v")
}
val id = s"finagle-postgres-test-$majorVersion"
Project(id = id, base = file(id))
.settings(baseSettings ++ buildSettings) // don't publish
.settings(
libraryDependencies ++= Seq(
// TODO
"io.zonky.test.postgres" % "embedded-postgres-binaries-darwin-amd64" % v % "test",
)
)
.settings(
parallelExecution in Test := false,
javaOptions in Test += "-Duser.timezone=UTC" // TODO: investigate and submit a test to demonstrate that timezone handling is broken.
)
.settings(
Test / sourceGenerators += Def.task {
val file = (Test / sourceManaged).value / "com" / "twitter" / "finagle" / "postgres" / "IntegrationSpec.scala"
IO.write(file,
s"""package com.twitter.finagle.postgres
|
|class IntegrationSpec extends BaseIntegrationSpec("$v")
|""".stripMargin)
Seq(file)
}.taskValue
)
.configs(IntegrationTest)
.dependsOn(integrationTestRef % "compile->compile;test->test")
}

lazy val `finagle-postgres-shapeless` = project
.settings(moduleName := "finagle-postgres-shapeless")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.twitter.finagle.postgres

import com.twitter.finagle.Postgres
import com.twitter.util.Try
import io.zonky.test.db.postgres.embedded.EmbeddedPostgres
import org.scalatest.BeforeAndAfterAll

abstract class EmbeddedPgSqlSpec extends Spec with BeforeAndAfterAll {

var embeddedPgSql: Option[EmbeddedPostgres] = None

final val TestDbUser = "postgres"
final val TestDbPassword: Option[String] = None
final val TestDbName = "finagle_postgres_test"

def configure(b: EmbeddedPostgres.Builder): EmbeddedPostgres.Builder = b

private[this] def using[C <: AutoCloseable, T](c: => C)(f: C => T): T =
Try(f(c)).ensure(c.close()).get

// NOTE: this replicates what .travis.yml does.
private[this] def prep(psql: EmbeddedPostgres): EmbeddedPostgres = {
using(psql.getPostgresDatabase.getConnection()) { conn =>
using(conn.createStatement()) { stmt =>
stmt.execute(s"CREATE DATABASE $TestDbName")
}
}
using(psql.getDatabase(TestDbUser, TestDbName).getConnection()) { conn =>
using(conn.createStatement()) { stmt =>
stmt.execute("CREATE EXTENSION IF NOT EXISTS hstore")
stmt.execute("CREATE EXTENSION IF NOT EXISTS citext")
}
}
psql
}

def getClient: PostgresClientImpl = embeddedPgSql match {
case None => sys.error("getClient invoked outside of test fragment")
case Some(pgsql) =>
val client = Postgres.Client()
.withCredentials(TestDbUser, TestDbPassword)
.database(TestDbName)
.withSessionPool.maxSize(1)
// TODO
// .conditionally(useSsl, c => sslHost.fold(c.withTransport.tls)(c.withTransport.tls(_)))
.newRichClient(s"localhost:${pgsql.getPort}")

while (!client.isAvailable) {}
client
}

override def beforeAll(): Unit = {
val builder =
EmbeddedPostgres.builder()
.setCleanDataDirectory(true)
.setErrorRedirector(ProcessBuilder.Redirect.INHERIT)
.setOutputRedirector(ProcessBuilder.Redirect.INHERIT)

embeddedPgSql = Some(prep(configure(builder).start()))
}

override def afterAll(): Unit = embeddedPgSql.foreach(_.close())

}
Loading