Skip to content

Commit 29c2362

Browse files
committed
Include transaction specs for both MySQL and PostgreSQL
1 parent 55a7ba8 commit 29c2362

File tree

5 files changed

+87
-6
lines changed

5 files changed

+87
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
databases/*
12
out/*
23
generate_bundles.rb
34
.cache

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
postgresql: postgres -D /Users/mauricio/databases/postgresql
1+
postgresql: postgres -D databases/postgresql
22
mysql: mysqld --log-warnings --console

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
package com.github.mauricio.async.db.mysql
22

3+
import java.util.UUID
4+
import java.util.concurrent.TimeUnit
5+
36
import org.specs2.mutable.Specification
47
import com.github.mauricio.async.db.util.FutureUtils.awaitFuture
58
import com.github.mauricio.async.db.mysql.exceptions.MySQLException
69
import com.github.mauricio.async.db.Connection
710

11+
import scala.concurrent.duration.Duration
12+
import scala.concurrent.{Await, Future}
13+
import scala.util.{Success, Failure}
14+
15+
object TransactionSpec {
16+
17+
val BrokenInsert = """INSERT INTO users (id, name) VALUES (1, 'Maurício Aragão')"""
18+
val InsertUser = """INSERT INTO users (name) VALUES (?)"""
19+
val TransactionInsert = "insert into transaction_test (id) values (?)"
20+
21+
}
22+
823
class TransactionSpec extends Specification with ConnectionHelper {
924

10-
val brokenInsert = """INSERT INTO users (id, name) VALUES (1, 'Maurício Aragão')"""
11-
val insertUser = """INSERT INTO users (name) VALUES (?)"""
25+
import TransactionSpec._
1226

1327
"connection in transaction" should {
1428

@@ -42,7 +56,7 @@ class TransactionSpec extends Specification with ConnectionHelper {
4256

4357
val future = connection.inTransaction {
4458
c =>
45-
c.sendQuery(this.insert).flatMap(r => c.sendQuery(brokenInsert))
59+
c.sendQuery(this.insert).flatMap(r => c.sendQuery(BrokenInsert))
4660
}
4761

4862
try {
@@ -77,7 +91,7 @@ class TransactionSpec extends Specification with ConnectionHelper {
7791
val future = pool.inTransaction {
7892
c =>
7993
connection = c
80-
c.sendQuery(this.brokenInsert)
94+
c.sendQuery(BrokenInsert)
8195
}
8296

8397
try {
@@ -97,6 +111,38 @@ class TransactionSpec extends Specification with ConnectionHelper {
97111

98112
}
99113

114+
"runs commands for a transaction in a single connection" in {
115+
116+
val id = UUID.randomUUID().toString
117+
118+
withPool {
119+
pool =>
120+
val operations = pool.inTransaction {
121+
connection =>
122+
connection.sendPreparedStatement(TransactionInsert, List(id)).flatMap {
123+
result =>
124+
connection.sendPreparedStatement(TransactionInsert, List(id)).map {
125+
failure =>
126+
List(result, failure)
127+
}
128+
}
129+
}
130+
131+
Await.ready(operations, Duration(5, TimeUnit.SECONDS))
132+
133+
operations.value.get match {
134+
case Success(e) => failure("should not have executed")
135+
case Failure(e) => {
136+
e.asInstanceOf[MySQLException].errorMessage.errorCode === 1062
137+
executePreparedStatement(pool, "select * from transaction_test where id = ?", id).rows.get.size === 0
138+
success("ok")
139+
}
140+
}
141+
142+
}
143+
144+
}
145+
100146
}
101147

102148
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@
1616

1717
package com.github.mauricio.async.db.postgresql.pool
1818

19+
import java.util.UUID
20+
1921
import com.github.mauricio.async.db.pool.{ConnectionPool, PoolConfiguration}
2022
import com.github.mauricio.async.db.postgresql.{PostgreSQLConnection, DatabaseTestHelper}
2123
import org.specs2.mutable.Specification
2224

25+
object ConnectionPoolSpec {
26+
val Insert = "insert into transaction_test (id) values (?)"
27+
}
28+
2329
class ConnectionPoolSpec extends Specification with DatabaseTestHelper {
2430

31+
import ConnectionPoolSpec.Insert
32+
2533
"pool" should {
2634

2735
"give you a connection when sending statements" in {
@@ -51,6 +59,30 @@ class ConnectionPoolSpec extends Specification with DatabaseTestHelper {
5159
}
5260
}
5361

62+
"runs commands for a transaction in a single connection" in {
63+
64+
val id = UUID.randomUUID().toString
65+
66+
withPool {
67+
pool =>
68+
val operations = pool.inTransaction {
69+
connection =>
70+
connection.sendPreparedStatement(Insert, List(id)).flatMap {
71+
result =>
72+
connection.sendPreparedStatement(Insert, List(id)).map {
73+
failure =>
74+
List(result, failure)
75+
}
76+
}
77+
}
78+
79+
val resultSets = await(operations)
80+
81+
resultSets.size mustEqual(2)
82+
}
83+
84+
}
85+
5486
}
5587

5688
def withPool[R]( fn : (ConnectionPool[PostgreSQLConnection]) => R ) : R = {

script/prepare_build.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
echo "Preparing MySQL configs"
44
mysql -u root -e 'create database mysql_async_tests;'
5+
mysql -u root -e "create table mysql_async_tests.transaction_test (id varchar(255) not null, primary key (id))"
56
mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'mysql_async'@'localhost' IDENTIFIED BY 'root' WITH GRANT OPTION";
67
mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'mysql_async_old'@'localhost' WITH GRANT OPTION";
78
mysql -u root -e "UPDATE mysql.user SET Password = OLD_PASSWORD('do_not_use_this'), plugin = 'mysql_old_password' where User = 'mysql_async_old'; flush privileges;";
@@ -12,10 +13,11 @@ echo "preparing postgresql configs"
1213
psql -c 'create database netty_driver_test;' -U postgres
1314
psql -c 'create database netty_driver_time_test;' -U postgres
1415
psql -c "alter database netty_driver_time_test set timezone to 'GMT'" -U postgres
16+
psql -c "create table transaction_test ( id varchar(255) not null, constraint id_unique primary key (id))" -U postgres netty_driver_test
1517
psql -c "CREATE USER postgres_md5 WITH PASSWORD 'postgres_md5'; GRANT ALL PRIVILEGES ON DATABASE netty_driver_test to postgres_md5;" -U postgres
1618
psql -c "CREATE USER postgres_cleartext WITH PASSWORD 'postgres_cleartext'; GRANT ALL PRIVILEGES ON DATABASE netty_driver_test to postgres_cleartext;" -U postgres
1719
psql -c "CREATE USER postgres_kerberos WITH PASSWORD 'postgres_kerberos'; GRANT ALL PRIVILEGES ON DATABASE netty_driver_test to postgres_kerberos;" -U postgres
18-
psql -d "netty_driver_test" -c "CREATE TYPE example_mood AS ENUM ('sad', 'ok', 'happy');"
20+
psql -d "netty_driver_test" -c "CREATE TYPE example_mood AS ENUM ('sad', 'ok', 'happy');" -U postgres
1921

2022
sudo chmod 777 /etc/postgresql/9.1/main/pg_hba.conf
2123

0 commit comments

Comments
 (0)