Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToxiproxyContainer.ContainerProxy deprecated #470

Closed
azevedoedison opened this issue Jan 4, 2023 · 3 comments
Closed

ToxiproxyContainer.ContainerProxy deprecated #470

azevedoedison opened this issue Jan 4, 2023 · 3 comments
Labels

Comments

@azevedoedison
Copy link

azevedoedison commented Jan 4, 2023

Hi, I did an implementation using ToxiproxyContainer.ContainerProxy and it works easily. However, the method is deprecated and I didn't find an example of how to use of new way. How can I create a proxy?

Look my example:

public abstract class CassandraTestBase {

	protected CassandraTestBase() {
	}

	private static final Network network = Network.newNetwork();

	@Container
	public static final CassandraContainer<?> cassandra = new CassandraContainer<>("cassandra:3.11.2")
			.withNetwork(network).withReuse(true);

	@Container
	public static final ToxiproxyContainer toxiproxy = new ToxiproxyContainer("ghcr.io/shopify/toxiproxy:2.5.0")
			.withNetwork(network);
	
	public static ToxiproxyContainer.ContainerProxy proxy;

	private static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS local WITH replication = { 'class': 'SimpleStrategy', 'replication_factor':'1' };";
	private static final String KEYSPACE_ACTIVATE_QUERY = "USE local;";
	private static final String LOCAL = "local";
	private static CqlSession session;
	private static String keyspace = "";
	/** Datacenter used for local-test profile. */
	private static String datacenter = "datacenter1";
	private static final int REQUEST_TIMEOUT = 12000;

	@BeforeAll
	public static void startCassandraContainer() {
		proxy = toxiproxy.getProxy(cassandra, 9042);
		TestcontainersConfiguration.getInstance().updateUserConfig("testcontainers.reuse.enable", "true");
		System.setProperty("spring.data.cassandra.contact-points", proxy.getContainerIpAddress());
		System.setProperty("spring.data.cassandra.port", String.valueOf(proxy.getProxyPort()));
		System.setProperty("spring.data.cassandra.local-datacenter", datacenter);
		assumeTrue(DockerClientFactory.instance().isDockerAvailable());

		session = CqlSession.builder()
				.addContactPoint(new InetSocketAddress(proxy.getContainerIpAddress(), proxy.getProxyPort()))
				.withLocalDatacenter(datacenter)
				.withConfigLoader(DriverConfigLoader.programmaticBuilder()
						.withDuration(DefaultDriverOption.METADATA_SCHEMA_REQUEST_TIMEOUT,
								Duration.ofMillis(REQUEST_TIMEOUT))
						.withDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT,
								Duration.ofMillis(REQUEST_TIMEOUT))
						.withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofMillis(REQUEST_TIMEOUT)).build())
				.build();

	}

	/**
	 * Creates the keyspace from the test resource file of the api
	 * 
	 * @param profile - Active profile used
	 * @param loader  Class
	 */
	public static void createKeyspace(String profile, Class<?> loader) {
		try (InputStream input = loader.getClassLoader()
				.getResourceAsStream("application-" + profile + ".properties")) {
			Properties applicationPoperties = new Properties();
			applicationPoperties.load(input);

			keyspace = applicationPoperties.getProperty("spring.data.cassandra.keyspace-name");

		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
		getSession().execute(KEYSPACE_CREATION_QUERY.replace(LOCAL, keyspace));
		getSession().execute(KEYSPACE_ACTIVATE_QUERY.replace(LOCAL, keyspace));
	}

	public static CqlSession getSession() {
		return session;
	}

}

And my test (toxics are deprecated too):

@DisplayName("Shouldn't update Timestamps when connection will be lost")
void testInsertDataWithToxic() throws Exception {	
		proxy.toxics().bandwidth("CUT_CONNECTION_DOWNSTREAM", ToxicDirection.DOWNSTREAM, 0);
		proxy.toxics().bandwidth("CUT_CONNECTION_UPSTREAM", ToxicDirection.UPSTREAM, 0);

		assertThrows(CassandraUncategorizedException.class, () -> {
			userRepo.updateTimestamps();
		});

		proxy.toxics().get("CUT_CONNECTION_DOWNSTREAM").remove();
		proxy.toxics().get("CUT_CONNECTION_UPSTREAM").remove();
	}
}
@miry
Copy link
Contributor

miry commented Jan 4, 2023

Hi. I believe it is part of https://github.com/trekawek/toxiproxy-java

@miry miry added the question label Jan 4, 2023
@eddumelendez
Copy link

You can check the docs

@miry
Copy link
Contributor

miry commented Jan 23, 2023

I am closing this, as it was provided links to the doc in Testcontainers and library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants