[Enhancement]: JdbcDatabaseContainer - Improve ``waitUntilContainerStarted``

未關閉
#10,360 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
45/100
Issue 類型
功能
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
java
領域
databases, testing

研究方向

從 modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java 開始,特別查看 waitUntilContainerStarted,並閱讀現有的 WaitStrategy 和 AbstractWaitStrategy API。加入 issue 中描述的 JDBC 專用 strategy 和預設的 combined strategy;完成的標準是啟動流程使用已設定的 rate limiter,且不再執行舊的硬編碼 polling 或 override 行為。

由索引模型根據 Issue 內容生成。

描述

type/enhancement
Module

Core

Proposal

Currently https://github.com/testcontainers/testcontainers-java/blob/7d8301903a6b45591605fd376702d24fc878a61d/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java#L176-L216 behaves very weird and this also impacts performance:

  • It doesn't utilize WaitStrategy and completely ignores/overrides it
  • It constantly tries to query if the container is running or builds database connections (these are very costly in terms of CPU usage)
    • These attempts use a hardcoded throttling value of 100ms and completely ignore the RateLimiter of WaitStrategy

I would propose that you use - as in all other containers - the WaitStrategy.

  • Remove the override/method JdbcDatabaseContainer#waitUntilContainerStarted
  • Create a custom WaitStrategy called JDBCWaitStrategy. It could look like this:
    class JDBCWaitStrategy extends AbstractWaitStrategy
    {
    	@Override
    	protected void waitUntilReady()
    	{
    		if(!(this.waitStrategyTarget instanceof final JdbcDatabaseContainer<?> container))
    		{
    			throw new IllegalArgumentException(
    				"Container must implement JdbcDatabaseContainer");
    		}
    		
    		try
    		{
    			Unreliables.retryUntilTrue(
    				(int)this.startupTimeout.getSeconds(),
    				TimeUnit.SECONDS,
    				() -> this.getRateLimiter().getWhenReady(() -> {
    					try(final Connection connection = container.createConnection("");
    						final Statement statement = connection.createStatement())
    					{
    						return statement.execute(container.getTestQueryString());
    					}
    				})
    			);
    		}
    		catch(final TimeoutException e)
    		{
    			throw new ContainerLaunchException(
    				"JDBCContainer cannot be accessed by (JDBC URL: "
    					+ container.getJdbcUrl()
    					+ "), please check container logs");
    		}
    	}
    }
    
  • Use the following default WaitStrategy for JDBCDatabaseContainer:
    new WaitAllStrategy()
    	.withStrategy(Wait.defaultWaitStrategy())
    	.withStrategy(new JDBCWaitStrategy())
    

Full example implementation:

主要語言
Java
星號
8.8k
分支
2k
平均合併
5 天 21 小時
30 天內合併 PR
15

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

testcontainers/testcontainers-java 的其他 Issue

查看 testcontainers/testcontainers-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。