Search in sources :

Example 1 with Repeat

use of io.vertx.ext.unit.junit.Repeat in project vertx-sql-client by eclipse-vertx.

the class MSSQLQueriesTest method testQueryCurrentTimestamp.

@Test
@Repeat(50)
public void testQueryCurrentTimestamp(TestContext ctx) {
    LocalDateTime start = LocalDateTime.now();
    connection.query("SELECT current_timestamp").execute(ctx.asyncAssertSuccess(rs -> {
        Object value = rs.iterator().next().getValue(0);
        ctx.assertTrue(value instanceof LocalDateTime);
        LocalDateTime localDateTime = (LocalDateTime) value;
        ctx.assertTrue(Math.abs(localDateTime.until(start, ChronoUnit.SECONDS)) < 1);
    }));
}
Also used : LocalDateTime(java.time.LocalDateTime) TestContext(io.vertx.ext.unit.TestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) LocalDateTime(java.time.LocalDateTime) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Repeat(io.vertx.ext.unit.junit.Repeat) Tuple(io.vertx.sqlclient.Tuple) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Rule(org.junit.Rule) ChronoUnit(java.time.temporal.ChronoUnit) After(org.junit.After) Row(io.vertx.sqlclient.Row) NullValue(io.vertx.sqlclient.data.NullValue) RepeatRule(io.vertx.ext.unit.junit.RepeatRule) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) Test(org.junit.Test) Repeat(io.vertx.ext.unit.junit.Repeat)

Example 2 with Repeat

use of io.vertx.ext.unit.junit.Repeat in project vertx-sql-client by eclipse-vertx.

the class PgPoolTest method testNoConnectionLeaks.

