Best Testcontainers-java code snippet using org.testcontainers.containers.ToxiproxyTest.checkCallWithLatency
Source:ToxiproxyTest.java
...44 final int portViaToxiproxy = proxy.getProxyPort();45 // }46 final Jedis jedis = new Jedis(ipAddressViaToxiproxy, portViaToxiproxy);47 jedis.set("somekey", "somevalue");48 checkCallWithLatency(jedis, "without interference", 0, 250);49 // addingLatency {50 proxy.toxics()51 .latency("latency", ToxicDirection.DOWNSTREAM, 1_100)52 .setJitter(100);53 // from now on the connection latency should be from 1000-1200 ms.54 // }55 checkCallWithLatency(jedis, "with interference", 1_000, 1_500);56 }57 @Test58 @Flaky(githubIssueUrl = "https://github.com/testcontainers/testcontainers-java/issues/1769", reviewDate = "2020-03-01")59 public void testConnectionCut() {60 final ToxiproxyContainer.ContainerProxy proxy = toxiproxy.getProxy(redis, 6379);61 final Jedis jedis = new Jedis(proxy.getContainerIpAddress(), proxy.getProxyPort());62 jedis.set("somekey", "somevalue");63 assertEquals("access to the container works OK before cutting the connection", "somevalue", jedis.get("somekey"));64 // disableProxy {65 proxy.setConnectionCut(true);66 // for example, expect failure when the connection is cut67 assertThrows("calls fail when the connection is cut",68 JedisConnectionException.class, () -> {69 jedis.get("somekey");70 });71 proxy.setConnectionCut(false);72 // and with the connection re-established, expect success73 assertEquals("access to the container works OK after re-establishing the connection", "somevalue", jedis.get("somekey"));74 // }75 }76 @Test77 @Flaky(githubIssueUrl = "https://github.com/testcontainers/testcontainers-java/issues/1769", reviewDate = "2020-03-01")78 public void testMultipleProxiesCanBeCreated() {79 try (GenericContainer secondRedis = new GenericContainer("redis:5.0.4")80 .withExposedPorts(6379)81 .withNetwork(network)) {82 secondRedis.start();83 final ToxiproxyContainer.ContainerProxy firstProxy = toxiproxy.getProxy(redis, 6379);84 final ToxiproxyContainer.ContainerProxy secondProxy = toxiproxy.getProxy(secondRedis, 6379);85 final Jedis firstJedis = new Jedis(firstProxy.getContainerIpAddress(), firstProxy.getProxyPort());86 final Jedis secondJedis = new Jedis(secondProxy.getContainerIpAddress(), secondProxy.getProxyPort());87 firstJedis.set("somekey", "somevalue");88 secondJedis.set("somekey", "somevalue");89 firstProxy.setConnectionCut(true);90 assertThrows("calls fail when the connection is cut, for only the relevant proxy",91 JedisConnectionException.class, () -> {92 firstJedis.get("somekey");93 });94 assertEquals("access via a different proxy is OK", "somevalue", secondJedis.get("somekey"));95 }96 }97 private void checkCallWithLatency(Jedis jedis, final String description, int expectedMinLatency, long expectedMaxLatency) {98 final long start = System.currentTimeMillis();99 String s = jedis.get("somekey");100 final long end = System.currentTimeMillis();101 final long duration = end - start;102 assertEquals(format("access to the container %s works OK", description), "somevalue", s);103 assertTrue(format("%s there is at least %dms latency", description, expectedMinLatency), duration >= expectedMinLatency);104 assertTrue(format("%s there is no more than %dms latency", description, expectedMaxLatency), duration < expectedMaxLatency);105 }106}...
checkCallWithLatency
Using AI Code Generation
1import org.testcontainers.containers.ToxiproxyTest2import org.testcontainers.containers.ToxiproxyContainer.ContainerProxy3import org.testcontainers.containers.ToxiproxyContainer4import org.testcontainers.containers.GenericContainer5import org.testcontainers.containers.Network6import org.testcontainers.containers.wait.strategy.Wait7import org.testcontainers.containers.output.Slf4jLogConsumer8import org.testcontainers.utility.DockerImageName9import org.testcontainers.containers.output.OutputFrame10import org.testcontainers.containers.output.ToStringConsumer11import org.testcontainers.containers.output.WaitingConsumer12import org.slf4j.LoggerFactory13import java.time.Duration14import java.util.concurrent.TimeUnit15import org.testcontainers.containers.output.FrameConsumerResultCallback16import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultCallbackException17import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultCallbackTimeoutException18import java.io.IOException19import java.util.concurrent.TimeoutException20import java.util.function.Consumer21import java.util.function.Predicate22import static org.testcontainers.containers.ToxiproxyTest.checkCallWithLatency23Network network = Network.newNetwork()24GenericContainer upstreamContainer = new GenericContainer(DockerImageName.parse("httpd:2.4.41-alpine")).withNetwork(network)25GenericContainer downstreamContainer = new GenericContainer(DockerImageName.parse("httpd:2.4.41-alpine")).withNetwork(network)26upstreamContainer.start()27downstreamContainer.start()28ToxiproxyContainer toxiproxyContainer = new ToxiproxyContainer().withNetwork(network)29toxiproxyContainer.start()30ContainerProxy upstreamProxy = toxiproxyContainer.getProxy(upstreamContainer, upstreamContainer.getMappedPort(80))31ContainerProxy downstreamProxy = toxiproxyContainer.getProxy(downstreamContainer, downstreamContainer.getMappedPort(80))32upstreamProxy.toxics().latency("latency_upstream", ToxiproxyContainer.ToxicDirection.UPSTREAM, 2000)33downstreamProxy.toxics().latency("latency_downstream", ToxiproxyContainer.ToxicDirection.DOWNSTREAM, 2000)
checkCallWithLatency
Using AI Code Generation
1import org.testcontainers.containers.ToxiproxyTest2import org.testcontainers.containers.ToxiproxyContainer.ContainerProxy3import org.testcontainers.containers.ToxiproxyContainer.ProxySpec4import org.testcontainers.containers.ToxiproxyContainer.ToxicDirection5import org.testcontainers.containers.ToxiproxyContainer.ToxicList6import org.testcontainers.containers.ToxiproxyContainer.ToxicList.ToxicListItem7import org.testcontainers.containers.ToxiproxyContainer.ToxicType8import org.testcontainers.containers.ToxiproxyContainer.Toxicity9import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Direction10import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Stream11import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Type12import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream13import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Downstream14import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Downstream.Upstream15import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Upstream.Downstream16import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Upstream.Upstream17import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Upstream.Upstream.Downstream18import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Upstream.Upstream.Downstream.Upstream19import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Upstream.Upstream.Upstream20import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Upstream.Upstream.Upstream.Downstream21import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Upstream.Upstream.Upstream.Downstream.Upstream22import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Upstream.Upstream.Upstream.Upstream23import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Upstream.Upstream.Upstream.Upstream.Downstream24import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Upstream.Upstream.Upstream.Upstream.Downstream.Upstream25import org.testcontainers.containers.ToxiproxyContainer.Toxicity.Upstream.Upstream.Upstream
checkCallWithLatency
Using AI Code Generation
1ToxiproxyContainer toxiproxy = new ToxiproxyContainer()2toxiproxy.start()3GenericContainer testContainer = new GenericContainer("alpine:3.8")4testContainer.withCommand("tail", "-f", "/dev/null")5testContainer.start()6ToxiproxyContainer.ContainerProxy proxy = toxiproxy.getProxy(testContainer, 80)7proxy.setLatency(1000)8org.testcontainers.containers.ToxiproxyTest.checkCallWithLatency(testContainer, 80, 1000)9proxy.removeLatency()10org.testcontainers.containers.ToxiproxyTest.checkCallWithLatency(testContainer, 80, 0)11ToxiproxyContainer#getProxy(GenericContainer container, int port)12ContainerProxy#setLatency(long latency)13ContainerProxy#removeLatency()14ToxiproxyTest#checkCallWithLatency(GenericContainer container, int port, long latency)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!