Best Testcontainers-java code snippet using org.testcontainers.containers.ToxiproxyTest.createJedis
Source:ToxiproxyTest.java
...34 .withNetworkAliases(TOXIPROXY_NETWORK_ALIAS);35 // }36 @Test37 public void testDirect() {38 final Jedis jedis = createJedis(redis.getHost(), redis.getFirstMappedPort());39 jedis.set("somekey", "somevalue");40 final String s = jedis.get("somekey");41 assertEquals("direct access to the container works OK", "somevalue", s);42 }43 @Test44 public void testLatencyViaProxy() throws IOException {45 // obtainProxyObject {46 final ToxiproxyContainer.ContainerProxy proxy = toxiproxy.getProxy(redis, 6379);47 // }48 // obtainProxiedHostAndPortForHostMachine {49 final String ipAddressViaToxiproxy = proxy.getContainerIpAddress();50 final int portViaToxiproxy = proxy.getProxyPort();51 // }52 final Jedis jedis = createJedis(ipAddressViaToxiproxy, portViaToxiproxy);53 jedis.set("somekey", "somevalue");54 checkCallWithLatency(jedis, "without interference", 0, 250);55 // addingLatency {56 proxy.toxics()57 .latency("latency", ToxicDirection.DOWNSTREAM, 1_100)58 .setJitter(100);59 // from now on the connection latency should be from 1000-1200 ms.60 // }61 checkCallWithLatency(jedis, "with interference", 1_000, 1_500);62 }63 @Test64 public void testConnectionCut() {65 final ToxiproxyContainer.ContainerProxy proxy = toxiproxy.getProxy(redis, 6379);66 final Jedis jedis = createJedis(proxy.getContainerIpAddress(), proxy.getProxyPort());67 jedis.set("somekey", "somevalue");68 assertEquals("access to the container works OK before cutting the connection", "somevalue", jedis.get("somekey"));69 // disableProxy {70 proxy.setConnectionCut(true);71 // for example, expect failure when the connection is cut72 assertThrows("calls fail when the connection is cut",73 JedisConnectionException.class, () -> {74 jedis.get("somekey");75 });76 proxy.setConnectionCut(false);77 // and with the connection re-established, expect success78 assertEquals("access to the container works OK after re-establishing the connection", "somevalue", jedis.get("somekey"));79 // }80 }81 @Test82 public void testMultipleProxiesCanBeCreated() {83 try (GenericContainer<?> secondRedis = new GenericContainer<>(REDIS_IMAGE)84 .withExposedPorts(6379)85 .withNetwork(network)) {86 secondRedis.start();87 final ToxiproxyContainer.ContainerProxy firstProxy = toxiproxy.getProxy(redis, 6379);88 final ToxiproxyContainer.ContainerProxy secondProxy = toxiproxy.getProxy(secondRedis, 6379);89 final Jedis firstJedis = createJedis(firstProxy.getContainerIpAddress(), firstProxy.getProxyPort());90 final Jedis secondJedis = createJedis(secondProxy.getContainerIpAddress(), secondProxy.getProxyPort());91 firstJedis.set("somekey", "somevalue");92 secondJedis.set("somekey", "somevalue");93 firstProxy.setConnectionCut(true);94 assertThrows("calls fail when the connection is cut, for only the relevant proxy",95 JedisConnectionException.class, () -> {96 firstJedis.get("somekey");97 });98 assertEquals("access via a different proxy is OK", "somevalue", secondJedis.get("somekey"));99 }100 }101 @Test102 public void testOriginalAndMappedPorts() {103 final ToxiproxyContainer.ContainerProxy proxy = toxiproxy.getProxy("hostname", 7070);104 // obtainProxiedHostAndPortForDifferentContainer {105 final String hostViaToxiproxy = TOXIPROXY_NETWORK_ALIAS;106 final int portViaToxiproxy = proxy.getOriginalProxyPort();107 // }108 assertEquals("host is correct", TOXIPROXY_NETWORK_ALIAS, hostViaToxiproxy);109 assertEquals("original port is correct", 8666, portViaToxiproxy);110 final ToxiproxyContainer.ContainerProxy proxy1 = toxiproxy.getProxy("hostname1", 8080);111 assertEquals("original port is correct", 8667, proxy1.getOriginalProxyPort());112 assertEquals("mapped port is correct", toxiproxy.getMappedPort(proxy1.getOriginalProxyPort()), proxy1.getProxyPort());113 final ToxiproxyContainer.ContainerProxy proxy2 = toxiproxy.getProxy("hostname2", 9090);114 assertEquals("original port is correct", 8668, proxy2.getOriginalProxyPort());115 assertEquals("mapped port is correct", toxiproxy.getMappedPort(proxy2.getOriginalProxyPort()), proxy2.getProxyPort());116 }117 private void checkCallWithLatency(Jedis jedis, final String description, int expectedMinLatency, long expectedMaxLatency) {118 final long start = System.currentTimeMillis();119 String s = jedis.get("somekey");120 final long end = System.currentTimeMillis();121 final long duration = end - start;122 assertEquals(format("access to the container %s works OK", description), "somevalue", s);123 assertTrue(format("%s there is at least %dms latency", description, expectedMinLatency), duration >= expectedMinLatency);124 assertTrue(format("%s there is no more than %dms latency", description, expectedMaxLatency), duration < expectedMaxLatency);125 }126 private static Jedis createJedis(String host, int port) {127 return new Jedis(host, port, Math.toIntExact(JEDIS_TIMEOUT.toMillis()));128 }129}...
createJedis
Using AI Code Generation
1import org.testcontainers.containers.ToxiproxyTest2import redis.clients.jedis.Jedis3val toxiproxy = ToxiproxyTest.createToxiproxy()4val redis = ToxiproxyTest.createJedis(toxiproxy)5redis.set("foo", "bar")6redis.get("foo")7toxiproxy.close()8redis.close()9import org.testcontainers.containers.ToxiproxyTest10import redis.clients.jedis.Jedis11val toxiproxy = ToxiproxyTest.createToxiproxy()12val redis = ToxiproxyTest.createJedis(toxiproxy)13redis.set("foo", "bar")14redis.get("foo")15toxiproxy.close()16redis.close()17import org.testcontainers.containers.ToxiproxyTest18import redis.clients.jedis.Jedis19val toxiproxy = ToxiproxyTest.createToxiproxy()20val redis = ToxiproxyTest.createJedis(toxiproxy)21redis.set("foo", "bar")22redis.get("foo")23toxiproxy.close()24redis.close()25import org.testcontainers.containers.ToxiproxyTest26import redis.clients.jedis.Jedis27val toxiproxy = ToxiproxyTest.createToxiproxy()28val redis = ToxiproxyTest.createJedis(toxiproxy)29redis.set("foo", "bar")30redis.get("foo")31toxiproxy.close()32redis.close()33import org.testcontainers.containers.ToxiproxyTest34import redis.clients.jedis.Jedis35val toxiproxy = ToxiproxyTest.createToxiproxy()36val redis = ToxiproxyTest.createJedis(toxiproxy)37redis.set("foo", "bar")38redis.get("foo")39toxiproxy.close()40redis.close()41import org.testcontainers.containers.ToxiproxyTest42import redis.clients.jedis.Jedis
createJedis
Using AI Code Generation
1ToxiproxyContainer toxiproxy = createToxiproxy();2Jedis jedis = createJedis(toxiproxy);3ToxiproxyContainer toxiproxy = createToxiproxy();4Jedis jedis = createJedis(toxiproxy);5ToxiproxyContainer toxiproxy = createToxiproxy();6Jedis jedis = createJedis(toxiproxy);7ToxiproxyContainer toxiproxy = createToxiproxy();8Jedis jedis = createJedis(toxiproxy);9ToxiproxyContainer toxiproxy = createToxiproxy();10Jedis jedis = createJedis(toxiproxy);11ToxiproxyContainer toxiproxy = createToxiproxy();12Jedis jedis = createJedis(toxiproxy);13ToxiproxyContainer toxiproxy = createToxiproxy();14Jedis jedis = createJedis(toxiproxy);15ToxiproxyContainer toxiproxy = createToxiproxy();16Jedis jedis = createJedis(toxiproxy);17ToxiproxyContainer toxiproxy = createToxiproxy();18Jedis jedis = createJedis(toxiproxy);19ToxiproxyContainer toxiproxy = createToxiproxy();20Jedis jedis = createJedis(toxiproxy);21ToxiproxyContainer toxiproxy = createToxiproxy();
createJedis
Using AI Code Generation
1jedis = ToxiproxyTest.createJedis("toxiproxy");2jedis = ToxiproxyTest.createJedis("toxiproxy", 6379);3jedis = ToxiproxyTest.createJedis("toxiproxy", 6379, "redis");4jedis = ToxiproxyTest.createJedis("toxiproxy", 6379, "redis", "redis");5jedis = ToxiproxyTest.createJedis("toxiproxy", 6379, "redis", "redis", "redis");6jedis = ToxiproxyTest.createJedis("toxiproxy", 6379, "redis", "redis", "redis", 0);
createJedis
Using AI Code Generation
1[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ testcontainers ---2package org.testcontainers.containers;3import static org.assertj.core.api.Assertions.assertThat;4import com.github.dockerjava.api.command.InspectContainerResponse;5import java.util.List;6import lombok.extern.slf4j.Slf4j;7import org.junit.After;8import org.junit.Before;9import org.junit.Test;10import org.testcontainers.containers.output.Slf4jLogConsumer;11import redis.clients.jedis.Jedis;12public class ToxiproxyTest {13 private ToxiproxyContainer toxiproxyContainer;14 private Jedis createJedis(ToxiproxyContainer.Proxy proxy) {15 return new Jedis(proxy.getContainerIpAddress(), proxy.getProxyPort());16 }17 public void setUp() {18 toxiproxyContainer = new ToxiproxyContainer();19 toxiproxyContainer.start();20 }21 public void tearDown() {
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!!