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

オープン
#10,360 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
15

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

testcontainers/testcontainers-java のほかの issue

testcontainers/testcontainers-java の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。