8000 Add DatabaseRule to tests that were missing it to allow working with … · qza/postgres-async-driver@a582612 · GitHub
[go: up one dir, main page]

Skip to content

Commit a582612

Browse files
author
Mikko Tiihonen
committed
Add DatabaseRule to tests that were missing it to allow working with embedded postgresql
1 parent 59dba19 commit a582612

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

src/test/java/com/github/pgasync/impl/AuthenticationTest.java

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

33
import com.github.pgasync.ConnectionPool;
44
import com.github.pgasync.SqlException;
5+
import org.junit.ClassRule;
56
import org.junit.Test;
67

78
import static com.github.pgasync.impl.DatabaseRule.createPoolBuilder;
@@ -10,9 +11,12 @@
1011

1112
public class AuthenticationTest {
1213

14+
@ClassRule
15+
public static DatabaseRule dbr = new DatabaseRule(createPoolBuilder(1));
16+
1317
@Test
1418
public void shouldThrowExceptionOnInvalidCredentials() throws Exception {
15-
try (ConnectionPool pool = createPoolBuilder(1).password("_invalid_").build()) {
19+
try (ConnectionPool pool = dbr.builder.password("_invalid_").build()) {
1620
pool.queryRows("SELECT 1").toBlocking().first();
1721
fail();
1822
} catch (SqlException sqle) {

src/test/java/com/github/pgasync/impl/DatabaseRule.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.pgasync.impl;
22

33
import static java.lang.System.getenv;
4+
import static java.lang.System.out;
45
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_5_0;
56

67
import com.github.pgasync.ConnectionPool;
@@ -51,6 +52,8 @@ class DatabaseRule extends ExternalResource {
5152
new AbstractPostgresConfig.Credentials("async-pg", "async-pg"));
5253
PostgresExecutable exec = runtime.prepare(config);
5354
process = exec.start();
55+
56+
out.printf("Started postgres to %s:%d%n", process.getConfig().net().host(), process.getConfig().net().port());
5457
}
5558
catch (IOException e)
5659
{
@@ -123,7 +126,6 @@ static class EmbeddedConnectionPoolBuilder extends ConnectionPoolBuilder {
123126
database("async-pg");
124127
username("async-pg");
125128
password("async-pg");
126-
port(2345);
127129
}
128130
}
129131

src/test/java/com/github/pgasync/impl/ListenNotifyTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.pgasync.impl;
22

33
import com.github.pgasync.ConnectionPool;
4-
import org.junit.AfterClass;
4+
import org.junit.ClassRule;
55
import org.junit.Test;
66
import rx.Subscription;
77

@@ -16,10 +16,12 @@
1616
*/
1717
public class ListenNotifyTest {
1818

19-
static final ConnectionPool pool = DatabaseRule.createPool(5);
19+
@ClassRule
20+
public static DatabaseRule dbr = new DatabaseRule(DatabaseRule.createPoolBuilder(5));
2021

2122
@Test
2223
public void shouldReceiveNotificationsOnListenedChannel() throws Exception {
24+
ConnectionPool pool = dbr.pool;
2325
BlockingQueue<String> result = new LinkedBlockingQueue<>(5);
2426

2527
Subscription subscription = pool.listen("example").subscribe(result::add, Throwable::printStackTrace);
@@ -39,9 +41,4 @@ public void shouldReceiveNotificationsOnListenedChannel() throws Exception {
3941
pool.querySet("notify example, 'msg'").toBlocking().single();
4042
assertNull(result.poll(2, TimeUnit.SECONDS));
4143
}
42-
43-
@AfterClass
44-
public static void close() throws Exception {
45-
pool.close();
46-
}
4744
}

src/test/java/com/github/pgasync/impl/PipelineTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.github.pgasync.ConnectionPool;
1919
import com.github.pgasync.ResultSet;
2020
import org.junit.After;
21+
import org.junit.ClassRule;
2122
import org.junit.Ignore;
2223
import org.junit.Test;
2324

@@ -42,6 +43,9 @@
4243
* @author Mikko Tiihonen
4344
*/
4445
public class PipelineTest {
46+
@ClassRule
47+
public static DatabaseRule dbr = new DatabaseRule();
48+
4549
final Consumer<Throwable> err = t -> { throw new AssertionError("failed", t); };
4650

4751
Connection c;
@@ -59,7 +63,7 @@ public void closeConnection() throws Exception {
5963

6064
@Test
6165
public void connectionPipelinesQueries() throws InterruptedException {
62-
pool = createPoolBuilder(1).pipeline(true).build();
66+
pool = dbr.builder.pipeline(true).build();
6367

6468
int count = 5;
6569
double sleep = 0.5;
@@ -80,7 +84,7 @@ public void connectionPipelinesQueries() throws InterruptedException {
8084
}
8185

8286
private Connection getConnection(boolean pipeline) throws InterruptedException {
83-
pool = createPoolBuilder(1).pipeline(pipeline).build();
87+
pool = dbr.builder.pipeline(pipeline).build();
8488
SynchronousQueue<Connection> connQueue = new SynchronousQueue<>();
8589
pool.getConnection().subscribe(connQueue::add);
8690
return c = connQueue.take();
@@ -123,7 +127,7 @@ public void connectionPoolPipelinesQueries() throws InterruptedException {
123127

124128
@Test
125129
public void connectionPoolPipelinesQueriesWithinTransaction() throws InterruptedException {
126-
pool = createPoolBuilder(1).pipeline(true).build();
130+
pool = dbr.builder.pipeline(true).build();
127131

128132
int count = 5;
129133
double sleep = 0.5;

0 commit comments

Comments
 (0)
0