-
Notifications
You must be signed in to change notification settings - Fork 131
perf(diagnostics): ограничение пула JLanguageTool #4260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6031af2
perf(diagnostics): ограничение пула JLanguageTool
sfaqer bb25dbd
fix(utils): замечания Sonar в AbstractObjectPool + тесты пула
sfaqer 2d551d7
test(utils): ожидание состояния потока через Awaitility (java:S2925)
sfaqer b13b486
fix(utils): валидация maxSize в AbstractObjectPool
sfaqer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
src/test/java/com/github/_1c_syntax/bsl/languageserver/utils/AbstractObjectPoolTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /* | ||
| * This file is a part of BSL Language Server. | ||
| * | ||
| * Copyright (c) 2018-2026 | ||
| * Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> and contributors | ||
| * | ||
| * SPDX-License-Identifier: LGPL-3.0-or-later | ||
| * | ||
| * BSL Language Server is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3.0 of the License, or (at your option) any later version. | ||
| * | ||
| * BSL Language Server is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with BSL Language Server. | ||
| */ | ||
| package com.github._1c_syntax.bsl.languageserver.utils; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.Timeout; | ||
|
|
||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.awaitility.Awaitility.await; | ||
|
|
||
| class AbstractObjectPoolTest { | ||
|
|
||
| private static final class CountingPool extends AbstractObjectPool<Object> { | ||
|
|
||
| private final AtomicInteger created = new AtomicInteger(); | ||
|
|
||
| private CountingPool() { | ||
| } | ||
|
|
||
| private CountingPool(int maxSize) { | ||
| super(maxSize); | ||
| } | ||
|
|
||
| @Override | ||
| protected Object create() { | ||
| created.incrementAndGet(); | ||
| return new Object(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void checkOutReusesReturnedInstance() { | ||
| var pool = new CountingPool(); | ||
|
|
||
| var first = pool.checkOut(); | ||
| pool.checkIn(first); | ||
| var second = pool.checkOut(); | ||
|
|
||
| assertThat(second).isSameAs(first); | ||
| assertThat(pool.created.get()).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void unboundedPoolCreatesInstancePerConcurrentCheckout() { | ||
| var pool = new CountingPool(); | ||
|
|
||
| var first = pool.checkOut(); | ||
| var second = pool.checkOut(); | ||
|
|
||
| assertThat(second).isNotSameAs(first); | ||
| assertThat(pool.created.get()).isEqualTo(2); | ||
| } | ||
|
|
||
| @Test | ||
| @Timeout(10) | ||
| void boundedPoolBlocksUntilCheckInAndHandsOverInstance() throws InterruptedException { | ||
| var pool = new CountingPool(1); | ||
| var first = pool.checkOut(); | ||
|
|
||
| var handedOver = new AtomicReference<>(); | ||
| var waiterStarted = new CountDownLatch(1); | ||
| var waiterFinished = new CountDownLatch(1); | ||
| var waiter = new Thread(() -> { | ||
| waiterStarted.countDown(); | ||
| handedOver.set(pool.checkOut()); | ||
| waiterFinished.countDown(); | ||
| }); | ||
| waiter.start(); | ||
|
|
||
| assertThat(waiterStarted.await(5, TimeUnit.SECONDS)).isTrue(); | ||
| // Ожидающий поток не должен создать новый экземпляр сверх лимита | ||
| assertThat(waiterFinished.await(200, TimeUnit.MILLISECONDS)).isFalse(); | ||
| assertThat(pool.created.get()).isEqualTo(1); | ||
|
|
||
| pool.checkIn(first); | ||
|
|
||
| assertThat(waiterFinished.await(5, TimeUnit.SECONDS)).isTrue(); | ||
| assertThat(handedOver.get()).isSameAs(first); | ||
| assertThat(pool.created.get()).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| @Timeout(10) | ||
| void interruptedWaiterGetsInstanceOverCapAndKeepsInterruptFlag() throws InterruptedException { | ||
| var pool = new CountingPool(1); | ||
| pool.checkOut(); | ||
|
|
||
| var wasInterrupted = new AtomicReference<Boolean>(); | ||
| var received = new AtomicReference<>(); | ||
| var waiter = new Thread(() -> { | ||
| received.set(pool.checkOut()); | ||
| wasInterrupted.set(Thread.currentThread().isInterrupted()); | ||
| }); | ||
| waiter.start(); | ||
|
|
||
| // Дождаться входа в ожидание и прервать | ||
| await().atMost(5, TimeUnit.SECONDS).until(() -> waiter.getState() == Thread.State.WAITING); | ||
| waiter.interrupt(); | ||
| waiter.join(TimeUnit.SECONDS.toMillis(5)); | ||
|
|
||
| assertThat(waiter.isAlive()).isFalse(); | ||
| assertThat(received.get()).isNotNull(); | ||
| assertThat(pool.created.get()).isEqualTo(2); | ||
| assertThat(wasInterrupted.get()).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void nonPositiveMaxSizeIsRejected() { | ||
| assertThatThrownBy(() -> new CountingPool(0)) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void toStringReportsPoolState() { | ||
| var pool = new CountingPool(); | ||
| var instance = pool.checkOut(); | ||
|
|
||
| assertThat(pool).hasToString("Pool available=0 inUse=1"); | ||
|
|
||
| pool.checkIn(instance); | ||
|
|
||
| assertThat(pool).hasToString("Pool available=1 inUse=0"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Зачем?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
До ограничения пула
checkOut()никогда не ждал: при пустомavailableсразу создавался новый экземпляр. С появлениемmaxSizeпоток при исчерпании лимита встаёт вwait()— безnotify*вcheckIn()он не проснётся никогда.notifyAllвместоnotify— по замечанию Sonar (java:S2446):notifyбудит произвольный поток. Ожидающие здесь однородны, так что технически хватило бы иnotify, ноnotifyAll— стандартный безопасный паттерн, а лишних пробуждений при размере пула ≤4 единицы.