forked from zio/zio-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionPoolSpec.scala
62 lines (53 loc) · 2.16 KB
/
ConnectionPoolSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package zio.sql
import zio.{ durationInt, Promise, ZIO, ZLayer }
import zio.test.TestAspect.{ sequential, timeout, withLiveClock }
import zio.test.{ TestEnvironment, _ }
import java.util.Properties
object ConnectionPoolSpec extends ZIOSpecDefault {
val poolSize = 10
private def connProperties(user: String, password: String): Properties = {
val props = new Properties
props.setProperty("user", user)
props.setProperty("password", password)
props
}
val poolConfigLayer: ZLayer[Any, Throwable, ConnectionPoolConfig] =
ZLayer.scoped {
TestContainer
.postgres()
.map(a =>
ConnectionPoolConfig(
url = a.jdbcUrl,
properties = connProperties(a.username, a.password),
poolSize = poolSize
)
)
}
override def spec: Spec[TestEnvironment, Any] =
specLayered.provideCustomShared((poolConfigLayer >>> ConnectionPool.live).orDie)
def specLayered: Spec[TestEnvironment with ConnectionPool, Object] =
suite("Postgres module")(
test("Fibers waiting for connections can be interrupted") {
// We need to actually sleep here to make sure that the started fibers
// had started acquiring connections.
for {
cp <- ZIO.service[ConnectionPool]
promise <- Promise.make[Nothing, Unit]
_ <- ZIO.replicateZIO(poolSize)(ZIO.scoped(cp.connection *> promise.await).fork)
_ <- ZIO.sleep(1.second)
waiting <- ZIO.replicateZIO(poolSize)(ZIO.scoped(cp.connection).fork)
_ <- ZIO.sleep(1.second)
_ <- ZIO.foreach(waiting)(_.interrupt)
_ <- promise.complete(ZIO.unit)
_ <- ZIO.scoped(cp.connection)
} yield assert("")(Assertion.anything)
} @@ timeout(10.seconds) @@ withLiveClock +
test("Invalid or closed fibers should be reacquired") {
for {
cp <- ZIO.service[ConnectionPool]
_ <- ZIO.replicateZIO(poolSize)(ZIO.scoped(cp.connection.map(_.close)))
conn <- ZIO.scoped(cp.connection)
} yield assert(conn.isValid(10))(Assertion.isTrue)
}
) @@ sequential
}