Skip to content

Commit 56076f6

Browse files
committed
Removing concurrent tests to make SFL4J happy
1 parent 2d5c7bc commit 56076f6

File tree

3 files changed

+131
-62
lines changed

3 files changed

+131
-62
lines changed

build.sbt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
scalaVersion := "2.10.3"
1+
scalaVersion := "2.10.3"
2+
3+
parallelExecution in ThisBuild := false

mysql-async/src/test/scala/com/github/mauricio/async/db/mysql/TransactionSpec.scala

+41-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package com.github.mauricio.async.db.mysql
33
import org.specs2.mutable.Specification
44
import com.github.mauricio.async.db.util.ExecutorServiceUtils._
55
import com.github.mauricio.async.db.util.FutureUtils.awaitFuture
6+
import com.github.mauricio.async.db.mysql.exceptions.MySQLException
7+
import com.github.mauricio.async.db.Connection
68

79
class TransactionSpec extends Specification with ConnectionHelper {
810

11+
val brokenInsert = """INSERT INTO users (id, name) VALUES (1, 'Maurício Aragão')"""
912
val insertUser = """INSERT INTO users (name) VALUES (?)"""
1013

1114
"connection in transaction" should {
@@ -36,9 +39,6 @@ class TransactionSpec extends Specification with ConnectionHelper {
3639
withConnection {
3740
connection =>
3841
executeQuery(connection, this.createTable)
39-
40-
val brokenInsert = """INSERT INTO users (id, name) VALUES (1, 'Maurício Aragão')"""
41-
4242
executeQuery(connection, this.insert)
4343

4444
val future = connection.inTransaction {
@@ -50,7 +50,11 @@ class TransactionSpec extends Specification with ConnectionHelper {
5050
awaitFuture(future)
5151
ko("Should not have arrived here")
5252
} catch {
53-
case e : Exception => {
53+
case e : MySQLException => {
54+
55+
e.errorMessage.errorCode === 1062
56+
e.errorMessage.errorMessage === "Duplicate entry '1' for key 'PRIMARY'"
57+
5458
val result = executePreparedStatement(connection, this.select).rows.get
5559
result.size === 1
5660
result(0)("name") === "Maurício Aragão"
@@ -61,6 +65,39 @@ class TransactionSpec extends Specification with ConnectionHelper {
6165

6266
}
6367

68+
"should make a connection invalid and not return it to the pool if it raises an exception" in {
69+
70+
withPool {
71+
pool =>
72+
73+
executeQuery(pool, this.createTable)
74+
executeQuery(pool, this.insert)
75+
76+
var connection : Connection = null
77+
78+
val future = pool.inTransaction {
79+
c =>
80+
connection = c
81+
c.sendQuery(this.brokenInsert)
82+
}
83+
84+
try {
85+
awaitFuture(future)
86+
ko("this should not be reached")
87+
} catch {
88+
case e : MySQLException => {
89+
90+
pool.availables must have size(0)
91+
pool.availables must not contain(connection.asInstanceOf[MySQLConnection])
92+
93+
ok("success")
94+
}
95+
}
96+
97+
}
98+
99+
}
100+
64101
}
65102

66103
}

postgresql-async/src/test/scala/com/github/mauricio/async/db/postgresql/TransactionSpec.scala

+87-57
Original file line numberDiff line numberDiff line change
@@ -2,100 +2,130 @@ package com.github.mauricio.async.db.postgresql
22

33
import org.specs2.mutable.Specification
44
import com.github.mauricio.async.db.util.Log
5-
import com.github.mauricio.async.db.exceptions.DatabaseException
65
import scala.concurrent.ExecutionContext.Implicits.global
7-
import scala.util.control.Exception.catching
6+
import com.github.mauricio.async.db.postgresql.exceptions.GenericDatabaseException
87

98
class TransactionSpec extends Specification with DatabaseTestHelper {
109

1110
val log = Log.get[TransactionSpec]
1211

1312
val tableCreate = "CREATE TEMP TABLE transaction_test (x integer PRIMARY KEY)"
14-
def tableInsert(x : Int) = "INSERT INTO transaction_test VALUES (" + x.toString + ")"
13+
14+
def tableInsert(x: Int) = "INSERT INTO transaction_test VALUES (" + x.toString + ")"
15+
1516
val tableSelect = "SELECT x FROM transaction_test ORDER BY x"
1617

1718
"transactions" should {
1819

1920
"commit simple inserts" in {
20-
withHandler { handler =>
21-
executeDdl(handler, tableCreate)
22-
await(handler.inTransaction { conn =>
23-
conn.sendQuery(tableInsert(1)).flatMap { _ =>
24-
conn.sendQuery(tableInsert(2))
25-
}
26-
})
21+
withHandler {
22+
handler =>
23+
executeDdl(handler, tableCreate)
24+
await(handler.inTransaction {
25+
conn =>
26+
conn.sendQuery(tableInsert(1)).flatMap {
27+
_ =>
28+
conn.sendQuery(tableInsert(2))
29+
}
30+
})
2731

28-
val rows = executeQuery(handler, tableSelect).rows.get
29-
rows.length === 2
30-
rows(0)(0) === 1
31-
rows(1)(0) === 2
32+
val rows = executeQuery(handler, tableSelect).rows.get
33+
rows.length === 2
34+
rows(0)(0) === 1
35+
rows(1)(0) === 2
3236
}
3337
}
3438

3539
"commit simple inserts with prepared statements" in {
36-
withHandler { handler =>
37-
executeDdl(handler, tableCreate)
38-
await(handler.inTransaction { conn =>
39-
conn.sendPreparedStatement(tableInsert(1)).flatMap { _ =>
40-
conn.sendPreparedStatement(tableInsert(2))
41-
}
42-
})
40+
withHandler {
41+
handler =>
42+
executeDdl(handler, tableCreate)
43+
await(handler.inTransaction {
44+
conn =>
45+
conn.sendPreparedStatement(tableInsert(1)).flatMap {
46+
_ =>
47+
conn.sendPreparedStatement(tableInsert(2))
48+
}
49+
})
4350

44-
val rows = executePreparedStatement(handler, tableSelect).rows.get
45-
rows.length === 2
46-
rows(0)(0) === 1
47-
rows(1)(0) === 2
51+
val rows = executePreparedStatement(handler, tableSelect).rows.get
52+
rows.length === 2
53+
rows(0)(0) === 1
54+
rows(1)(0) === 2
4855
}
4956
}
5057

5158
"rollback on error" in {
52-
withHandler { handler =>
53-
executeDdl(handler, tableCreate)
54-
catching(classOf[DatabaseException]).opt(
55-
await(handler.inTransaction { conn =>
56-
conn.sendQuery(tableInsert(1)).flatMap { _ =>
57-
conn.sendQuery(tableInsert(1))
59+
withHandler {
60+
handler =>
61+
executeDdl(handler, tableCreate)
62+
63+
try {
64+
await(handler.inTransaction {
65+
conn =>
66+
conn.sendQuery(tableInsert(1)).flatMap {
67+
_ =>
68+
conn.sendQuery(tableInsert(1))
69+
}
70+
})
71+
failure("Should not have come here")
72+
} catch {
73+
case e: GenericDatabaseException => {
74+
e.errorMessage.message === "duplicate key value violates unique constraint \"transaction_test_pkey\""
5875
}
59-
})
60-
) === None
76+
}
6177

62-
val rows = executeQuery(handler, tableSelect).rows.get
63-
rows.length === 0
78+
val rows = executeQuery(handler, tableSelect).rows.get
79+
rows.length === 0
6480
}
6581

82+
}
83+
84+
"do not reuse connection in pool if the transaction failed" in {
85+
86+
87+
6688
}
6789

6890
"rollback explicitly" in {
69-
withHandler { handler =>
70-
executeDdl(handler, tableCreate)
71-
await(handler.inTransaction { conn =>
72-
conn.sendQuery(tableInsert(1)).flatMap { _ =>
73-
conn.sendQuery("ROLLBACK")
74-
}
75-
})
91+
withHandler {
92+
handler =>
93+
executeDdl(handler, tableCreate)
94+
await(handler.inTransaction {
95+
conn =>
96+
conn.sendQuery(tableInsert(1)).flatMap {
97+
_ =>
98+
conn.sendQuery("ROLLBACK")
99+
}
100+
})
76101

77-
val rows = executeQuery(handler, tableSelect).rows.get
78-
rows.length === 0
102+
val rows = executeQuery(handler, tableSelect).rows.get
103+
rows.length === 0
79104
}
80105

81106
}
82107

83108
"rollback to savepoint" in {
84-
withHandler { handler =>
85-
executeDdl(handler, tableCreate)
86-
await(handler.inTransaction { conn =>
87-
conn.sendQuery(tableInsert(1)).flatMap { _ =>
88-
conn.sendQuery("SAVEPOINT one").flatMap { _ =>
89-
conn.sendQuery(tableInsert(2)).flatMap { _ =>
90-
conn.sendQuery("ROLLBACK TO SAVEPOINT one")
109+
withHandler {
110+
handler =>
111+
executeDdl(handler, tableCreate)
112+
await(handler.inTransaction {
113+
conn =>
114+
conn.sendQuery(tableInsert(1)).flatMap {
115+
_ =>
116+
conn.sendQuery("SAVEPOINT one").flatMap {
117+
_ =>
118+
conn.sendQuery(tableInsert(2)).flatMap {
119+
_ =>
120+
conn.sendQuery("ROLLBACK TO SAVEPOINT one")
121+
}
122+
}
91123
}
92-
}
93-
}
94-
})
124+
})
95125

96-
val rows = executeQuery(handler, tableSelect).rows.get
97-
rows.length === 1
98-
rows(0)(0) === 1
126+
val rows = executeQuery(handler, tableSelect).rows.get
127+
rows.length === 1
128+
rows(0)(0) === 1
99129
}
100130

101131
}

0 commit comments

Comments
 (0)