Skip to content

Commit

Permalink
test: Updated config manager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed Feb 7, 2023
1 parent a35ca32 commit 9f61b80
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.util.Collection;

import org.awaitility.Awaitility;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;

import com.redis.lettucemod.api.StatefulRedisModulesConnection;
import com.redis.testcontainers.RedisServer;
import com.redis.testcontainers.RedisStackContainer;
import com.redis.testcontainers.junit.AbstractTestcontainersRedisTestBase;
Expand All @@ -26,17 +26,17 @@ protected Collection<RedisServer> redisServers() {

@ParameterizedTest
@RedisTestContextsSource
void testDriver(RedisTestContext redis) throws Exception {
BootstrapConfig bootstrap = new BootstrapConfig();
bootstrap.getRedis().setCluster(redis.isCluster());
try (StatefulRedisModulesConnection<String, String> connection = redis.getConnection();
ConfigManager configManager = new ConfigManager()) {
String key = bootstrap.getRedis().key("config");
Config config = configManager.fetchConfig(connection, key, Duration.ofSeconds(1), new Config());
Awaitility.await().until(() -> redis.sync().jsonGet(key) != null);
int bufferSize = 123456890;
redis.sync().jsonSet(key, ".bufferSize", String.valueOf(bufferSize));
Awaitility.await().until(() -> config.getBufferSize() == bufferSize);
void configUpdated(RedisTestContext redis) throws Exception {
try (ConfigManager<RulesConfig> configManager = new ConfigManager<>(redis.getConnection(),
Duration.ofMillis(100))) {
String key = "testUpdate";
RulesConfig config = new RulesConfig();
configManager.register(key, config);
Assertions.assertNotNull(redis.sync().jsonGet(key));
long ttl = 123;
redis.sync().jsonSet(key, ".rules[0].ttl", String.valueOf(ttl));
Awaitility.await().timeout(Duration.ofMillis(300))
.until(() -> config.getRules().size() == 1 && config.getRules().get(0).getTtl() == ttl);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.redis.sidecar;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.redis.sidecar.RulesConfig.RuleConfig;

class RulesConfigTests {

@Test
void propertyChangeListener() throws Exception {
RulesConfig config = new RulesConfig();
EventList eventList = new EventList();
config.addPropertyChangeListener(eventList);
RuleConfig newRule = RuleConfig.builder().tables(Arrays.asList("table1")).build();
config.setRules(Arrays.asList(newRule));
Assertions.assertEquals(1, eventList.getEvents().size());
Assertions.assertEquals(RulesConfig.PROPERTY_RULES, eventList.getEvents().get(0).getPropertyName());
}

class EventList implements PropertyChangeListener {

private final List<PropertyChangeEvent> events = new ArrayList<>();

@Override
public void propertyChange(PropertyChangeEvent evt) {
events.add(evt);
}

public List<PropertyChangeEvent> getEvents() {
return events;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.redis.sidecar;

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
Expand Down Expand Up @@ -30,7 +31,7 @@ protected Collection<RedisServer> redisServers() {
@RedisTestContextsSource
void testDriver(RedisTestContext redis) throws SQLException, ClassNotFoundException {
Class.forName(SidecarDriver.class.getName());
java.sql.Driver driver = DriverManager.getDriver("jdbc:" + redis.getRedisURI());
Driver driver = DriverManager.getDriver("jdbc:" + redis.getRedisURI());
Assert.assertNotNull(driver);
Assert.assertTrue(driver.getMajorVersion() >= 0);
Assert.assertTrue(driver.getMinorVersion() >= 0);
Expand Down

0 comments on commit 9f61b80

Please sign in to comment.