Skip to content

Commit

Permalink
GEODE-8358: Run Geode Redis session tests against native Redis (apach…
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbey37 authored Jul 16, 2020
1 parent 0f16c0f commit 86e3266
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 17 deletions.
6 changes: 6 additions & 0 deletions geode-redis/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@ dependencies {
integrationTestRuntime(project(':geode-log4j'))

acceptanceTestImplementation(sourceSets.integrationTest.output)
acceptanceTestImplementation(sourceSets.distributedTest.output)
acceptanceTestImplementation(sourceSets.commonTest.output)
acceptanceTestImplementation(project(':geode-dunit'))
acceptanceTestImplementation(project(':geode-junit'))
acceptanceTestImplementation('redis.clients:jedis')
acceptanceTestImplementation('org.testcontainers:testcontainers')
acceptanceTestRuntime(project(':geode-log4j'))
acceptanceTestImplementation('org.springframework.boot:spring-boot-starter-web') {
exclude module: 'spring-boot-starter-tomcat'
}
acceptanceTestImplementation('org.springframework.boot:spring-boot-starter-data-redis')
acceptanceTestImplementation('org.springframework.session:spring-session-data-redis')

distributedTestCompile('org.apache.logging.log4j:log4j-core')
distributedTestImplementation(project(':geode-dunit'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You 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 session;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.testcontainers.containers.GenericContainer;
import redis.clients.jedis.Jedis;

import org.apache.geode.internal.AvailablePortHelper;
import org.apache.geode.redis.session.RedisSessionDUnitTest;
import org.apache.geode.test.junit.rules.IgnoreOnWindowsRule;

public class NativeRedisSessionAcceptanceTest extends RedisSessionDUnitTest {

@ClassRule
public static TestRule ignoreOnWindowsRule = new IgnoreOnWindowsRule();

@BeforeClass
public static void setup() {
GenericContainer redisContainer = new GenericContainer<>("redis:5.0.6").withExposedPorts(6379);
redisContainer.start();
int[] availablePorts = AvailablePortHelper.getRandomAvailableTCPPorts(2);
ports.put(APP1, availablePorts[0]);
ports.put(APP2, availablePorts[1]);
ports.put(SERVER1, redisContainer.getFirstMappedPort());

jedisConnetedToServer1 = new Jedis("localhost", ports.get(SERVER1), JEDIS_TIMEOUT);
startSpringApp(APP1, SERVER1, DEFAULT_SESSION_TIMEOUT);
startSpringApp(APP2, SERVER1, DEFAULT_SESSION_TIMEOUT);
}

@Test
@Ignore
public void should_getSessionFromServer1_whenServer2GoesDown() {
// Only using one server for Native Redis
}

@Test
@Ignore
public void should_getSessionFromServer_whenServerGoesDownAndIsRestarted() {
// Only using one server for Native Redis
}

@Test
@Ignore
public void should_getSession_whenServer2GoesDown_andAppFailsOverToServer1() {
// Only using one server for Native Redis
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You 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 session;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.testcontainers.containers.GenericContainer;
import redis.clients.jedis.Jedis;

import org.apache.geode.internal.AvailablePortHelper;
import org.apache.geode.redis.session.SessionExpirationDUnitTest;
import org.apache.geode.test.junit.rules.IgnoreOnWindowsRule;

public class NativeRedisSessionExpirationAcceptanceTest extends SessionExpirationDUnitTest {

@ClassRule
public static TestRule ignoreOnWindowsRule = new IgnoreOnWindowsRule();

@BeforeClass
public static void setup() {
GenericContainer redisContainer = new GenericContainer<>("redis:5.0.6").withExposedPorts(6379);
redisContainer.start();
int[] availablePorts = AvailablePortHelper.getRandomAvailableTCPPorts(2);
ports.put(APP1, availablePorts[0]);
ports.put(APP2, availablePorts[1]);
ports.put(SERVER1, redisContainer.getFirstMappedPort());

jedisConnetedToServer1 = new Jedis("localhost", ports.get(SERVER1), JEDIS_TIMEOUT);
startSpringApp(APP1, SERVER1, SHORT_SESSION_TIMEOUT);
startSpringApp(APP2, SERVER1, SHORT_SESSION_TIMEOUT);
}

@Test
@Ignore
public void sessionShouldTimeout_whenAppFailsOverToAnotherRedisServer() {
// Only using one server for Native Redis
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.geode.test.junit.categories.RedisTest;

@Category({RedisTest.class})
public class RedisSessionDistDUnitTest extends SessionDUnitTest {
public class RedisSessionDUnitTest extends SessionDUnitTest {

@BeforeClass
public static void setup() {
Expand Down Expand Up @@ -85,7 +85,7 @@ public void should_getSessionFromServer_whenServerGoesDownAndIsRestarted() {

String[] sessionNotes = getSessionNotes(APP2, sessionCookie);

assertThat(sessionNotes).containsExactly("noteFromClient2", "noteFromClient1");
assertThat(sessionNotes).containsExactlyInAnyOrder("noteFromClient2", "noteFromClient1");
}

@Test
Expand All @@ -112,7 +112,11 @@ public void should_getSessionCreatedByApp2_whenApp2GoesDown_andClientConnectsToA

assertThat(sessionNotes).containsExactly("noteFromClient2");
} finally {
startSpringApp(APP2, SERVER1, SERVER2, DEFAULT_SESSION_TIMEOUT);
if (ports.get(SERVER2) == null) {
startSpringApp(APP2, SERVER1, DEFAULT_SESSION_TIMEOUT);
} else {
startSpringApp(APP2, SERVER1, SERVER2, DEFAULT_SESSION_TIMEOUT);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package org.apache.geode.redis.session;

import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -65,11 +67,12 @@ public abstract class SessionDUnitTest {
protected static final int APP1 = 3;
protected static final int APP2 = 4;

private static final Map<Integer, Integer> ports = new HashMap<>();
protected static final Map<Integer, Integer> ports = new HashMap<>();
public static ConfigurableApplicationContext springApplicationContext;

protected static Jedis jedisConnetedToServer1;
private static final int JEDIS_TIMEOUT = Math.toIntExact(GeodeAwaitility.getTimeout().toMillis());
protected static final int JEDIS_TIMEOUT =
Math.toIntExact(GeodeAwaitility.getTimeout().toMillis());

@BeforeClass
public static void setup() {
Expand Down Expand Up @@ -109,6 +112,19 @@ protected static void startRedisServer(int server) {
});
}

protected static void startSpringApp(int sessionApp, int primaryServer, long sessionTimeout) {
int primaryRedisPort = ports.get(primaryServer);
int httpPort = ports.get(sessionApp);
VM host = cluster.getVM(sessionApp);
host.invoke("Start a Spring app", () -> {
System.setProperty("server.port", "" + httpPort);
System.setProperty("spring.redis.port", "" + primaryRedisPort);
System.setProperty("server.servlet.session.timeout", "" + sessionTimeout + "s");
springApplicationContext = SpringApplication.run(
RedisSpringTestApplication.class, "" + primaryRedisPort);
});
}

static void startSpringApp(int sessionApp, int primaryServer, int secondaryServer,
long sessionTimeout) {
int primaryRedisPort = ports.get(primaryServer);
Expand All @@ -131,15 +147,29 @@ static void stopSpringApp(int sessionApp) {

protected String createNewSessionWithNote(int sessionApp, String note) {
HttpEntity<String> request = new HttpEntity<>(note);
HttpHeaders resultHeaders = new RestTemplate()
.postForEntity(
"http://localhost:" + ports.get(sessionApp)
+ "/addSessionNote",
request,
String.class)
.getHeaders();

return resultHeaders.getFirst("Set-Cookie");
boolean noteAdded = false;
String sessionCookie = "";
do {
try {
HttpHeaders resultHeaders = new RestTemplate()
.postForEntity(
"http://localhost:" + ports.get(sessionApp)
+ "/addSessionNote",
request,
String.class)
.getHeaders();
sessionCookie = resultHeaders.getFirst("Set-Cookie");
noteAdded = true;
} catch (HttpServerErrorException e) {
if (e.getMessage().contains("memberDeparted")) {
// retry
} else {
throw e;
}
}
} while (!noteAdded);

return sessionCookie;
}

protected String[] getSessionNotes(int sessionApp, String sessionCookie) {
Expand Down Expand Up @@ -172,6 +202,8 @@ protected String[] getSessionNotes(int sessionApp, String sessionCookie) {
void addNoteToSession(int sessionApp, String sessionCookie, String note) {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Cookie", sessionCookie);
List<String> notes = new ArrayList<>();
Collections.addAll(notes, getSessionNotes(sessionApp, sessionCookie));
HttpEntity<String> request = new HttpEntity<>(note, requestHeaders);
boolean noteAdded = false;
do {
Expand All @@ -184,8 +216,13 @@ void addNoteToSession(int sessionApp, String sessionCookie, String note) {
.getHeaders();
noteAdded = true;
} catch (HttpServerErrorException e) {
if (e.getMessage().contains("Internal Server Error")) {
// retry
if (e.getMessage().contains("memberDeparted")) {
List<String> updatedNotes = new ArrayList<>();
Collections.addAll(updatedNotes, getSessionNotes(sessionApp, sessionCookie));
if (notes.containsAll(updatedNotes)) {
noteAdded = true;
}
e.printStackTrace();
} else {
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class SessionExpirationDUnitTest extends SessionDUnitTest {

private static final int SHORT_SESSION_TIMEOUT = 5;
protected static final int SHORT_SESSION_TIMEOUT = 5;

@BeforeClass
public static void setup() {
Expand Down

0 comments on commit 86e3266

Please sign in to comment.