Browse Source

Merge pull request #8149 from smatvienko-tb/feature/redis-testcontainers

[3.5] Redis test suite on testcontainers
pull/8162/head
Andrew Shvayka 4 years ago
committed by GitHub
parent
commit
9482390394
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      common/cache/src/main/java/org/thingsboard/server/cache/RedisTbTransactionalCache.java
  2. 2
      dao/src/main/java/org/thingsboard/server/dao/attributes/CachedAttributesService.java
  3. 51
      dao/src/test/java/org/thingsboard/server/dao/AbstractRedisContainer.java
  4. 24
      dao/src/test/java/org/thingsboard/server/dao/RedisSqlTestSuite.java
  5. 4
      dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceCredentialsCacheTest.java
  6. 3
      dao/src/test/resources/application-test.properties

4
common/cache/src/main/java/org/thingsboard/server/cache/RedisTbTransactionalCache.java

@ -108,6 +108,10 @@ public abstract class RedisTbTransactionalCache<K extends Serializable, V extend
@Override @Override
public void evict(Collection<K> keys) { public void evict(Collection<K> keys) {
//Redis expects at least 1 key to delete. Otherwise - ERR wrong number of arguments for 'del' command
if (keys.isEmpty()) {
return;
}
try (var connection = connectionFactory.getConnection()) { try (var connection = connectionFactory.getConnection()) {
connection.del(keys.stream().map(this::getRawKey).toArray(byte[][]::new)); connection.del(keys.stream().map(this::getRawKey).toArray(byte[][]::new));
} }

2
dao/src/main/java/org/thingsboard/server/dao/attributes/CachedAttributesService.java

@ -66,7 +66,7 @@ public class CachedAttributesService implements AttributesService {
private final TbTransactionalCache<AttributeCacheKey, AttributeKvEntry> cache; private final TbTransactionalCache<AttributeCacheKey, AttributeKvEntry> cache;
private ListeningExecutorService cacheExecutor; private ListeningExecutorService cacheExecutor;
@Value("${cache.type}") @Value("${cache.type:caffeine}")
private String cacheType; private String cacheType;
public CachedAttributesService(AttributesDao attributesDao, public CachedAttributesService(AttributesDao attributesDao,

51
dao/src/test/java/org/thingsboard/server/dao/AbstractRedisContainer.java

@ -0,0 +1,51 @@
/**
* Copyright © 2016-2023 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.dao;
import lombok.extern.slf4j.Slf4j;
import org.junit.ClassRule;
import org.junit.rules.ExternalResource;
import org.testcontainers.containers.GenericContainer;
import java.util.List;
@Slf4j
public class AbstractRedisContainer {
@ClassRule(order = 0)
public static GenericContainer redis = new GenericContainer("redis:7.0")
.withExposedPorts(6379);
@ClassRule(order = 1)
public static ExternalResource resource = new ExternalResource() {
@Override
protected void before() throws Throwable {
redis.start();
System.setProperty("cache.type", "redis");
System.setProperty("redis.connection.type", "standalone");
System.setProperty("redis.standalone.host", redis.getHost());
System.setProperty("redis.standalone.port", String.valueOf(redis.getMappedPort(6379)));
}
@Override
protected void after() {
redis.stop();
List.of("cache.type", "redis.connection.type", "redis.standalone.host", "redis.standalone.port")
.forEach(System.getProperties()::remove);
}
};
}

24
dao/src/test/java/org/thingsboard/server/dao/RedisSqlTestSuite.java

@ -15,37 +15,15 @@
*/ */
package org.thingsboard.server.dao; package org.thingsboard.server.dao;
import org.junit.ClassRule;
import org.junit.extensions.cpsuite.ClasspathSuite; import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.ClassnameFilters; import org.junit.extensions.cpsuite.ClasspathSuite.ClassnameFilters;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.testcontainers.containers.GenericContainer;
@ContextConfiguration(initializers = RedisSqlTestSuite.class)
@RunWith(ClasspathSuite.class) @RunWith(ClasspathSuite.class)
@ClassnameFilters( @ClassnameFilters(
//All the same tests using redis instead of caffeine. //All the same tests using redis instead of caffeine.
"org.thingsboard.server.dao.service.*ServiceSqlTest" "org.thingsboard.server.dao.service.*ServiceSqlTest"
) )
public class RedisSqlTestSuite implements ApplicationContextInitializer<ConfigurableApplicationContext> { public class RedisSqlTestSuite extends AbstractRedisContainer {
@ClassRule
public static GenericContainer redis = new GenericContainer("redis:4.0").withExposedPorts(6379);
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext, "cache.type=redis");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext, "redis.connection.type=standalone");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext, "redis.standalone.host=localhost");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext, "redis.standalone.port=" + redis.getMappedPort(6379));
}
} }

4
dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceCredentialsCacheTest.java

@ -43,8 +43,8 @@ import static org.mockito.Mockito.when;
public abstract class BaseDeviceCredentialsCacheTest extends AbstractServiceTest { public abstract class BaseDeviceCredentialsCacheTest extends AbstractServiceTest {
private static final String CREDENTIALS_ID_1 = StringUtils.randomAlphanumeric(20); private final String CREDENTIALS_ID_1 = StringUtils.randomAlphanumeric(20);
private static final String CREDENTIALS_ID_2 = StringUtils.randomAlphanumeric(20); private final String CREDENTIALS_ID_2 = StringUtils.randomAlphanumeric(20);
@Autowired @Autowired
private DeviceCredentialsService deviceCredentialsService; private DeviceCredentialsService deviceCredentialsService;

3
dao/src/test/resources/application-test.properties

@ -7,10 +7,9 @@ updates.enabled=false
audit-log.enabled=true audit-log.enabled=true
audit-log.sink.type=none audit-log.sink.type=none
cache.type=caffeine #cache.type=caffeine # will be injected redis by RedisContainer or will be default (caffeine)
cache.maximumPoolSize=16 cache.maximumPoolSize=16
cache.attributes.enabled=true cache.attributes.enabled=true
#cache.type=redis
cache.specs.relations.timeToLiveInMinutes=1440 cache.specs.relations.timeToLiveInMinutes=1440
cache.specs.relations.maxSize=100000 cache.specs.relations.maxSize=100000

Loading…
Cancel
Save