14 Haziran 2023 Çarşamba

ToxiproxyContainers ToxiproxyContainer Sınıfı

Maven
Şu satırı dahil ederiz
<dependency>
<groupId>org.testcontainers</groupId> <artifactId>toxiproxy</artifactId> <version>1.18.3</version> <scope>test</scope> </dependency>
ToxiproxyClient Sınıfı
ToxiproxyContainer kullanılarak yaratılır

latency metodu
Örnek
Şöyle yaparız
import eu.rekawek.toxiproxy.Proxy;
import eu.rekawek.toxiproxy.ToxiproxyClient;
import eu.rekawek.toxiproxy.model.ToxicDirection;

@Testcontainers
public class ToxiproxyExample {

  @Container
  private static ToxiproxyContainer toxiproxy = new ToxiproxyContainer();

  @Container
  private static PostgreSQLContainer<?> database = new PostgreSQLContainer<>("postgres:11.19-bullseye")

  @Rule
  public ToxiproxyClient toxiproxyClient = new ToxiproxyClient(toxiproxy.getHost(), toxiproxy.getControlPort());
   
  @Test
  public void testApplicationWithLatency() {
    // ToxicDirection.DOWNSTREAM is from client to DB
    // Create a toxic that adds 100 milliseconds of latency to the connection to the DB.
    toxiproxyClient.toxics().latency("database", ToxicDirection.DOWNSTREAM, 100);

    // Try to connect to the database.
    try (var connection = DriverManager.getConnection(database.getHost() + ":" + database.getMappedPort(5432), "postgres", "postgres")) {
        // Do something with the connection.
    }
    // Remove the toxic.
    toxiproxyClient.toxics().get("database").remove();
  }
}


Hiç yorum yok:

Yorum Gönder