| 1 |
/* |
| 2 |
* Copyright (C) 2013, 2014 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.newHikariConfig; |
| 20 |
import static org.junit.Assert.assertEquals; |
| 21 |
|
| 22 |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 23 |
import java.util.concurrent.ThreadFactory; |
| 24 |
import java.util.concurrent.ThreadPoolExecutor; |
| 25 |
import java.util.concurrent.TimeUnit; |
| 26 |
|
| 27 |
import org.junit.After; |
| 28 |
import org.junit.Before; |
| 29 |
import org.junit.Test; |
| 30 |
|
| 31 |
import com.zaxxer.hikari.HikariConfig; |
| 32 |
import com.zaxxer.hikari.HikariDataSource; |
| 33 |
import com.zaxxer.hikari.util.UtilityElf; |
| 34 |
|
| 35 |
/** |
| 36 |
* @author Martin Stříž ([email protected]) |
| 37 |
*/ |
| 38 |
public class HouseKeeperCleanupTest |
| 39 |
{ |
| 40 |
|
| 41 |
private ScheduledThreadPoolExecutor executor; |
| 42 |
|
| 43 |
@Before |
| 44 |
public void before() throws Exception |
| 45 |
{ |
| 46 |
ThreadFactory threadFactory = new UtilityElf.DefaultThreadFactory("global housekeeper", true); |
| 47 |
|
| 48 |
executor = new ScheduledThreadPoolExecutor(1, threadFactory, new ThreadPoolExecutor.DiscardPolicy()); |
| 49 |
executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
| 50 |
executor.setRemoveOnCancelPolicy(true); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void testHouseKeeperCleanupWithCustomExecutor() throws Exception |
| 55 |
{ |
| 56 |
HikariConfig config = newHikariConfig(); |
| 57 |
config.setMinimumIdle(0); |
| 58 |
config.setMaximumPoolSize(10); |
| 59 |
config.setInitializationFailTimeout(Long.MAX_VALUE); |
| 60 |
config.setConnectionTimeout(2500); |
| 61 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 62 |
config.setScheduledExecutor(executor); |
| 63 |
|
| 64 |
HikariConfig config2 = newHikariConfig(); |
| 65 |
config.copyStateTo(config2); |
| 66 |
|
| 67 |
try ( |
| 68 |
final HikariDataSource ds1 = new HikariDataSource(config); |
| 69 |
final HikariDataSource ds2 = new HikariDataSource(config2) |
| 70 |
) { |
| 71 |
assertEquals("Scheduled tasks count not as expected, ", 2, executor.getQueue().size()); |
| 72 |
} |
| 73 |
|
| 74 |
assertEquals("Scheduled tasks count not as expected, ", 0, executor.getQueue().size()); |
| 75 |
} |
| 76 |
|
| 77 |
@After |
| 78 |
public void after() throws Exception |
| 79 |
{ |
| 80 |
executor.shutdown(); |
| 81 |
executor.awaitTermination(5, TimeUnit.SECONDS); |
| 82 |
} |
| 83 |
|
| 84 |
} |