@Test
@Repeat(50)
public void testNoConnectionLeaks(TestContext ctx) {
    Async killConnections = ctx.async();
    PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> {
        Collector<Row, ?, List<Integer>> collector = mapping(row -> row.getInteger(0), toList());
        String sql = "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND datname = $1";
        PreparedQuery<SqlResult<List<Integer>>> preparedQuery = conn.preparedQuery(sql).collecting(collector);
        Tuple params = Tuple.of(options.getDatabase());
        preparedQuery.execute(params).compose(cf -> conn.close()).onComplete(ctx.asyncAssertSuccess(v -> killConnections.complete()));
    }));
    killConnections.awaitSuccess();
    String sql = "SELECT pg_backend_pid() AS pid, (SELECT count(*) FROM pg_stat_activity WHERE application_name LIKE '%vertx%') AS cnt";
    int idleTimeout = 50;
    poolOptions.setMaxSize(1).setIdleTimeout(idleTimeout).setIdleTimeoutUnit(TimeUnit.MILLISECONDS).setPoolCleanerPeriod(5);
    PgPool pool = createPool(options, poolOptions);
    Async async = ctx.async();
    AtomicInteger pid = new AtomicInteger();
    vertx.getOrCreateContext().runOnContext(v -> {
        pool.query(sql).execute(ctx.asyncAssertSuccess(rs1 -> {
            Row row1 = rs1.iterator().next();
            pid.set(row1.getInteger("pid"));
            ctx.assertEquals(1, row1.getInteger("cnt"));
            vertx.setTimer(2 * idleTimeout, l -> {
                pool.query(sql).execute(ctx.asyncAssertSuccess(rs2 -> {
                    Row row2 = rs2.iterator().next();
                    ctx.assertEquals(1, row2.getInteger("cnt"));
                    ctx.assertNotEquals(pid.get(), row2.getInteger("pid"));
                    async.complete();
                }));
            });
        }));
    });
    async.awaitSuccess();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) PgSocketConnection(io.vertx.pgclient.impl.PgSocketConnection) ConnectionFactory(io.vertx.sqlclient.spi.ConnectionFactory) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PgDriver(io.vertx.pgclient.spi.PgDriver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collectors.mapping(java.util.stream.Collectors.mapping) SqlConnectionInternal(io.vertx.sqlclient.impl.SqlConnectionInternal) RepeatRule(io.vertx.ext.unit.junit.RepeatRule) Collector(java.util.stream.Collector) io.vertx.sqlclient(io.vertx.sqlclient) VertxOptions(io.vertx.core.VertxOptions) Set(java.util.Set) Test(org.junit.Test) EventLoop(io.netty.channel.EventLoop) Repeat(io.vertx.ext.unit.junit.Repeat) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Rule(org.junit.Rule) Handler(io.vertx.core.Handler) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Collector(java.util.stream.Collector) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Test(org.junit.Test) Repeat(io.vertx.ext.unit.junit.Repeat)

Example 3 with Repeat

use of io.vertx.ext.unit.junit.Repeat in project vertx-sql-client by eclipse-vertx.

the class MySQLPoolTest method testNoConnectionLeaks.

@Test
@Repeat(50)
public void testNoConnectionLeaks(TestContext ctx) {
    Tuple params = Tuple.of(options.getUser(), options.getDatabase());
    Async killConnections = ctx.async();
    MySQLConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> {
        String sql = "SELECT ID FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID <> CONNECTION_ID() AND User = ? AND db = ?";
        Collector<Row, ?, List<Integer>> collector = mapping(row -> row.getInteger(0), toList());
        conn.preparedQuery(sql).collecting(collector).execute(params, ctx.asyncAssertSuccess(ids -> {
            CompositeFuture killAll = ids.value().stream().<Future>map(connId -> conn.query("KILL " + connId).execute()).collect(Collectors.collectingAndThen(toList(), CompositeFuture::all));
            killAll.compose(cf -> conn.close()).onComplete(ctx.asyncAssertSuccess(v -> killConnections.complete()));
        }));
    }));
    killConnections.awaitSuccess();
    String sql = "SELECT CONNECTION_ID() AS cid, (SELECT count(*) FROM INFORMATION_SCHEMA.PROCESSLIST WHERE User = ? AND db = ?) AS cnt";
    int idleTimeout = 50;
    PoolOptions poolOptions = new PoolOptions().setMaxSize(1).setIdleTimeout(idleTimeout).setIdleTimeoutUnit(TimeUnit.MILLISECONDS).setPoolCleanerPeriod(5);
    pool = MySQLPool.pool(options, poolOptions);
    Async async = ctx.async();
    AtomicInteger cid = new AtomicInteger();
    vertx.getOrCreateContext().runOnContext(v -> {
        pool.preparedQuery(sql).execute(params, ctx.asyncAssertSuccess(rs1 -> {
            Row row1 = rs1.iterator().next();
            cid.set(row1.getInteger("cid"));
            ctx.assertEquals(1, row1.getInteger("cnt"));
            vertx.setTimer(2 * idleTimeout, l -> {
                pool.preparedQuery(sql).execute(params, ctx.asyncAssertSuccess(rs2 -> {
                    Row row2 = rs2.iterator().next();
                    ctx.assertEquals(1, row2.getInteger("cnt"));
                    ctx.assertNotEquals(cid.get(), row2.getInteger("cid"));
                    async.complete();
                }));
            });
        }));
    });
    async.awaitSuccess();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Repeat(io.vertx.ext.unit.junit.Repeat) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) PoolOptions(io.vertx.sqlclient.PoolOptions) Tuple(io.vertx.sqlclient.Tuple) TimeUnit(java.util.concurrent.TimeUnit) CompositeFuture(io.vertx.core.CompositeFuture) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Rule(org.junit.Rule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Collectors.mapping(java.util.stream.Collectors.mapping) Row(io.vertx.sqlclient.Row) RepeatRule(io.vertx.ext.unit.junit.RepeatRule) Collector(java.util.stream.Collector) Before(org.junit.Before) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompositeFuture(io.vertx.core.CompositeFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Collector(java.util.stream.Collector) PoolOptions(io.vertx.sqlclient.PoolOptions) Row(io.vertx.sqlclient.Row) Tuple(io.vertx.sqlclient.Tuple) Test(org.junit.Test) Repeat(io.vertx.ext.unit.junit.Repeat)

Aggregations

TestContext (io.vertx.ext.unit.TestContext)3 Repeat (io.vertx.ext.unit.junit.Repeat)3 RepeatRule (io.vertx.ext.unit.junit.RepeatRule)3 Rule (org.junit.Rule)3 Test (org.junit.Test)3 Future (io.vertx.core.Future)2 Vertx (io.vertx.core.Vertx)2 Async (io.vertx.ext.unit.Async)2 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)2 Row (io.vertx.sqlclient.Row)2 Tuple (io.vertx.sqlclient.Tuple)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collector (java.util.stream.Collector)2 Collectors.mapping (java.util.stream.Collectors.mapping)2 Collectors.toList (java.util.stream.Collectors.toList)2 After (org.junit.After)2 Before (org.junit.Before)2 EventLoop (io.netty.channel.EventLoop)1