8000 Merge pull request #29 from alaisi/embedded_test_postgres · juliombb/postgres-async-driver@59dba19 · GitHub
[go: up one dir, main page]

Skip to content

Commit 59dba19

Browse files
authored
Merge pull request alaisi#29 from alaisi/embedded_test_postgres
Run tests using embedded postgresql
2 parents 4423c9d + a04f2f5 commit 59dba19

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

pom.xml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<plugin>
4949
<groupId>org.apache.maven.plugins</groupId>
5050
<artifactId>maven-compiler-plugin</artifactId>
51-
<version>3.3</version>
51+
<version>3.5.1</version>
5252
<configuration>
5353
<source>1.8</source>
5454
<target>1.8</target>
@@ -57,7 +57,7 @@
5757
<plugin>
5858
<groupId>org.codehaus.mojo</groupId>
5959
<artifactId>findbugs-maven-plugin</artifactId>
60-
<version>3.0.1</version>
60+
<version>3.0.4</version>
6161
<configuration>
6262
<findbugsXmlOutput>true</findbugsXmlOutput>
6363
<effort>Max</effort>
@@ -71,17 +71,17 @@
7171
<dependency>
7272
<groupId>io.netty</groupId>
7373
<artifactId>netty-handler</artifactId>
74-
<version>4.1.1.Final</version>
74+
<version>4.1.5.Final</version>
7575
</dependency>
7676
<dependency>
7777
<groupId>io.reactivex</groupId>
7878
<artifactId>rxjava</artifactId>
79-
<version>1.1.5</version>
79+
<version>1.1.10</version>
8080
</dependency>
8181
<dependency>
8282
<groupId>com.google.code.findbugs</groupId>
8383
<artifactId>annotations</artifactId>
84-
<version>3.0.0</version>
84+
<version>3.0.1u2</version>
8585
<scope>provided</scope>
8686
</dependency>
8787

@@ -91,6 +91,11 @@
9191
<version>4.12</version>
9292
<scope>test</scope>
9393
</dependency>
94+
<dependency>
95+
<groupId>ru.yandex.qatools.embed</groupId>
96+
<artifactId>postgresql-embedded</artifactId>
97+
<version>1.15</version>
98+
</dependency>
9499
</dependencies>
95100

96101
</project>

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

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

3+
import static java.lang.System.getenv;
4+
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_5_0;
5+
36
import com.github.pgasync.ConnectionPool;
47
import com.github.pgasync.ConnectionPoolBuilder;
58
import com.github.pgasync.Db;
69
import com.github.pgasync.ResultSet;
710
import org.junit.rules.ExternalResource;
811
import rx.Observable;
912

13+
import java.io.IOException;
1014
import java.util.AbstractMap.SimpleImmutableEntry;
1115
import java.util.List;
1216
import java.util.Map.Entry;
1317
import java.util.concurrent.ArrayBlockingQueue;
1418
import java.util.concurrent.BlockingQueue;
1519
import java.util.concurrent.TimeUnit;
1620

21+
import ru.yandex.qatools.embed.postgresql.PostgresExecutable;
22+
import ru.yandex.qatools.embed.postgresql.PostgresProcess;
23+
import ru.yandex.qatools.embed.postgresql.PostgresStarter;
24+
import ru.yandex.qatools.embed.postgresql.config.AbstractPostgresConfig;
25+
import ru.yandex.qatools.embed.postgresql.config.PostgresConfig;
26+
1727
/**
1828
* @author Antti Laisi
1929
*/
2030
class DatabaseRule extends ExternalResource {
2131

2232
final ConnectionPoolBuilder builder;
33+
static PostgresProcess process;
2334
ConnectionPool pool;
2435

2536
DatabaseRule() {
@@ -28,6 +39,28 @@ class DatabaseRule extends ExternalResource {
2839

2940
DatabaseRule(ConnectionPoolBuilder builder) {
3041
this.builder = builder;
42+
if (builder instanceof EmbeddedConnectionPoolBuilder)
43+
{
44+
if (process == null)
45+
{
46+
try
47+
{
48+
PostgresStarter<PostgresExecutable, PostgresProcess> runtime = PostgresStarter.getDefaultInstance();
49+
PostgresConfig config = new PostgresConfig(V9_5_0, new AbstractPostgresConfig.Net(),
50+
new AbstractPostgresConfig.Storage("async-pg"), new AbstractPostgresConfig.Timeout(),
51+
new AbstractPostgresConfig.Credentials("async-pg", "async-pg"));
52+
PostgresExecutable exec = runtime.prepare(config);
53+
process = exec.start();
54+
}
55+
catch (IOException e)
56+
{
57+
throw new RuntimeException(e);
58+
}
59+
}
60+
61+
builder.hostname(process.getConfig().net().host());
62+
builder.port(process.getConfig().net().port());
63+
}
3164
}
3265

3366
@Override
@@ -85,11 +118,27 @@ Db db() {
85118
return pool;
86119
}
87120

121+
static class EmbeddedConnectionPoolBuilder extends ConnectionPoolBuilder {
122+
EmbeddedConnectionPoolBuilder() {
123+
database("async-pg");
124+
username("async-pg");
125+
password("async-pg");
126+
port(2345);
127+
}
128+
}
129+
88130
static ConnectionPool createPool(int size) {
89131
return createPoolBuilder(size).build();
90132
}
91133
static ConnectionPoolBuilder createPoolBuilder(int size) {
92-
return new ConnectionPoolBuilder()
134+
String db = getenv("PG_DATABASE");
135+
String user = getenv("PG_USERNAME");
136+
String pass = getenv("PG_PASSWORD");
137+
138+
if (db == null && user == null && pass == null) {
139+
return new EmbeddedConnectionPoolBuilder().poolSize(size);
140+
}
141+
return new EmbeddedConnectionPoolBuilder()
93142
.database(envOrDefault("PG_DATABASE", "postgres"))
94143
.username(envOrDefault("PG_USERNAME", "postgres"))
95144
.password(envOrDefault("PG_PASSWORD", "postgres"))
@@ -99,7 +148,7 @@ static ConnectionPoolBuilder createPoolBuilder(int size) {
99148

100149

101150
static String envOrDefault(String var, String def) {
102-
String value = System.getenv(var);
151+
String value = getenv(var);
103152
return value != null ? value : def;
104153
}
105154
}

0 commit comments

Comments
 (0)
0