| 1 |
/* |
| 2 |
* Copyright (C) 2013 Brett Wooldridge |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.zaxxer.hikari.pool; |
| 18 |
|
| 19 |
import static com.zaxxer.hikari.pool.TestElf.getPool; |
| 20 |
import static com.zaxxer.hikari.pool.TestElf.newHikariConfig; |
| 21 |
import static com.zaxxer.hikari.pool.TestElf.setSlf4jTargetStream; |
| 22 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 23 |
import static org.junit.Assert.assertEquals; |
| 24 |
import static org.junit.Assert.assertNotEquals; |
| 25 |
import static org.junit.Assert.assertNotNull; |
| 26 |
import static org.junit.Assert.assertTrue; |
| 27 |
|
| 28 |
import java.io.ByteArrayOutputStream; |
| 29 |
import java.io.PrintStream; |
| 30 |
import java.util.concurrent.CompletableFuture; |
| 31 |
|
| 32 |
import org.junit.AfterClass; |
| 33 |
import org.junit.BeforeClass; |
| 34 |
import org.junit.Test; |
| 35 |
|
| 36 |
import com.zaxxer.hikari.HikariConfig; |
| 37 |
import com.zaxxer.hikari.HikariDataSource; |
| 38 |
import com.zaxxer.hikari.util.ConcurrentBag; |
| 39 |
|
| 40 |
/** |
| 41 |
* |
| 42 |
* @author Brett Wooldridge |
| 43 |
*/ |
| 44 |
public class TestConcurrentBag |
| 45 |
{ |
| 46 |
private static HikariDataSource ds; |
| 47 |
private static HikariPool pool; |
| 48 |
|
| 49 |
@BeforeClass |
| 50 |
public static void setup() |
| 51 |
{ |
| 52 |
HikariConfig config = newHikariConfig(); |
| 53 |
config.setMinimumIdle(1); |
| 54 |
config.setMaximumPoolSize(2); |
| 55 |
config.setInitializationFailTimeout(0); |
| 56 |
config.setConnectionTestQuery("VALUES 1"); |
| 57 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 58 |
|
| 59 |
ds = new HikariDataSource(config); |
| 60 |
pool = getPool(ds); |
| 61 |
} |
| 62 |
|
| 63 |
@AfterClass |
| 64 |
public static void teardown() |
| 65 |
{ |
| 66 |
ds.close(); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void testConcurrentBag() throws Exception |
| 71 |
{ |
| 72 |
try (ConcurrentBag<PoolEntry> bag = new ConcurrentBag<>((x) -> CompletableFuture.completedFuture(Boolean.TRUE))) { |
| 73 |
assertEquals(0, bag.values(8).size()); |
| 74 |
|
| 75 |
PoolEntry reserved = pool.newPoolEntry(); |
| 76 |
bag.add(reserved); |
| 77 |
bag.reserve(reserved); // reserved |
| 78 |
|
| 79 |
PoolEntry inuse = pool.newPoolEntry(); |
| 80 |
bag.add(inuse); |
| 81 |
bag.borrow(2, MILLISECONDS); // in use |
| 82 |
|
| 83 |
PoolEntry notinuse = pool.newPoolEntry(); |
| 84 |
bag.add(notinuse); // not in use |
| 85 |
|
| 86 |
bag.dumpState(); |
| 87 |
|
| 88 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 89 |
PrintStream ps = new PrintStream(baos, true); |
| 90 |
setSlf4jTargetStream(ConcurrentBag.class, ps); |
| 91 |
|
| 92 |
bag.requite(reserved); |
| 93 |
|
| 94 |
bag.remove(notinuse); |
| 95 |
assertTrue(new String(baos.toByteArray()).contains("not borrowed or reserved")); |
| 96 |
|
| 97 |
bag.unreserve(notinuse); |
| 98 |
assertTrue(new String(baos.toByteArray()).contains("was not reserved")); |
| 99 |
|
| 100 |
bag.remove(inuse); |
| 101 |
bag.remove(inuse); |
| 102 |
assertTrue(new String(baos.toByteArray()).contains("not borrowed or reserved")); |
| 103 |
|
| 104 |
bag.close(); |
| 105 |
try { |
| 106 |
PoolEntry bagEntry = pool.newPoolEntry(); |
| 107 |
bag.add(bagEntry); |
| 108 |
assertNotEquals(bagEntry, bag.borrow(100, MILLISECONDS)); |
| 109 |
} |
| 110 |
catch (IllegalStateException e) { |
| 111 |
assertTrue(new String(baos.toByteArray()).contains("ignoring add()")); |
| 112 |
} |
| 113 |
|
| 114 |
assertNotNull(notinuse.toString()); |
| 115 |
} |
| 116 |
} |
| 117 |
} |