|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.github.pgasync.impl; |
| 16 | + |
| 17 | +import static com.github.pgasync.impl.DatabaseRule.createPoolBuilder; |
| 18 | +import static java.lang.System.currentTimeMillis; |
| 19 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 20 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 21 | +import static org.hamcrest.CoreMatchers.containsString; |
| 22 | +import static org.hamcrest.CoreMatchers.is; |
| 23 | +import static org.hamcrest.CoreMatchers.isA; |
| 24 | +import static org.junit.Assert.assertThat; |
| 25 | + |
| 26 | +import java.util.Deque; |
| 27 | +import java.util.concurrent.BlockingQueue; |
| 28 | +import java.util.concurrent.CountDownLatch; |
| 29 | +import java.util.concurrent.LinkedBlockingDeque; |
| 30 | +import java.util.concurrent.SynchronousQueue; |
| 31 | +import java.util.concurrent.atomic.AtomicLong; |
| 32 | +import java.util.function.Consumer; |
| 33 | + |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +import com.github.pgasync.Connection; |
| 38 | +import com.github.pgasync.ConnectionPool; |
| 39 | +import com.github.pgasync.ResultSet; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests for statement pipelining. |
| 43 | + * |
| 44 | + * @author Mikko Tiihonen |
| 45 | + */ |
| 46 | +public class PipelineTest { |
| 47 | + final Consumer<Throwable> err = t -> { throw new AssertionError("failed", t); }; |
| 48 | + |
| 49 | + Connection c; |
| 50 | + ConnectionPool pool; |
| 51 | + |
| 52 | + @After |
| 53 | + public void closeConnection() { |
| 54 | + if (c != null) { |
| 55 | + pool.release(c); |
| 56 | + } |
| 57 | + if (pool != null) { |
| 58 | + pool.close(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void connectionPipelinesQueries() throws InterruptedException { |
| 64 | + pool = createPoolBuilder(1).pipeline(true).build(); |
| 65 | + |
| 66 | + int count = 5; |
| 67 | + double sleep = 0.5; |
| 68 | + Deque<Long> results = new LinkedBlockingDeque<>(); |
| 69 | + long startWrite = currentTimeMillis(); |
| 70 | + for (int i = 0; i < count; ++i) { |
| 71 | + pool.query("select " + i + ", pg_sleep(" + sleep + ")", r -> results.add(currentTimeMillis()), |
| 72 | + err); |
| 73 | + } |
| 74 | + long writeTime = currentTimeMillis() - startWrite; |
| 75 | + |
| 76 | + long remoteWaitTimeSeconds = (long) (sleep * count); |
| 77 | + SECONDS.sleep(1 + remoteWaitTimeSeconds); |
| 78 | + long readTime = results.getLast() - results.getFirst(); |
| 79 | + |
| 80 | + assertThat(results.size(), is(count)); |
| 81 | + assertThat(MILLISECONDS.toSeconds(writeTime), is(0L)); |
| 82 | + assertThat(MILLISECONDS.toSeconds(readTime + 999) >= remoteWaitTimeSeconds, is(true)); |
| 83 | + } |
| 84 | + |
| 85 | + private Connection getConnection(boolean pipeline) throws InterruptedException { |
| 86 | + pool = createPoolBuilder(1).pipeline(pipeline).build(); |
| 87 | + SynchronousQueue<Connection> connQueue = new SynchronousQueue<>(); |
| 88 | + pool.getConnection(c -> connQueue.add(c), err); |
| 89 | + return c = connQueue.take(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void disabledConnectionPipeliningThrowsErrorWhenPipeliningIsAttempted() throws Exception { |
| 94 | + Connection c = getConnection(false); |
| 95 | + |
| 96 | + BlockingQueue<ResultSet> rs = new LinkedBlockingDeque<>(); |
| 97 | + BlockingQueue<Throwable> err = new LinkedBlockingDeque<>(); |
| 98 | + for (int i = 0; i < 2; ++i) { |
| 99 | + c.query("select " + i + ", pg_sleep(0.5)", r -> rs.add(r), e -> err.add(e)); |
| 100 | + } |
| 101 | + assertThat(err.take().getMessage(), containsString("Pipelining not enabled")); |
| 102 | + assertThat(rs.take(), isA(ResultSet.class)); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void connectionPoolPipelinesQueries() throws InterruptedException { |
| 107 | + Connection c = getConnection(true); |
| 108 | + |
| 109 | + int count = 5; |
| 110 | + double sleep = 0.5; |
| 111 | + Deque<Long> results = new LinkedBlockingDeque<>(); |
| 112 | + long startWrite = currentTimeMillis(); |
| 113 | + for (int i = 0; i < count; ++i) { |
| 114 | + c.query("select " + i + ", pg_sleep(" + sleep + ")", r -> results.add(currentTimeMillis()), |
| 115 | + err); |
| 116 | + } |
| 117 | + long writeTime = currentTimeMillis() - startWrite; |
| 118 | + |
| 119 | + long remoteWaitTimeSeconds = (long) (sleep * count); |
| 120 | + SECONDS.sleep(1 + remoteWaitTimeSeconds); |
| 121 | + long readTime = results.getLast() - results.getFirst(); |
| 122 | + |
| 123 | + assertThat(results.size(), is(count)); |
| 124 | + assertThat(MILLISECONDS.toSeconds(writeTime), is(0L)); |
| 125 | + assertThat(MILLISECONDS.toSeconds(readTime + 999) >= remoteWaitTimeSeconds, is(true)); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void connectionPoolPipelinesQueriesWithinTransaction() throws InterruptedException { |
| 130 | + pool = createPoolBuilder(1).pipeline(true).build(); |
| 131 | + |
| 132 | + int count = 5; |
| 133 | + double sleep = 0.5; |
| 134 | + Deque<Long> results = new LinkedBlockingDeque<>(); |
| 135 | + AtomicLong writeTime = new AtomicLong(); |
| 136 | + |
| 137 | + CountDownLatch sync = new CountDownLatch(1); |
| 138 | + long startWrite = currentTimeMillis(); |
| 139 | + pool.begin(t -> { |
| 140 | + for (int i = 0; i < count; ++i) { |
| 141 | + t.query("select " + i + ", pg_sleep(" + sleep + ")", r -> results.add(currentTimeMillis()), |
| 142 | + err); |
| 143 | + } |
| 144 | + t.commit(() -> { |
| 145 | + sync.countDown(); |
| 146 | + } , err); |
| 147 | + writeTime.set(currentTimeMillis() - startWrite); |
| 148 | + } , err); |
| 149 | + sync.await(3, SECONDS); |
| 150 | + |
| 151 | + long remoteWaitTimeSeconds = (long) (sleep * count); |
| 152 | + SECONDS.sleep(1 + remoteWaitTimeSeconds); |
| 153 | + long readTime = results.getLast() - results.getFirst(); |
| 154 | + |
| 155 | + assertThat(results.size(), is(count)); |
| 156 | + assertThat(MILLISECONDS.toSeconds(writeTime.get()), is(0L)); |
| 157 | + assertThat(MILLISECONDS.toSeconds(readTime + 999) >= remoteWaitTimeSeconds, is(true)); |
| 158 | + } |
| 159 | +} |
0 commit comments