Skip to content

Commit

Permalink
Close server and client resources after each test method invocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalterman committed Dec 11, 2015
1 parent 355d033 commit 34eb778
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 100 deletions.
40 changes: 22 additions & 18 deletions manager/src/test/java/io/atomix/AbstractReplicaTest.java
Expand Up @@ -15,19 +15,19 @@
*/
package io.atomix;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import io.atomix.catalyst.transport.Address;
import io.atomix.catalyst.transport.LocalServerRegistry;
import io.atomix.catalyst.transport.LocalTransport;
import io.atomix.copycat.server.storage.Storage;
import io.atomix.copycat.server.storage.StorageLevel;
import net.jodah.concurrentunit.ConcurrentTestCase;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
* Abstract server test.
Expand All @@ -38,7 +38,21 @@ public abstract class AbstractReplicaTest extends ConcurrentTestCase {
protected LocalServerRegistry registry;
protected int port;
protected List<Address> members;
protected List<Atomix> replicas;

@BeforeMethod
protected void init() {
port = 5000;
registry = new LocalServerRegistry();
members = new ArrayList<>();
replicas = new ArrayList<>();
}

@AfterMethod
protected void cleanup() {
replicas.stream().forEach(s -> s.close().join());
}

/**
* Returns the next server address.
*
Expand All @@ -54,8 +68,6 @@ protected Address nextAddress() {
* Creates a set of Atomix servers.
*/
protected List<Atomix> createReplicas(int nodes) throws Throwable {
List<Atomix> replicas = new ArrayList<>();

List<Address> members = new ArrayList<>();
for (int i = 1; i <= nodes; i++) {
members.add(nextAddress());
Expand Down Expand Up @@ -86,13 +98,5 @@ protected Atomix createReplica(Address address) {
.build())
.build();
}

@BeforeMethod
@AfterMethod
public void clearTests() throws IOException {
port = 5000;
registry = new LocalServerRegistry();
members = new ArrayList<>();
}


}
116 changes: 58 additions & 58 deletions manager/src/test/java/io/atomix/AbstractServerTest.java
Expand Up @@ -15,55 +15,71 @@
*/
package io.atomix;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import io.atomix.catalyst.transport.Address;
import io.atomix.catalyst.transport.LocalServerRegistry;
import io.atomix.catalyst.transport.LocalTransport;
import io.atomix.copycat.server.CopycatServer;
import io.atomix.copycat.server.state.Member;
import io.atomix.copycat.server.storage.Storage;
import io.atomix.copycat.server.storage.StorageLevel;
import net.jodah.concurrentunit.ConcurrentTestCase;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Abstract server test.
*
* @author <a href="http://github.com/kuujo>Jordan Halterman</a>
*/
@Test
public abstract class AbstractServerTest extends ConcurrentTestCase {
protected volatile LocalServerRegistry registry;
protected volatile int port;
protected volatile List<Member> members;
protected volatile List<Atomix> clients = new ArrayList<>();
protected volatile List<AtomixServer> servers = new ArrayList<>();
protected LocalServerRegistry registry;
protected int port;
protected List<Address> members;
protected List<Atomix> clients;
protected List<AtomixServer> servers;

@BeforeMethod
protected void init() {
port = 5000;
registry = new LocalServerRegistry();
members = new ArrayList<>();
clients = new ArrayList<>();
servers = new ArrayList<>();
}

@AfterMethod
protected void cleanup() {
clients.stream().forEach(c -> c.close().join());
servers.stream().forEach(s -> s.close().join());
}

/**
* Returns the next server address.
*
* @return The next server address.
*/
protected Member nextMember() {
return new Member(CopycatServer.Type.INACTIVE, new Address("localhost", ++port), new Address("localhost", port + 1000));
protected Address nextAddress() {
Address address = new Address("localhost", port++);
members.add(address);
return address;
}

/**
* Creates a set of Atomix servers.
*/
protected List<AtomixServer> createServers(int nodes) throws Throwable {
List<AtomixServer> servers = new ArrayList<>();

for (int i = 0; i < nodes; i++) {
members.add(nextMember());
List<Address> members = new ArrayList<>();
for (int i = 1; i <= nodes; i++) {
members.add(nextAddress());
}

for (int i = 0; i < nodes; i++) {
AtomixServer server = createServer(members, members.get(i));
AtomixServer server = createServer(members.get(i));
server.open().thenRun(this::resume);
servers.add(server);
}
Expand All @@ -74,45 +90,29 @@ protected List<AtomixServer> createServers(int nodes) throws Throwable {
}

/**
* Creates an Atomix server.
* Creates a client.
*/
protected AtomixServer createServer(List<Member> members, Member member) {
AtomixServer server = AtomixServer.builder(member.clientAddress(), member.serverAddress(), members.stream().map(Member::serverAddress).collect(Collectors.toList()))
.withTransport(new LocalTransport(registry))
.withStorage(Storage.builder()
.withStorageLevel(StorageLevel.MEMORY)
.withMaxSegmentSize(1024 * 1024)
.withMaxEntriesPerSegment(8)
.withMinorCompactionInterval(Duration.ofSeconds(3))
.withMajorCompactionInterval(Duration.ofSeconds(7))
.build())
.build();
servers.add(server);
return server;
protected Atomix createClient() throws Throwable {
Atomix client = AtomixClient.builder(members).withTransport(new LocalTransport(registry)).build();
client.open().thenRun(this::resume);
await(10000);
clients.add(client);
return client;
}

@BeforeMethod
@AfterMethod
public void clearTests() throws Exception {
clients.forEach(c -> {
try {
c.close().join();
} catch (Exception e) {
}
});

servers.forEach(s -> {
try {
s.close().join();
} catch (Exception e) {
}
});

registry = new LocalServerRegistry();
members = new ArrayList<>();
port = 5000;
clients = new ArrayList<>();
servers = new ArrayList<>();

/**
* Creates an Atomix server.
*/
protected AtomixServer createServer(Address address) {
return AtomixServer.builder(address, members)
.withTransport(new LocalTransport(registry))
.withStorage(Storage.builder()
.withStorageLevel(StorageLevel.MEMORY)
.withMaxEntriesPerSegment(8)
.withMinorCompactionInterval(Duration.ofSeconds(5))
.withMajorCompactionInterval(Duration.ofSeconds(10))
.build())
.build();
}

}
30 changes: 10 additions & 20 deletions manager/src/test/java/io/atomix/AtomixClientServerTest.java
Expand Up @@ -15,17 +15,19 @@
*/
package io.atomix;

import io.atomix.catalyst.transport.LocalTransport;
import java.util.concurrent.CompletableFuture;

import org.testng.annotations.Test;

import io.atomix.copycat.client.Command;
import io.atomix.copycat.client.Query;
import io.atomix.copycat.client.CopycatClient;
import io.atomix.copycat.client.Query;
import io.atomix.copycat.server.Commit;
import io.atomix.copycat.server.state.Member;
import io.atomix.resource.*;
import org.testng.annotations.Test;

import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import io.atomix.resource.Consistency;
import io.atomix.resource.Resource;
import io.atomix.resource.ResourceStateMachine;
import io.atomix.resource.ResourceType;
import io.atomix.resource.ResourceTypeInfo;

/**
* Client server test.
Expand Down Expand Up @@ -230,18 +232,6 @@ public void testGetResourceKeys() throws Throwable {
await(10000);
}

/**
* Creates a client.
*/
private Atomix createClient() throws Throwable {
Atomix client = AtomixClient.builder(members.stream().map(Member::clientAddress).collect(Collectors.toList()))
.withTransport(new LocalTransport(registry))
.build();
client.open().thenRun(this::resume);
await(10000);
return client;
}

/**
* Test resource.
*/
Expand Down
4 changes: 2 additions & 2 deletions manager/src/test/java/io/atomix/AtomixServerTest.java
Expand Up @@ -34,7 +34,7 @@ public void testServerJoin() throws Throwable {
createServers(3);
AtomixServer joiner = createServer(members, nextMember());
joiner.open().thenRun(this::resume);
await();
await(30000);
}

/**
Expand All @@ -44,7 +44,7 @@ public void testServerLeave() throws Throwable {
List<AtomixServer> servers = createServers(3);
AtomixServer server = servers.get(0);
server.close().thenRun(this::resume);
await();
await(30000);
}

}
Expand Up @@ -18,6 +18,9 @@
import java.util.ArrayList;
import java.util.List;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import io.atomix.catalyst.transport.Address;
import io.atomix.catalyst.transport.LocalServerRegistry;
import io.atomix.catalyst.transport.LocalTransport;
Expand All @@ -37,8 +40,8 @@ public abstract class AbstractAtomixTest extends ConcurrentTestCase {
protected LocalServerRegistry registry;
protected int port;
protected List<Address> members;
protected List<CopycatClient> clients = new ArrayList<>();
protected List<CopycatServer> servers = new ArrayList<>();
protected List<CopycatClient> clients;
protected List<CopycatServer> servers;

/**
* Creates a new resource state machine.
Expand All @@ -47,6 +50,21 @@ public abstract class AbstractAtomixTest extends ConcurrentTestCase {
*/
protected abstract ResourceStateMachine createStateMachine();

@BeforeMethod
protected void init() {
port = 5000;
registry = new LocalServerRegistry();
members = new ArrayList<>();
clients = new ArrayList<>();
servers = new ArrayList<>();
}

@AfterMethod
protected void cleanup() {
clients.stream().forEach(c -> c.close().join());
servers.stream().forEach(s -> s.close().join());
}

/**
* Returns the next server address.
*
Expand Down

0 comments on commit 34eb778

Please sign in to comment.