Added some scaffolding for testing peer related stuffs.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6592 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2011-04-03 22:49:46 +00:00
parent 560fd59f26
commit bc4c9224ec
4 changed files with 265 additions and 0 deletions
+6
View File
@@ -119,6 +119,12 @@
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -0,0 +1,71 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.presents.peer.server;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Tests basic peer network setup and peer communication.
*/
public class PeerTest
{
@Test
public void testPeerConnect ()
throws Exception
{
PeerTestGroup group = new PeerTestGroup(2, true);
// wire up callbacks for when we're connected to a peer
final CountDownLatch done = new CountDownLatch(group.servers.size());
TestPeerManager p1 = group.injectors.get(0).getInstance(TestPeerManager.class);
p1.setOnConnected(new TestPeerManager.Callback<String>() {
public void apply (String nodeName) {
done.countDown();
}
});
TestPeerManager p2 = group.injectors.get(1).getInstance(TestPeerManager.class);
p2.setOnConnected(new TestPeerManager.Callback<String>() {
public void apply (String nodeName) {
done.countDown();
}
});
// start up all of our servers
group.start();
// trigger the immediate establishment of the peer network (normally this is delayed for 5
// seconds to allow slack for differing peer startup times)
p1.refreshPeers();
try {
done.await(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("Interrupte?");
}
group.shutdown();
}
}
@@ -0,0 +1,132 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.presents.peer.server;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.samskivert.depot.PersistenceContext;
import com.samskivert.jdbc.StaticConnectionProvider;
import com.threerings.NaryaLog;
import com.threerings.presents.server.PresentsServer;
/**
* Manages a collection of peer servers.
*/
public class PeerTestGroup
{
public final List<PresentsServer> servers = Lists.newArrayList();
public final List<Injector> injectors = Lists.newArrayList();
public PeerTestGroup (int count, boolean suppressInfoLogging)
throws Exception
{
if (suppressInfoLogging) {
Logger.getLogger("").setLevel(Level.WARNING);
}
for (int ii = 0; ii < count; ii++) {
final String nodename = "node" + ii;
final int port = BASE_PORT + ii;
Injector inj = Guice.createInjector(new PresentsServer.PresentsModule() {
@Override protected void configure () {
super.configure();
bind(PresentsServer.class).toInstance(new PeerTestServer(port));
bind(PeerManager.class).to(TestPeerManager.class);
bind(PersistenceContext.class).toInstance(new PersistenceContext());
}
});
this.injectors.add(inj);
PersistenceContext pctx = inj .getInstance(PersistenceContext.class);
Properties props = new Properties();
props.put("default.driver", "org.hsqldb.jdbcDriver");
props.put("default.url", "jdbc:hsqldb:mem:testdb");
props.put("default.username", "sa");
props.put("default.password", "");
pctx.init("testdb", new StaticConnectionProvider(props), null);
pctx.initializeRepositories(true);
PresentsServer server = inj.getInstance(PresentsServer.class);
server.init(inj);
this.servers.add(server);
PeerManager peermgr = inj.getInstance(PeerManager.class);
peermgr.init(nodename, "I has a s3cr3t!", "localhost", "localhost", port);
}
}
public void start ()
{
Preconditions.checkState(_threads.isEmpty(), "Group already started.");
for (final PresentsServer server : this.servers) {
Thread thread = new Thread() {
public void run () {
server.run();
}
};
_threads.add(thread);
thread.start();
}
}
public void shutdown ()
{
for (PresentsServer server : this.servers) {
server.queueShutdown();
}
try {
// shutdown our servers
for (Thread t : _threads) {
t.join(1000);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
protected static class PeerTestServer extends PresentsServer {
public PeerTestServer (int port) {
_ports = new int[] { port };
}
@Override protected int[] getListenPorts () {
return _ports;
}
protected int[] _ports;
@Inject protected PeerManager _pmgr; // trigger pmgr resolution
}
protected final List<Thread> _threads = Lists.newArrayList();
protected static final int BASE_PORT = 1234;
}
@@ -0,0 +1,56 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.presents.peer.server;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.samskivert.util.Lifecycle;
/**
* A peer manager with hooks for testing.
*/
@Singleton
public class TestPeerManager extends PeerManager
{
public interface Callback<T> {
void apply (T value);
}
@Inject
public TestPeerManager (Lifecycle cycle) {
super(cycle);
}
public void setOnConnected (Callback<String> onConnected) {
_onConnected = onConnected;
}
protected void connectedToPeer (PeerNode peer) {
super.connectedToPeer(peer);
if (_onConnected != null) {
_onConnected.apply(peer.nodeobj.nodeName);
}
}
protected Callback<String> _onConnected;
}