|
| 1 | +/* |
| 2 | + * This file is a part of BSL Language Server. |
| 3 | + * |
| 4 | + * Copyright (c) 2018-2026 |
| 5 | + * Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> and contributors |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: LGPL-3.0-or-later |
| 8 | + * |
| 9 | + * BSL Language Server is free software; you can redistribute it and/or |
| 10 | + * modify it under the terms of the GNU Lesser General Public |
| 11 | + * License as published by the Free Software Foundation; either |
| 12 | + * version 3.0 of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * BSL Language Server is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with BSL Language Server. |
| 21 | + */ |
| 22 | +package com.github._1c_syntax.bsl.languageserver.utils; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.Timeout; |
| 26 | + |
| 27 | +import java.util.concurrent.CountDownLatch; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import java.util.concurrent.atomic.AtomicInteger; |
| 30 | +import java.util.concurrent.atomic.AtomicReference; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 34 | +import static org.awaitility.Awaitility.await; |
| 35 | + |
| 36 | +class AbstractObjectPoolTest { |
| 37 | + |
| 38 | + private static final class CountingPool extends AbstractObjectPool<Object> { |
| 39 | + |
| 40 | + private final AtomicInteger created = new AtomicInteger(); |
| 41 | + |
| 42 | + private CountingPool() { |
| 43 | + } |
| 44 | + |
| 45 | + private CountingPool(int maxSize) { |
| 46 | + super(maxSize); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected Object create() { |
| 51 | + created.incrementAndGet(); |
| 52 | + return new Object(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void checkOutReusesReturnedInstance() { |
| 58 | + var pool = new CountingPool(); |
| 59 | + |
| 60 | + var first = pool.checkOut(); |
| 61 | + pool.checkIn(first); |
| 62 | + var second = pool.checkOut(); |
| 63 | + |
| 64 | + assertThat(second).isSameAs(first); |
| 65 | + assertThat(pool.created.get()).isEqualTo(1); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void unboundedPoolCreatesInstancePerConcurrentCheckout() { |
| 70 | + var pool = new CountingPool(); |
| 71 | + |
| 72 | + var first = pool.checkOut(); |
| 73 | + var second = pool.checkOut(); |
| 74 | + |
| 75 | + assertThat(second).isNotSameAs(first); |
| 76 | + assertThat(pool.created.get()).isEqualTo(2); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @Timeout(10) |
| 81 | + void boundedPoolBlocksUntilCheckInAndHandsOverInstance() throws InterruptedException { |
| 82 | + var pool = new CountingPool(1); |
| 83 | + var first = pool.checkOut(); |
| 84 | + |
| 85 | + var handedOver = new AtomicReference<>(); |
| 86 | + var waiterStarted = new CountDownLatch(1); |
| 87 | + var waiterFinished = new CountDownLatch(1); |
| 88 | + var waiter = new Thread(() -> { |
| 89 | + waiterStarted.countDown(); |
| 90 | + handedOver.set(pool.checkOut()); |
| 91 | + waiterFinished.countDown(); |
| 92 | + }); |
| 93 | + waiter.start(); |
| 94 | + |
| 95 | + assertThat(waiterStarted.await(5, TimeUnit.SECONDS)).isTrue(); |
| 96 | + // Ожидающий поток не должен создать новый экземпляр сверх лимита |
| 97 | + assertThat(waiterFinished.await(200, TimeUnit.MILLISECONDS)).isFalse(); |
| 98 | + assertThat(pool.created.get()).isEqualTo(1); |
| 99 | + |
| 100 | + pool.checkIn(first); |
| 101 | + |
| 102 | + assertThat(waiterFinished.await(5, TimeUnit.SECONDS)).isTrue(); |
| 103 | + assertThat(handedOver.get()).isSameAs(first); |
| 104 | + assertThat(pool.created.get()).isEqualTo(1); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @Timeout(10) |
| 109 | + void interruptedWaiterGetsInstanceOverCapAndKeepsInterruptFlag() throws InterruptedException { |
| 110 | + var pool = new CountingPool(1); |
| 111 | + pool.checkOut(); |
| 112 | + |
| 113 | + var wasInterrupted = new AtomicReference<Boolean>(); |
| 114 | + var received = new AtomicReference<>(); |
| 115 | + var waiter = new Thread(() -> { |
| 116 | + received.set(pool.checkOut()); |
| 117 | + wasInterrupted.set(Thread.currentThread().isInterrupted()); |
| 118 | + }); |
| 119 | + waiter.start(); |
| 120 | + |
| 121 | + // Дождаться входа в ожидание и прервать |
| 122 | + await().atMost(5, TimeUnit.SECONDS).until(() -> waiter.getState() == Thread.State.WAITING); |
| 123 | + waiter.interrupt(); |
| 124 | + waiter.join(TimeUnit.SECONDS.toMillis(5)); |
| 125 | + |
| 126 | + assertThat(waiter.isAlive()).isFalse(); |
| 127 | + assertThat(received.get()).isNotNull(); |
| 128 | + assertThat(pool.created.get()).isEqualTo(2); |
| 129 | + assertThat(wasInterrupted.get()).isTrue(); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void nonPositiveMaxSizeIsRejected() { |
| 134 | + assertThatThrownBy(() -> new CountingPool(0)) |
| 135 | + .isInstanceOf(IllegalArgumentException.class); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void toStringReportsPoolState() { |
| 140 | + var pool = new CountingPool(); |
| 141 | + var instance = pool.checkOut(); |
| 142 | + |
| 143 | + assertThat(pool).hasToString("Pool available=0 inUse=1"); |
| 144 | + |
| 145 | + pool.checkIn(instance); |
| 146 | + |
| 147 | + assertThat(pool).hasToString("Pool available=1 inUse=0"); |
| 148 | + } |
| 149 | +} |
0 commit comments