Skip to content

Commit

Permalink
Terracotta-OSS#191 : Stabilize some ITs
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonydahanne committed Dec 20, 2016
1 parent c1bd590 commit 1426f35
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.Rule;
Expand Down Expand Up @@ -56,7 +57,7 @@
*/
public abstract class AbstractTest {

private final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper = new ObjectMapper().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);

private Connection managementConnection;
private Cluster cluster;
Expand Down Expand Up @@ -198,4 +199,25 @@ protected void queryAllRemoteStatsUntil(Predicate<List<? extends ContextualStati
assertTrue(test.test(statistics));
}

protected String removeRandomValues(String currentTopo) {
// removes all random values
return currentTopo
.replaceAll("\"(hostName)\":\"[^\"]*\"", "\"$1\":\"<hostname>\"")
.replaceAll("\"hostAddress\":[^,]*", "\"hostAddress\":\"127\\.0\\.0\\.1\"")
.replaceAll("\"bindPort\":[0-9]+", "\"bindPort\":0")
.replaceAll("\"groupPort\":[0-9]+", "\"groupPort\":0")
.replaceAll("\"port\":[0-9]+", "\"port\":0")
.replaceAll("\"activateTime\":[0-9]+", "\"activateTime\":0")
.replaceAll("\"time\":[0-9]+", "\"time\":0")
.replaceAll("\"startTime\":[0-9]+", "\"startTime\":0")
.replaceAll("\"upTimeSec\":[0-9]+", "\"upTimeSec\":0")
.replaceAll("\"id\":\"[0-9]+@[^:]*:([^:]*):[^\"]*\",\"pid\":[0-9]+", "\"id\":\"[email protected]:$1:<uuid>\",\"pid\":0")
.replaceAll("\"buildId\":\"[^\"]*\"", "\"buildId\":\"Build ID\"")
.replaceAll("\"version\":\"[^\"]*\"", "\"version\":\"<version>\"")
.replaceAll("\"clientId\":\"[0-9]+@[^:]*:([^:]*):[^\"]*\"", "\"clientId\":\"[email protected]:$1:<uuid>\"")
.replaceAll("\"logicalConnectionUid\":\"[^\"]*\"", "\"logicalConnectionUid\":\"<uuid>\"")
.replaceAll("\"id\":\"[^\"]*\",\"logicalConnectionUid\":\"[^\"]*\"", "\"id\":\"<uuid>:SINGLE:testServer0:127.0.0.1:0\",\"logicalConnectionUid\":\"<uuid>\"")
.replaceAll("\"vmId\":\"[^\"]*\"", "\"vmId\":\"[email protected]\"");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/**
Expand All @@ -49,19 +50,28 @@ public void get_notifications_when_passive_leaves() throws Exception {
List<Message> messages;
do {
messages = tmsAgentService.readMessages();

if (messages.stream()
.filter(message -> message.getType().equals("NOTIFICATION"))
.flatMap(message -> message.unwrap(ContextualNotification.class).stream())
.map(ContextualNotification::getType)
.anyMatch(s -> s.equals("SERVER_LEFT"))) {
break;
}
}
while (messages.isEmpty() && !Thread.currentThread().isInterrupted());
while (!Thread.currentThread().isInterrupted());

assertThat(messages.stream()
.filter(message -> message.getType().equals("NOTIFICATION"))
.flatMap(message -> message.unwrap(ContextualNotification.class).stream())
.map(ContextualNotification::getType)
.collect(Collectors.toList()),
equalTo(Arrays.asList("SERVER_LEFT")));
.anyMatch(s -> s.equals("SERVER_LEFT")),
is(true));

assertThat(messages.stream()
.filter(message -> message.getType().equals("NOTIFICATION"))
.flatMap(message -> message.unwrap(ContextualNotification.class).stream())
.filter(notif -> notif.getType().equals("SERVER_LEFT"))
.map(contextualNotification -> contextualNotification.getContext().get(Server.NAME_KEY))
.collect(Collectors.toList()),
equalTo(Arrays.asList(passive.getServerName())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.stream.Stream;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;

Expand Down Expand Up @@ -105,8 +106,15 @@ public void get_notifications_when_passive_joins() throws Exception {
// wait for SYNC_END message to transit from passive to active
do {
messages = tmsAgentService.readMessages();
if (messages.stream()
.filter(message -> message.getType().equals("NOTIFICATION"))
.flatMap(message -> message.unwrap(ContextualNotification.class).stream())
.map(ContextualNotification::getType)
.anyMatch(s -> s.equals("SYNC_END"))) {
break;
}
}
while (messages.isEmpty() && !Thread.currentThread().isInterrupted());
while (!Thread.currentThread().isInterrupted());

assertThat(messages.stream()
.filter(message -> message.getType().equals("NOTIFICATION"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,15 @@ public class PassiveTopologyIT extends AbstractHATest {
@Test
public void topology_includes_passives() throws Exception {
Cluster cluster = tmsAgentService.readTopology();

// removes all random values

cluster.serverStream().forEach(server -> {
server.setActivateTime(0);
server.setUpTimeSec(0);
server.setStartTime(0);
server.setBuildId("Build ID");
server.setVersion("");
server.setGroupPort(0);
server.setBindPort(0);
});

cluster.serverEntityStream()
.map(ServerEntity::getManagementRegistry)
.flatMap(managementRegistry -> Stream.of(
managementRegistry.flatMap(r -> r.getCapability("ServerCacheSettings")),
managementRegistry.flatMap(r -> r.getCapability("OffHeapResourceSettings"))))
.forEach(capability -> {
if (capability.isPresent()) {
capability.get()
.getDescriptors(Settings.class)
.stream()
.filter(settings -> settings.containsKey("time")).forEach(settings -> settings.set("time", 0));
}
});

Server passive = cluster.serverStream().filter(server -> !server.isActive()).findFirst().get();
final String[] currentPassive = {toJson(passive.toMap()).toString()};
cluster.clientStream().forEach(client -> currentPassive[0] = currentPassive[0]
.replace(passive.getServerName(), "stripe-PASSIVE"));

// please leave this: easy to compare if something changes
/*FileWriter w = new FileWriter(new File("target/out.json"));
w.write(currentPassive[0]);
w.close();*/
String actual = removeRandomValues(currentPassive[0]);

// and compare
assertEquals(readJson("passive.json").toString(), currentPassive[0]);
assertEquals(readJson("passive.json").toString(), actual);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.Test;
import org.terracotta.management.model.capabilities.descriptors.Settings;
import org.terracotta.management.model.cluster.Client;
import org.terracotta.management.model.cluster.Cluster;
import org.terracotta.management.model.cluster.ServerEntity;
import org.terracotta.management.model.message.Message;
Expand All @@ -39,65 +40,18 @@ public class TopologyIT extends AbstractSingleTest {
@Test
public void can_read_topology() throws Exception {
Cluster cluster = tmsAgentService.readTopology();
String currentTopo = toJson(cluster.toMap()).toString();
String actual = removeRandomValues(currentTopo);

// removes all random values

cluster.serverStream().forEach(server -> {
server.setActivateTime(0);
server.setStartTime(0);
server.setBuildId("Build ID");
});

cluster.serverEntityStream()
.map(ServerEntity::getManagementRegistry)
.flatMap(managementRegistry -> Stream.of(
managementRegistry.flatMap(r -> r.getCapability("ServerCacheSettings")),
managementRegistry.flatMap(r -> r.getCapability("OffHeapResourceSettings"))))
.forEach(capability -> {
if (capability.isPresent()) {
capability.get()
.getDescriptors(Settings.class)
.stream()
.filter(settings -> settings.containsKey("time")).forEach(settings -> settings.set("time", 0));
}
});

final String[] currentTopo = {toJson(cluster.toMap()).toString()};
cluster.clientStream().forEach(client -> currentTopo[0] = currentTopo[0]
.replace(client.getClientIdentifier().getConnectionUid(), "<uuid>")
.replace(String.valueOf(client.getPid()), "0")
.replace(String.valueOf(client.connectionStream().findFirst().get().getClientEndpoint().getPort()), "0")
.replace(client.getHostName(), "<hostname>")
.replace(client.getHostAddress(), "127.0.0.1"));

String actual = replaceHostname(zeroPortsAndUptime(currentTopo[0]));
String expected = replaceVersion(readJson("topology.json").toString());

String expected = readJson("topology.json").toString();

System.out.println("This is the actual topology : " + actual);
System.out.println("This is the expected topology : " + expected);
// and compare
assertEquals(expected, actual);
}

private String zeroPortsAndUptime(String s) {
return s.replaceAll("(\"bindPort\":[0-9]+)", "\"bindPort\":0")
.replaceAll("(\"groupPort\":[0-9]+)", "\"groupPort\":0")
.replaceAll("(\"upTimeSec\":[0-9]+)", "\"upTimeSec\":0");
}

private String replaceHostname(String s) {
return s.replaceAll("\"hostname\":(.*),", "\"hostname\":\"<hostname>\",");
}

/**
*
* @param topology, probably from topology.json
* @return the same topology, but replacing VERSION_TO_REPLACE with the tc core version from the pom
*/
private String replaceVersion(String topology) {
return topology.replace("VERSION_TO_REPLACE", System.getProperty("kitInstallationPath").substring(System.getProperty("kitInstallationPath").lastIndexOf("terracotta-") + 11, System.getProperty("kitInstallationPath").length()-1));
}

@Test
public void can_read_messages() throws Exception {
List<Message> messages = tmsAgentService.readMessages();
Expand Down Expand Up @@ -136,18 +90,14 @@ public void notifications_have_a_source_context() throws Exception {
.flatMap(message -> message.unwrap(ContextualNotification.class).stream())
.collect(Collectors.toList());

// removes all random values

final String[] currentJson = {toJson(notifs).toString()};
String currentJson = toJson(notifs).toString();
String actual = removeRandomValues(currentJson);
String expected = readJson("notifications.json").toString();

tmsAgentService.readTopology().clientStream().forEach(client -> currentJson[0] = currentJson[0]
.replace(client.getClientIdentifier().getConnectionUid(), "<uuid>")
.replace(String.valueOf(client.getPid()), "0")
.replace(String.valueOf(client.connectionStream().findFirst().get().getClientEndpoint().getPort()), "0")
.replace(client.getHostName(), "<hostname>")
.replace(client.getHostAddress(), "127.0.0.1"));
System.out.println("This is the actual topology : " + actual);
System.out.println("This is the expected topology : " + expected);

assertEquals(readJson("notifications.json").toString(), currentJson[0]);
assertEquals(expected, actual);
}

}
Loading

0 comments on commit 1426f35

Please sign in to comment.