use testcontainers to start memcached via docker for testing real remote memcached server

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>
This commit is contained in:
olivier lamy 2020-05-18 15:35:19 +10:00 committed by Olivier Lamy
parent de6273baae
commit a78d2f475d
4 changed files with 60 additions and 4 deletions

View File

@ -24,7 +24,6 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -11,6 +11,7 @@
<url>http://www.eclipse.org/jetty</url>
<properties>
<bundle-symbolic-name>${project.groupId}.sessions.memcached</bundle-symbolic-name>
<memcached.docker.version></memcached.docker.version>
</properties>
<build>
<plugins>
@ -65,13 +66,22 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>memcached</id>
<id>remote-session-tests</id>
<activation>
<property>
<name>memcached.enabled</name>

View File

@ -19,11 +19,13 @@
package org.eclipse.jetty.memcached.sessions;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import org.eclipse.jetty.memcached.session.MemcachedSessionDataMapFactory;
import org.eclipse.jetty.server.session.AbstractSessionDataStore;
import org.eclipse.jetty.server.session.AbstractSessionDataStoreFactory;
@ -32,6 +34,10 @@ import org.eclipse.jetty.server.session.SessionData;
import org.eclipse.jetty.server.session.SessionDataStore;
import org.eclipse.jetty.server.session.SessionDataStoreFactory;
import org.eclipse.jetty.server.session.SessionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
/**
* MemcachedTestHelper
@ -132,11 +138,49 @@ public class MemcachedTestHelper
}
}
private static final Logger LOG = LoggerFactory.getLogger(MemcachedTestHelper.class);
private static final Logger MEMCACHED_LOG = LoggerFactory.getLogger("org.eclipse.jetty.memcached.sessions.MemcachedLogs");
static GenericContainer memcached =
new GenericContainer("memcached:" + System.getProperty("memcached.docker.version", "1.6.6"))
.withLogConsumer(new Slf4jLogConsumer(MEMCACHED_LOG));
static
{
try
{
long start = System.currentTimeMillis();
memcached.start();
LOG.info("time to start memcache instance {}ms on {}:{}", System.currentTimeMillis() - start,
memcached.getContainerIpAddress(), memcached.getMappedPort(11211));
}
catch (Exception e)
{
LOG.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
public static SessionDataStoreFactory newSessionDataStoreFactory()
{
MockDataStoreFactory storeFactory = new MockDataStoreFactory();
MemcachedSessionDataMapFactory mapFactory = new MemcachedSessionDataMapFactory();
mapFactory.setAddresses(new InetSocketAddress("localhost", 11211));
String host = memcached.getContainerIpAddress();
int port = memcached.getMappedPort(11211);
InetSocketAddress inetSocketAddress = new InetSocketAddress(host, port);
mapFactory.setAddresses(inetSocketAddress);
try
{
XMemcachedClientBuilder builder = new XMemcachedClientBuilder(Arrays.asList(inetSocketAddress));
builder.build().flushAll();
}
catch (Exception e)
{
LOG.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
CachingSessionDataStoreFactory factory = new CachingSessionDataStoreFactory();
factory.setSessionDataMapFactory(mapFactory);

View File

@ -0,0 +1,3 @@
org.slf4j.simpleLogger.defaultLogLevel=info
org.slf4j.simpleLogger.log.org.eclipse.jetty.memcached.sessions=info
org.slf4j.simpleLogger.log.org.eclipse.jetty.memcached.sessions.MemcachedLogs=info