Skip to content

Commit

Permalink
Remove toxiproxy tests from redis and keydb
Browse files Browse the repository at this point in the history
  • Loading branch information
Fameing committed Sep 30, 2024
1 parent 35807c0 commit 8aab1aa
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
@SpringBootTest(
classes = BaseClusterEmbeddedKeyDbTest.TestConfiguration.class,
properties = {
"embedded.keydb.clustered=true",
"embedded.toxiproxy.proxies.keydb.enabled=true"
"embedded.keydb.clustered=true"
}
)
abstract class BaseClusterEmbeddedKeyDbTest extends BaseEmbeddedKeyDbTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,87 +1,53 @@
package com.playtika.testcontainer.keydb.standalone;

import com.playtika.testcontainer.keydb.BaseEmbeddedKeyDbTest;
import eu.rekawek.toxiproxy.model.ToxicDirection;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.testcontainers.containers.ToxiproxyContainer;

import java.util.concurrent.Callable;

import static com.playtika.testcontainer.keydb.KeyDbProperties.BEAN_NAME_EMBEDDED_KEYDB;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(
classes = BaseStandaloneEmbeddedKeyDbTest.TestConfiguration.class,
properties = {
"embedded.toxiproxy.proxies.keydb.enabled=true"
}
classes = BaseStandaloneEmbeddedKeyDbTest.TestConfiguration.class,
properties = {
"embedded.toxiproxy.proxies.keydb.enabled=false"
}
)
abstract class BaseStandaloneEmbeddedKeyDbTest extends BaseEmbeddedKeyDbTest {

@Autowired
ToxiproxyContainer.ContainerProxy keyDbContainerProxy;

@Test
public void shouldEmulateLatency() throws Exception {
ValueOperations<String, String> ops = template.opsForValue();

assertThat(durationOf(() -> ops.get("any")))
.isLessThan(100L);

keyDbContainerProxy.toxics().latency("latency", ToxicDirection.UPSTREAM, 1000);

assertThat(durationOf(() -> ops.get("any")))
.isGreaterThanOrEqualTo(1000L);

keyDbContainerProxy.toxics()
.get("latency").remove();

assertThat(durationOf(() -> ops.get("any")))
.isLessThan(100L);
}

@Test
public void shouldSetupDependsOnForAllClients() throws Exception {
String[] beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, RedisConnectionFactory.class);
assertThat(beanNamesForType)
.as("RedisConnectionFactory should be present")
.hasSize(1)
.contains("redisConnectionFactory");
asList(beanNamesForType).forEach(this::hasDependsOn);

beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, RedisTemplate.class);
assertThat(beanNamesForType)
.as("redisTemplates should be present")
.hasSize(2)
.contains("redisTemplate", "stringRedisTemplate");
asList(beanNamesForType).forEach(this::hasDependsOn);
}

private void hasDependsOn(String beanName) {
assertThat(beanFactory.getBeanDefinition(beanName).getDependsOn())
.isNotNull()
.isNotEmpty()
.contains(BEAN_NAME_EMBEDDED_KEYDB);
}

private static long durationOf(Callable<?> op) throws Exception {
long start = System.currentTimeMillis();
op.call();
return System.currentTimeMillis() - start;
}

@EnableAutoConfiguration
@Configuration
static class TestConfiguration {
}
@Test
public void shouldSetupDependsOnForAllClients() throws Exception {
String[] beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, RedisConnectionFactory.class);
assertThat(beanNamesForType)
.as("RedisConnectionFactory should be present")
.hasSize(1)
.contains("redisConnectionFactory");
asList(beanNamesForType).forEach(this::hasDependsOn);

beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, RedisTemplate.class);
assertThat(beanNamesForType)
.as("redisTemplates should be present")
.hasSize(2)
.contains("redisTemplate", "stringRedisTemplate");
asList(beanNamesForType).forEach(this::hasDependsOn);
}

private void hasDependsOn(String beanName) {
assertThat(beanFactory.getBeanDefinition(beanName).getDependsOn())
.isNotNull()
.isNotEmpty()
.contains(BEAN_NAME_EMBEDDED_KEYDB);
}

@EnableAutoConfiguration
@Configuration
static class TestConfiguration {
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spring.data.redis.host=${embedded.keydb.toxiproxy.host}
spring.data.redis.port=${embedded.keydb.toxiproxy.port}
spring.data.redis.host=${embedded.keydb.host}
spring.data.redis.port=${embedded.keydb.port}

spring.data.redis.timeout=2500

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
@SpringBootTest(
classes = BaseClusterEmbeddedRedisTest.TestConfiguration.class,
properties = {
"embedded.redis.clustered=true",
"embedded.toxiproxy.proxies.redis.enabled=true"
"embedded.redis.clustered=true"
}
)
public abstract class BaseClusterEmbeddedRedisTest extends BaseEmbeddedRedisTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,23 @@
package com.playtika.testcontainer.redis.standalone;

import com.playtika.testcontainer.redis.BaseEmbeddedRedisTest;
import eu.rekawek.toxiproxy.model.ToxicDirection;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.testcontainers.containers.ToxiproxyContainer;

import java.util.concurrent.Callable;

import static com.playtika.testcontainer.redis.RedisProperties.BEAN_NAME_EMBEDDED_REDIS;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(
classes = BaseStandaloneEmbeddedRedisTest.TestConfiguration.class,
properties = {
"embedded.toxiproxy.proxies.redis.enabled=true"
}
classes = BaseStandaloneEmbeddedRedisTest.TestConfiguration.class
)
public abstract class BaseStandaloneEmbeddedRedisTest extends BaseEmbeddedRedisTest {

@Autowired
ToxiproxyContainer.ContainerProxy redisContainerProxy;

@Test
public void shouldEmulateLatency() throws Exception {
ValueOperations<String, String> ops = template.opsForValue();

assertThat(durationOf(() -> ops.get("any")))
.isLessThan(100L);

redisContainerProxy.toxics().latency("latency", ToxicDirection.UPSTREAM, 1000);

assertThat(durationOf(() -> ops.get("any")))
.isGreaterThanOrEqualTo(1000L);

redisContainerProxy.toxics()
.get("latency").remove();

assertThat(durationOf(() -> ops.get("any")))
.isLessThan(100L);
}

@Test
public void shouldSetupDependsOnForAllClients() throws Exception {
String[] beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, RedisConnectionFactory.class);
Expand All @@ -73,12 +42,6 @@ private void hasDependsOn(String beanName) {
.contains(BEAN_NAME_EMBEDDED_REDIS);
}

private static long durationOf(Callable<?> op) throws Exception {
long start = System.currentTimeMillis();
op.call();
return System.currentTimeMillis() - start;
}

@EnableAutoConfiguration
@Configuration
static class TestConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spring.data.redis.host=${embedded.redis.toxiproxy.host}
spring.data.redis.port=${embedded.redis.toxiproxy.port}
spring.data.redis.host=${embedded.redis.host}
spring.data.redis.port=${embedded.redis.port}

spring.data.redis.timeout=2500

0 comments on commit 8aab1aa

Please sign in to comment.