How to use toxics method of org.testcontainers.containers.ToxiproxyContainer class

Best Testcontainers-java code snippet using org.testcontainers.containers.ToxiproxyContainer.toxics

Source:SmartRouterSystemTest.java Github

copy

Full Screen

...241 return containers.has("result") ? 242 containers.getJSONObject("result").getJSONObject("kie-containers").getJSONArray("kie-container").length() : 0;243 }244 private void removeAllToxics(ToxiproxyContainer.ContainerProxy proxy) throws IOException {245 for (Toxic t : proxy.toxics().getAll()) {246 t.remove();247 }248 }249 250 251 private static Stream<Arguments> provideToxics() {252 ToxicSupplier<Toxic, IOException> limitData1 = () -> proxy1.toxics().limitData("limitData", DOWNSTREAM, 5);253 ToxicSupplier<Toxic, IOException> resetPeer1 = () -> proxy1.toxics().resetPeer("resetPeer", DOWNSTREAM, 0);254 ToxicSupplier<Toxic, IOException> timeout1 = () -> proxy1.toxics().timeout("timeout", DOWNSTREAM, getRandomTimeout(0,3000));255 256 ToxicSupplier<Toxic, IOException> limitData3 = () -> proxy3.toxics().limitData("limitData", DOWNSTREAM, 5);257 ToxicSupplier<Toxic, IOException> resetPeer3 = () -> proxy3.toxics().resetPeer("resetPeer", DOWNSTREAM, 0);258 ToxicSupplier<Toxic, IOException> timeout3 = () -> proxy3.toxics().timeout("timeout", DOWNSTREAM, getRandomTimeout(2000,5000));259 260 return Stream.of(Arguments.of(limitData1, null),261 Arguments.of(resetPeer1, null),262 Arguments.of(timeout1, null),263 Arguments.of(null, limitData3),264 Arguments.of(null, resetPeer3),265 Arguments.of(null, timeout3),266 Arguments.of(limitData1, limitData3),267 Arguments.of(limitData1, resetPeer3),268 Arguments.of(limitData1, timeout3),269 Arguments.of(resetPeer1, limitData3),270 Arguments.of(resetPeer1, resetPeer3),271 Arguments.of(resetPeer1, timeout3),272 Arguments.of(timeout1, limitData3),273 Arguments.of(timeout1, resetPeer3),274 Arguments.of(timeout1, timeout3),275 Arguments.of(null, null));276 }277 278 public static int getRandomTimeout(int min, int max) {279 Random random = new Random();280 int timeout = random.ints(min, max).findFirst().getAsInt();281 logger.debug("random timeout is {} ms", timeout);282 return timeout;283 }284 285 @ParameterizedTest286 @MethodSource("provideToxics")287 void shouldUpdateRoutingTableWhenDisconnectedAndReconnected(ToxicSupplier<Toxic, IOException> toxic1, 288 ToxicSupplier<Toxic, IOException> toxic3) throws Exception {289 logger.debug("*** Cut Connection ***");290 291 cutConnection(toxic1, toxic3);292 293 logger.debug("*** Reestablish Connection ***");294 removeAllToxics(proxy1);295 removeAllToxics(proxy3);296 297 await().atMost(ONE_MINUTE).untilAsserted(() -> assertThat(readNumberOfContainers()).isEqualTo(2));298 299 await().atMost(ONE_MINUTE).until(() -> assertServersInRoutingTable(URL_NODE1, URL_NODE3));300 }301 @ParameterizedTest302 @MethodSource("provideToxics")303 void shouldStopPollingWhenReconnectionLimitIsReached(ToxicSupplier<Toxic, IOException> toxic1, 304 ToxicSupplier<Toxic, IOException> toxic3) throws Exception {305 306 logger.debug("*** Cut Connection ***");307 TestFixture fixture = cutConnection(toxic1, toxic3);308 309 if (!fixture.exercisedToxics.isEmpty()) {310 logger.debug("*** Waiting for max_reach ***");311 consumerSmartRouter.waitUntil(frame -> frame.getUtf8String().contains(REACHED_ATTEMPTS_LIMIT), 312 50, TimeUnit.SECONDS, 2-fixture.expectedContainers+reachedLimitCounter);313 }314 315 logger.debug("*** Reestablish Connection ***");316 removeAllToxics(proxy1);317 removeAllToxics(proxy3);318 319 await().atMost(ONE_MINUTE).untilAsserted(() -> assertThat(readNumberOfContainers()).isEqualTo(fixture.expectedContainers));320 321 await().atMost(ONE_MINUTE).until(() -> assertServersInRoutingTable(fixture.expectedServers[1], fixture.expectedServers[3]));322 }323 private TestFixture cutConnection(ToxicSupplier<Toxic, IOException> toxic1, ToxicSupplier<Toxic, IOException> toxic3)324 throws IOException {325 TestFixture fixture = getTestFixture(toxic1, toxic3);326 327 logger.debug("Exercising with toxics:{}", fixture.exercisedToxics);328 logger.debug("expectedContainers:{} and expectedServer1:{} and expectedServer3:{}", 329 fixture.expectedContainers, fixture.expectedServers[1], fixture.expectedServers[3]);330 331 await().atMost(ONE_MINUTE).untilAsserted(() -> assertThat(readNumberOfContainers()).isEqualTo(fixture.expectedContainers));332 333 final String expected1 = fixture.expectedServers[1];334 final String expected3 = fixture.expectedServers[3];335 336 await().atMost(ONE_MINUTE).until(() -> assertServersInRoutingTable(expected1, expected3));337 338 return fixture;339 }340 341 private TestFixture getTestFixture(ToxicSupplier<Toxic, IOException> toxic1, ToxicSupplier<Toxic, IOException> toxic3) throws IOException {...

Full Screen

Full Screen

Source:EdgeTestBase.java Github

copy

Full Screen

...74 log.info("Cutting connection on {} image.", img.getName());75 CompletableFuture.runAsync(() -> {76 try {77 log.info("Adding CUT_CONNECTION_DOWNSTREAM toxic");78 img.proxy.toxics().limitData("CUT_CONNECTION_DOWNSTREAM", ToxicDirection.DOWNSTREAM, 0L);79 log.info("Adding CUT_CONNECTION_UPSTREAM toxic");80 img.proxy.toxics().limitData("CUT_CONNECTION_UPSTREAM", ToxicDirection.UPSTREAM, 0L);81 } catch (IOException e) {82 log.error("Could not add Toxic, ", e);83 throw new CompletionException(e);84 }85 }).get(TIMEOUT_IT, TimeUnit.MILLISECONDS);86 img.isCurrentlyCut.set(true);87 } else if (!cut && img.isCurrentlyCut.get()) {88 log.info("Uncutting connection on {} image.", img.getName());89 removeWithTimeout(img.proxy.toxics().get("CUT_CONNECTION_DOWNSTREAM"), 0);90 removeWithTimeout(img.proxy.toxics().get("CUT_CONNECTION_UPSTREAM"), 0);91 img.isCurrentlyCut.set(false);92 } else {93 log.info("{} Image is already in the desired state.", img.getName());94 }95 } catch (IOException | InterruptedException | TimeoutException | ExecutionException e) {96 throw new RuntimeException("Could not control proxy", e);97 }98 }99 public static void removeWithTimeout (Toxic toxic, int counter) throws InterruptedException, TimeoutException, ExecutionException {100 try {101 CompletableFuture.runAsync(() -> {102 try {103 toxic.remove();104 } catch (IOException e) {...

Full Screen

Full Screen

Source:PulsarContainerTest.java Github

copy

Full Screen

...42 pulsar.stop();43 network.close();44 }45 protected void testPulsarFunctionality(String pulsarBrokerUrl) throws Exception {46 pulsarProxy.toxics()47 .latency("latencyDownstream", ToxicDirection.DOWNSTREAM, 1_100)48 .setJitter(150);49 pulsarProxy.toxics()50 .latency("latencyUpstream", ToxicDirection.UPSTREAM, 2_100)51 .setJitter(200);52 @Cleanup53 final PulsarClient client = PulsarClient.builder()54 .serviceUrl(pulsarBrokerUrl)55 .build();56 @Cleanup57 final Consumer<String> consumer = client.newConsumer(Schema.STRING)58 .topic(TEST_TOPIC)59 .subscriptionName("test-subs")60 .subscribe();61 @Cleanup62 final Producer<String> producer = client.newProducer(Schema.STRING)63 .topic(TEST_TOPIC)...

Full Screen

Full Screen

toxics

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import com.github.dockerjava.api.DockerClient;3import com.github.dockerjava.api.command.CreateContainerCmd;4import com.github.dockerjava.api.command.CreateContainerResponse;5import com.github.dockerjava.api.command.ExecCreateCmdResponse;6import com.github.dockerjava.api.command.InspectContainerResponse;7import com.github.dockerjava.api.command.InspectExecResponse;8import com.github.dockerjava.api.model.Container;9import com.github.dockerjava.api.model.ExposedPort;10import com.github.dockerjava.api.model.Link;11import com.github.dockerjava.api.model.Ports.Binding;12import com.github.dockerjava.api.model.Ports.Binding;13import com.github.dockerjava.api.model.Ports;14import com.github.dockerjava.core.command.ExecStartResultCallback;15import com.github

Full Screen

Full Screen

toxics

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.ToxiproxyContainer;4import org.testcontainers.containers.output.Slf4jLogConsumer;5import org.testcontainers.containers.wait.strategy.Wait;6import org.testcontainers.utility.DockerImageName;7import org.testcontainers.utility.MountableFile;8import java.io.IOException;9import static org.junit.Assert.*;10public class 1 {11 public void testToxiproxy() throws IOException {12 ToxiproxyContainer toxiproxy = new ToxiproxyContainer(DockerImageName.parse("shopify/toxiproxy:2.1.4"));13 toxiproxy.start();14 GenericContainer webServer = new GenericContainer(DockerImageName.parse("httpd:2.4.39-alpine"))15 .withExposedPorts(80)16 .withCopyFileToContainer(MountableFile.forClasspathResource("index.html"), "/usr/local/apache2/htdocs/index.html")17 .waitingFor(Wait.forHttp("/").forStatusCode(200));18 webServer.start();19 GenericContainer webClient = new GenericContainer(DockerImageName.parse("alpine:3.12.1"))20 .withLogConsumer(new Slf4jLogConsumer(org.slf4j.LoggerFactory.getLogger("webClient")))21 .withNetwork(toxiproxy.getNetwork())22 .withNetworkAliases("webserver")23 .withEnv("http_proxy", toxiproxy.getProxy(webServer, 80).getListenAddress());24 webClient.start();25 toxiproxy.getProxy(webServer, 80).toxics().latency("latency", ToxiproxyContainer.ToxicDirection.DOWNSTREAM, 10000);26 Thread.sleep(60000);27 toxiproxy.stop();28 }29}30The testToxiproxy() method creates a toxiproxy container, a web server container, and a web client container. The web server container is exposed

Full Screen

Full Screen

toxics

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.ToxiproxyContainer;2import org.testcontainers.containers.ToxiproxyContainer.ContainerProxy;3import org.testcontainers.containers.ToxiproxyContainer.ToxicDirection;4import org.testcontainers.containers.ToxiproxyContainer.ToxicList;5import org.testcontainers.containers.ToxiproxyContainer.ToxicProxy;6public class ToxiproxyContainerTest {7 public static void main(String[] args) {8 ToxiproxyContainer toxiproxyContainer = new ToxiproxyContainer()9 .withProxy("http", 8080, 8081)10 .withProxy("https", 8082, 8083);11 toxiproxyContainer.start();12 ContainerProxy containerProxy = toxiproxyContainer.getProxy("http");13 ToxicProxy toxicProxy = containerProxy.toxics();14 ToxicList toxicList = toxicProxy.list();15 toxicList.getToxics();16 toxicProxy.latency("latency", ToxicDirection.DOWNSTREAM, 1000);17 toxicProxy.bandwidth("bandwidth", ToxicDirection.DOWNSTREAM, 1000);18 toxicProxy.slowClose("slow_close", ToxicDirection.DOWNSTREAM, 1000);19 toxicProxy.timeout("timeout", ToxicDirection.DOWNSTREAM, 1000);20 toxicProxy.slicer("slicer", ToxicDirection.DOWNSTREAM, 1000, 1000, 1000);21 toxicProxy.limitData("limit_data", ToxicDirection.DOWNSTREAM, 1000);22 toxicProxy.shuffle("shuffle", ToxicDirection.DOWNSTREAM);

Full Screen

Full Screen

toxics

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import com.github.dockerjava.api.model.ContainerNetwork;3import com.github.dockerjava.api.model.Network.Ipam;4import com.github.dockerjava.api.model.Network.Ipam.Config;5import com.github.dockerjava.api.model.Network.Ipam.Config.Builder;6import org.testcontainers.containers.wait.strategy.Wait;7import org.testcontainers.utility.Base58;8import org.testcontainers.utility.DockerImageName;9import java.util.Collections;10import java.util.HashMap;11import java.util.Map;12import java.util.Map.Entry;

Full Screen

Full Screen

toxics

Using AI Code Generation

copy

Full Screen

1import java.io.IOException;2import java.util.concurrent.TimeoutException;3import org.testcontainers.containers.ToxiproxyContainer;4import org.testcontainers.containers.ToxiproxyContainer.ContainerProxy;5import org.testcontainers.containers.ToxiproxyContainer.ToxicDirection;6import org.testcontainers.containers.ToxiproxyContainer.ToxicList;7import org.testcontainers.containers.ToxiproxyContainer.ToxicType;8public class 1 {9 public static void main(String[] args) throws IOException, TimeoutException {10 ToxiproxyContainer toxiproxy = new ToxiproxyContainer();11 toxiproxy.start();12 ContainerProxy proxy = toxiproxy.getProxy(toxiproxy.getContainerIpAddress(), 6379);13 ToxicList toxicList = proxy.toxics().list();14 toxicList.add(ToxicType.SLOW_CLOSE, ToxicDirection.DOWNSTREAM, 10000, 0);15 System.out.println(toxicList);16 }17}18ToxicList{toxics=[SlowCloseToxic{attributes=SlowCloseToxicAttributes{rate=10000, d

Full Screen

Full Screen

toxics

Using AI Code Generation

copy

Full Screen

1ToxiproxyContainer toxiproxy = new ToxiproxyContainer();2toxiproxy.start();3Proxy proxy = toxiproxy.getProxy(container, 8080);4Latency latency = proxy.addLatency(ToxiproxyContainer.ToxicDirection.DOWNSTREAM, 1000);5latency.remove();6proxy.stop();7toxiproxy.stop();8Network network = Network.newNetwork();9ToxiproxyContainer toxiproxy = new ToxiproxyContainer();10toxiproxy.start();11Proxy proxy = toxiproxy.getProxy(container, 8080);12Latency latency = proxy.addLatency(ToxiproxyContainer.ToxicDirection.DOWNSTREAM, 1000);13latency.remove();14proxy.stop();15toxiproxy.stop();16network.close();17ToxiproxyContainer toxiproxy = new ToxiproxyContainer();18toxiproxy.start();19Proxy proxy = toxiproxy.getProxy(container, 8080);20Latency latency = proxy.addLatency(ToxiproxyContainer.ToxicDirection.DOWNSTREAM, 1000);21latency.remove();22proxy.stop();23toxiproxy.stop();24ToxiproxyContainer toxiproxy = new ToxiproxyContainer();25toxiproxy.start();26Proxy proxy = toxiproxy.getProxy(container, 8080);27Latency latency = proxy.addLatency(ToxiproxyContainer.ToxicDirection.DOWNSTREAM, 1000);28latency.remove();29proxy.stop();30toxiproxy.stop();

Full Screen

Full Screen

toxics

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import org.testcontainers.containers.ToxiproxyContainer.ContainerProxy;3public class toxics {4 public static void main(String[] args) throws Exception {5 try (ToxiproxyContainer toxiproxy = new ToxiproxyContainer()) {6 toxiproxy.start();7 ContainerProxy proxy = toxiproxy.getProxy(toxiproxy.getContainerIpAddress(), toxiproxy.getProxyPort(80));8 proxy.toxics().latency("latency", ToxicDirection.DOWNSTREAM, 10000);9 }10 }11}12package org.testcontainers.containers;13import org.testcontainers.containers.ToxiproxyContainer.ContainerProxy;14public class toxics {15 public static void main(String[] args) throws Exception {16 try (ToxiproxyContainer toxiproxy = new ToxiproxyContainer()) {17 toxiproxy.start();18 ContainerProxy proxy = toxiproxy.getProxy(toxiproxy.getContainerIpAddress(), toxiproxy.getProxyPort(80));19 proxy.toxics().latency("latency", ToxicDirection.DOWNSTREAM, 10000);20 }21 }22}23package org.testcontainers.containers;24import org.testcontainers.containers.ToxiproxyContainer.ContainerProxy;25public class toxics {26 public static void main(String[] args) throws Exception {27 try (ToxiproxyContainer toxiproxy = new ToxiproxyContainer()) {28 toxiproxy.start();29 ContainerProxy proxy = toxiproxy.getProxy(toxiproxy.getContainerIpAddress(), toxiproxy.getProxyPort(80));30 proxy.toxics().latency("latency", ToxicDirection.DOWNSTREAM, 10000);31 }32 }33}34package org.testcontainers.containers;35import org.testcontainers.containers.ToxiproxyContainer.ContainerProxy;36public class toxics {

Full Screen

Full Screen

toxics

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.ToxiproxyContainer;2import org.testcontainers.utility.DockerImageName;3public class TestToxiproxy {4 public static void main(String[] args) throws Exception {5 try (ToxiproxyContainer toxiproxy = new ToxiproxyContainer(DockerImageName.parse("shopify/toxiproxy:2.1.4"))) {6 toxiproxy.start();7 ToxiproxyContainer.ContainerProxy proxy = toxiproxy.getProxy(toxiproxy.getContainerIpAddress(),8 toxiproxy.getProxyPort());9 proxy.toxics().latency("latency", ToxiproxyContainer.ToxicDirection.DOWNSTREAM, 10000, 1000);10 }11 }12}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful