Aaron Kruglikov
Committed by Gerrit Code Review

Changes to speed up tests.

Change-Id: I1830f564710b9cb06d9c41d31e31854a272fbf4d
......@@ -19,20 +19,18 @@ package org.onosproject.store.primitives.resources.impl;
import com.google.common.collect.Lists;
import com.google.common.collect.Multiset;
import com.google.common.collect.TreeMultiset;
import com.google.common.io.Files;
import io.atomix.resource.ResourceType;
import org.apache.commons.collections.keyvalue.DefaultMapEntry;
import org.junit.Ignore;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onlab.util.Tools;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
......@@ -41,9 +39,7 @@ import static org.junit.Assert.assertTrue;
/**
* Tests the {@link AsyncConsistentSetMultimap}.
*/
@Ignore
public class AsyncConsistentSetMultimapTest extends AtomixTestBase {
private final File testDir = Files.createTempDir();
private final String keyOne = "hello";
private final String keyTwo = "goodbye";
private final String keyThree = "foo";
......@@ -58,7 +54,16 @@ public class AsyncConsistentSetMultimapTest extends AtomixTestBase {
valueTwo,
valueThree,
valueFour);
private final AtomicInteger port = new AtomicInteger(49200);
@BeforeClass
public static void preTestSetup() throws Throwable {
createCopycatServers(3);
}
@AfterClass
public static void postTestCleanup() throws Exception {
clearTests();
}
@Override
protected ResourceType resourceType() {
......@@ -71,8 +76,7 @@ public class AsyncConsistentSetMultimapTest extends AtomixTestBase {
*/
@Test
public void testSize() throws Throwable {
clearTests();
AsyncConsistentSetMultimap map = createResource(3);
AsyncConsistentSetMultimap map = createResource("testOneMap");
//Simplest operation case
map.isEmpty().thenAccept(result -> assertTrue(result));
map.put(keyOne, valueOne).
......@@ -114,7 +118,6 @@ public class AsyncConsistentSetMultimapTest extends AtomixTestBase {
map.isEmpty().thenAccept(result -> assertTrue(result));
map.destroy().join();
clearTests();
}
/**
......@@ -122,8 +125,7 @@ public class AsyncConsistentSetMultimapTest extends AtomixTestBase {
*/
@Test
public void containsTest() throws Throwable {
clearTests();
AsyncConsistentSetMultimap map = createResource(3);
AsyncConsistentSetMultimap map = createResource("testTwoMap");
//Populate the maps
allKeys.forEach(key -> {
......@@ -174,7 +176,6 @@ public class AsyncConsistentSetMultimapTest extends AtomixTestBase {
});
map.destroy().join();
clearTests();
}
/**
......@@ -183,8 +184,7 @@ public class AsyncConsistentSetMultimapTest extends AtomixTestBase {
*/
@Test
public void addAndRemoveTest() throws Exception {
clearTests();
AsyncConsistentSetMultimap map = createResource(3);
AsyncConsistentSetMultimap map = createResource("testThreeMap");
//Test single put
allKeys.forEach(key -> {
......@@ -309,7 +309,6 @@ public class AsyncConsistentSetMultimapTest extends AtomixTestBase {
});
map.destroy().join();
clearTests();
}
/**
......@@ -319,8 +318,7 @@ public class AsyncConsistentSetMultimapTest extends AtomixTestBase {
*/
@Test
public void testAccessors() throws Exception {
clearTests();
AsyncConsistentSetMultimap map = createResource(3);
AsyncConsistentSetMultimap map = createResource("testFourMap");
//Populate for full map behavior tests
allKeys.forEach(key -> {
......@@ -400,15 +398,13 @@ public class AsyncConsistentSetMultimapTest extends AtomixTestBase {
map.entries()
.thenAccept(result -> assertTrue(result.isEmpty())).join();
map.destroy();
clearTests();
map.destroy().join();
}
private AsyncConsistentSetMultimap createResource(int clusterSize) {
private AsyncConsistentSetMultimap createResource(String mapName) {
try {
createCopycatServers(clusterSize);
AsyncConsistentSetMultimap map = createAtomixClient().
getResource("testMap", AsyncConsistentSetMultimap.class)
getResource("mapName", AsyncConsistentSetMultimap.class)
.join();
return map;
} catch (Throwable e) {
......
......@@ -18,7 +18,8 @@ package org.onosproject.store.primitives.resources.impl;
import com.google.common.base.Throwables;
import com.google.common.collect.Sets;
import io.atomix.resource.ResourceType;
import org.junit.Ignore;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onlab.util.Tools;
import org.onosproject.store.primitives.MapUpdate;
......@@ -49,9 +50,17 @@ import static org.junit.Assert.fail;
/**
* Unit tests for {@link AtomixConsistentMap}.
*/
@Ignore
public class AtomixConsistentMapTest extends AtomixTestBase {
@BeforeClass
public static void preTestSetup() throws Throwable {
createCopycatServers(3);
}
@AfterClass
public static void postTestCleanup() throws Exception {
clearTests();
}
@Override
protected ResourceType resourceType() {
return new ResourceType(AtomixConsistentMap.class);
......@@ -98,12 +107,11 @@ public class AtomixConsistentMapTest extends AtomixTestBase {
}
protected void basicMapOperationTests(int clusterSize) throws Throwable {
createCopycatServers(clusterSize);
final byte[] rawFooValue = Tools.getBytesUtf8("Hello foo!");
final byte[] rawBarValue = Tools.getBytesUtf8("Hello bar!");
AtomixConsistentMap map = createAtomixClient().getResource("test", AtomixConsistentMap.class).join();
AtomixConsistentMap map = createAtomixClient().getResource("testBasicMapOperationMap",
AtomixConsistentMap.class).join();
map.isEmpty().thenAccept(result -> {
assertTrue(result);
......@@ -228,12 +236,12 @@ public class AtomixConsistentMapTest extends AtomixTestBase {
}
public void mapComputeOperationTests(int clusterSize) throws Throwable {
createCopycatServers(clusterSize);
final byte[] value1 = Tools.getBytesUtf8("value1");
final byte[] value2 = Tools.getBytesUtf8("value2");
final byte[] value3 = Tools.getBytesUtf8("value3");
AtomixConsistentMap map = createAtomixClient().getResource("test", AtomixConsistentMap.class).join();
AtomixConsistentMap map = createAtomixClient().getResource("testMapComputeOperationsMap",
AtomixConsistentMap.class).join();
map.computeIfAbsent("foo", k -> value1).thenAccept(result -> {
assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), value1));
......@@ -266,12 +274,12 @@ public class AtomixConsistentMapTest extends AtomixTestBase {
protected void mapListenerTests(int clusterSize) throws Throwable {
createCopycatServers(clusterSize);
final byte[] value1 = Tools.getBytesUtf8("value1");
final byte[] value2 = Tools.getBytesUtf8("value2");
final byte[] value3 = Tools.getBytesUtf8("value3");
AtomixConsistentMap map = createAtomixClient().getResource("test", AtomixConsistentMap.class).join();
AtomixConsistentMap map = createAtomixClient().getResource("testMapListenerMap",
AtomixConsistentMap.class).join();
TestMapEventListener listener = new TestMapEventListener();
// add listener; insert new value into map and verify an INSERT event is received.
......@@ -326,11 +334,11 @@ public class AtomixConsistentMapTest extends AtomixTestBase {
}
protected void transactionCommitTests(int clusterSize) throws Throwable {
createCopycatServers(clusterSize);
final byte[] value1 = Tools.getBytesUtf8("value1");
final byte[] value2 = Tools.getBytesUtf8("value2");
AtomixConsistentMap map = createAtomixClient().getResource("test", AtomixConsistentMap.class).join();
AtomixConsistentMap map = createAtomixClient().getResource("testCommitTestsMap",
AtomixConsistentMap.class).join();
TestMapEventListener listener = new TestMapEventListener();
map.addListener(listener).join();
......@@ -421,11 +429,11 @@ public class AtomixConsistentMapTest extends AtomixTestBase {
}
protected void transactionRollbackTests(int clusterSize) throws Throwable {
createCopycatServers(clusterSize);
final byte[] value1 = Tools.getBytesUtf8("value1");
final byte[] value2 = Tools.getBytesUtf8("value2");
AtomixConsistentMap map = createAtomixClient().getResource("test", AtomixConsistentMap.class).join();
AtomixConsistentMap map = createAtomixClient().getResource("testTransactionRollbackTestsMap",
AtomixConsistentMap.class).join();
TestMapEventListener listener = new TestMapEventListener();
map.addListener(listener).join();
......
......@@ -20,7 +20,8 @@ import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import org.junit.Ignore;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
......@@ -36,13 +37,22 @@ import io.atomix.resource.ResourceType;
/**
* Unit tests for {@link AtomixLeaderElector}.
*/
@Ignore
public class AtomixLeaderElectorTest extends AtomixTestBase {
NodeId node1 = new NodeId("node1");
NodeId node2 = new NodeId("node2");
NodeId node3 = new NodeId("node3");
@BeforeClass
public static void preTestSetup() throws Throwable {
createCopycatServers(3);
}
@AfterClass
public static void postTestCleanup() throws Exception {
clearTests();
}
@Override
protected ResourceType resourceType() {
return new ResourceType(AtomixLeaderElector.class);
......@@ -54,9 +64,9 @@ public class AtomixLeaderElectorTest extends AtomixTestBase {
}
private void leaderElectorRunTests(int numServers) throws Throwable {
createCopycatServers(numServers);
Atomix client1 = createAtomixClient();
AtomixLeaderElector elector1 = client1.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector1 = client1.getResource("test-elector-run",
AtomixLeaderElector.class).join();
elector1.run("foo", node1).thenAccept(result -> {
assertEquals(node1, result.leaderNodeId());
assertEquals(1, result.leader().term());
......@@ -64,7 +74,8 @@ public class AtomixLeaderElectorTest extends AtomixTestBase {
assertEquals(node1, result.candidates().get(0));
}).join();
Atomix client2 = createAtomixClient();
AtomixLeaderElector elector2 = client2.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector2 = client2.getResource("test-elector-run",
AtomixLeaderElector.class).join();
elector2.run("foo", node2).thenAccept(result -> {
assertEquals(node1, result.leaderNodeId());
assertEquals(1, result.leader().term());
......@@ -80,12 +91,13 @@ public class AtomixLeaderElectorTest extends AtomixTestBase {
}
private void leaderElectorWithdrawTests(int numServers) throws Throwable {
createCopycatServers(numServers);
Atomix client1 = createAtomixClient();
AtomixLeaderElector elector1 = client1.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector1 = client1.getResource("test-elector-withdraw",
AtomixLeaderElector.class).join();
elector1.run("foo", node1).join();
Atomix client2 = createAtomixClient();
AtomixLeaderElector elector2 = client2.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector2 = client2.getResource("test-elector-withdraw",
AtomixLeaderElector.class).join();
elector2.run("foo", node2).join();
LeaderEventListener listener1 = new LeaderEventListener();
......@@ -117,13 +129,15 @@ public class AtomixLeaderElectorTest extends AtomixTestBase {
}
private void leaderElectorAnointTests(int numServers) throws Throwable {
createCopycatServers(numServers);
Atomix client1 = createAtomixClient();
AtomixLeaderElector elector1 = client1.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector1 = client1.getResource("test-elector-anoint",
AtomixLeaderElector.class).join();
Atomix client2 = createAtomixClient();
AtomixLeaderElector elector2 = client2.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector2 = client2.getResource("test-elector-anoint",
AtomixLeaderElector.class).join();
Atomix client3 = createAtomixClient();
AtomixLeaderElector elector3 = client3.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector3 = client3.getResource("test-elector-anoint",
AtomixLeaderElector.class).join();
elector1.run("foo", node1).join();
elector2.run("foo", node2).join();
......@@ -171,13 +185,15 @@ public class AtomixLeaderElectorTest extends AtomixTestBase {
}
private void leaderElectorPromoteTests(int numServers) throws Throwable {
createCopycatServers(numServers);
AtomixClient client1 = createAtomixClient();
AtomixLeaderElector elector1 = client1.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector1 = client1.getResource("test-elector-promote",
AtomixLeaderElector.class).join();
AtomixClient client2 = createAtomixClient();
AtomixLeaderElector elector2 = client2.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector2 = client2.getResource("test-elector-promote",
AtomixLeaderElector.class).join();
AtomixClient client3 = createAtomixClient();
AtomixLeaderElector elector3 = client3.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector3 = client3.getResource("test-elector-promote",
AtomixLeaderElector.class).join();
elector1.run("foo", node1).join();
elector2.run("foo", node2).join();
......@@ -229,12 +245,13 @@ public class AtomixLeaderElectorTest extends AtomixTestBase {
}
private void leaderElectorLeaderSessionCloseTests(int numServers) throws Throwable {
createCopycatServers(numServers);
AtomixClient client1 = createAtomixClient();
AtomixLeaderElector elector1 = client1.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector1 = client1.getResource("test-elector-leader-session-close",
AtomixLeaderElector.class).join();
elector1.run("foo", node1).join();
Atomix client2 = createAtomixClient();
AtomixLeaderElector elector2 = client2.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector2 = client2.getResource("test-elector-leader-session-close",
AtomixLeaderElector.class).join();
LeaderEventListener listener = new LeaderEventListener();
elector2.run("foo", node2).join();
elector2.addChangeListener(listener).join();
......@@ -252,12 +269,13 @@ public class AtomixLeaderElectorTest extends AtomixTestBase {
}
private void leaderElectorNonLeaderSessionCloseTests(int numServers) throws Throwable {
createCopycatServers(numServers);
Atomix client1 = createAtomixClient();
AtomixLeaderElector elector1 = client1.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector1 = client1.getResource("test-elector-non-leader-session-close",
AtomixLeaderElector.class).join();
elector1.run("foo", node1).join();
AtomixClient client2 = createAtomixClient();
AtomixLeaderElector elector2 = client2.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector2 = client2.getResource("test-elector-non-leader-session-close",
AtomixLeaderElector.class).join();
LeaderEventListener listener = new LeaderEventListener();
elector2.run("foo", node2).join();
elector1.addChangeListener(listener).join();
......@@ -275,11 +293,12 @@ public class AtomixLeaderElectorTest extends AtomixTestBase {
}
private void leaderElectorQueryTests(int numServers) throws Throwable {
createCopycatServers(numServers);
Atomix client1 = createAtomixClient();
Atomix client2 = createAtomixClient();
AtomixLeaderElector elector1 = client1.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector2 = client2.getResource("test-elector", AtomixLeaderElector.class).join();
AtomixLeaderElector elector1 = client1.getResource("test-elector-query",
AtomixLeaderElector.class).join();
AtomixLeaderElector elector2 = client2.getResource("test-elector-query",
AtomixLeaderElector.class).join();
elector1.run("foo", node1).join();
elector2.run("foo", node2).join();
elector2.run("bar", node2).join();
......
......@@ -18,7 +18,8 @@ package org.onosproject.store.primitives.resources.impl;
import io.atomix.Atomix;
import io.atomix.resource.ResourceType;
import io.atomix.variables.DistributedLong;
import org.junit.Ignore;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
......@@ -28,9 +29,18 @@ import static org.junit.Assert.assertTrue;
/**git s
* Unit tests for {@link AtomixCounter}.
*/
@Ignore
public class AtomixLongTest extends AtomixTestBase {
@BeforeClass
public static void preTestSetup() throws Throwable {
createCopycatServers(3);
}
@AfterClass
public static void postTestCleanup() throws Exception {
clearTests();
}
@Override
protected ResourceType resourceType() {
return new ResourceType(DistributedLong.class);
......@@ -42,9 +52,9 @@ public class AtomixLongTest extends AtomixTestBase {
}
protected void basicOperationsTest(int clusterSize) throws Throwable {
createCopycatServers(clusterSize);
Atomix atomix = createAtomixClient();
AtomixCounter along = new AtomixCounter("test-long", atomix.getLong("test-long").join());
AtomixCounter along = new AtomixCounter("test-long-basic-operations",
atomix.getLong("test-long").join());
assertEquals(0, along.get().join().longValue());
assertEquals(1, along.incrementAndGet().join().longValue());
along.set(100).join();
......
......@@ -27,8 +27,6 @@ import io.atomix.copycat.server.storage.Storage;
import io.atomix.copycat.server.storage.StorageLevel;
import io.atomix.manager.internal.ResourceManagerState;
import io.atomix.resource.ResourceType;
import org.junit.After;
import org.junit.Before;
import org.onlab.junit.TestTools;
import org.onosproject.store.primitives.impl.CatalystSerializers;
......@@ -43,18 +41,18 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Base class for various Atomix* tests.
* Base class for various Atomix tests.
*/
public abstract class AtomixTestBase {
private static final File TEST_DIR = new File("target/test-logs");
protected LocalServerRegistry registry;
protected final AtomicInteger port = new AtomicInteger(49200);
protected List<Address> members;
protected List<CopycatClient> copycatClients = new ArrayList<>();
protected List<CopycatServer> copycatServers = new ArrayList<>();
protected List<AtomixClient> atomixClients = new ArrayList<>();
protected List<CopycatServer> atomixServers = new ArrayList<>();
protected Serializer serializer = CatalystSerializers.getSerializer();
protected static File testDir;
protected static LocalServerRegistry registry = new LocalServerRegistry();
protected static List<Address> members = new ArrayList<>();
protected static List<CopycatClient> copycatClients = new ArrayList<>();
protected static List<CopycatServer> copycatServers = new ArrayList<>();
protected static List<AtomixClient> atomixClients = new ArrayList<>();
protected static List<CopycatServer> atomixServers = new ArrayList<>();
protected static Serializer serializer = CatalystSerializers.getSerializer();
protected static AtomicInteger port = new AtomicInteger(49200);
/**
* Creates a new resource state machine.
......@@ -68,7 +66,7 @@ public abstract class AtomixTestBase {
*
* @return The next server address.
*/
private Address nextAddress() {
private static Address nextAddress() {
Address address = new Address("127.0.0.1",
TestTools.findAvailablePort(port.getAndIncrement()));
members.add(address);
......@@ -78,7 +76,8 @@ public abstract class AtomixTestBase {
/**
* Creates a set of Copycat servers.
*/
protected List<CopycatServer> createCopycatServers(int nodes) throws Throwable {
protected static List<CopycatServer> createCopycatServers(int nodes)
throws Throwable {
CountDownLatch latch = new CountDownLatch(nodes);
List<CopycatServer> servers = new ArrayList<>();
......@@ -91,20 +90,18 @@ public abstract class AtomixTestBase {
if (members.size() <= 1) {
server.bootstrap().thenRun(latch::countDown).join();
} else {
server.join(members).thenRun(latch::countDown);
server.join(members).join();
}
servers.add(server);
}
Uninterruptibles.awaitUninterruptibly(latch);
return servers;
}
/**
* Creates a Copycat server.
*/
protected CopycatServer createCopycatServer(Address address) {
protected static CopycatServer createCopycatServer(Address address) {
CopycatServer server = CopycatServer.builder(address)
.withTransport(NettyTransport.builder().withThreads(1).build())
.withStorage(Storage.builder()
......@@ -120,23 +117,21 @@ public abstract class AtomixTestBase {
return server;
}
@Before
@After
public void clearTests() throws Exception {
public static void clearTests() throws Exception {
registry = new LocalServerRegistry();
members = new ArrayList<>();
CompletableFuture<Void> closeClients =
CompletableFuture.allOf(atomixClients.stream()
.map(AtomixClient::close)
.toArray(CompletableFuture[]::new));
.map(AtomixClient::close)
.toArray(CompletableFuture[]::new));
closeClients.thenCompose(v -> CompletableFuture.allOf(copycatServers.stream()
closeClients
.thenCompose(v -> CompletableFuture
.allOf(copycatServers.stream()
.map(CopycatServer::shutdown)
.toArray(CompletableFuture[]::new))).join();
deleteDirectory(TEST_DIR);
atomixClients = new ArrayList<>();
copycatServers = new ArrayList<>();
......