forked from External/mage
Test for multiple connects. Reproduces bug with "already connected" exception.
This commit is contained in:
parent
8d12b34c6d
commit
16c03d6d4d
2 changed files with 137 additions and 0 deletions
134
Mage.Client/src/test/java/mage/client/game/MultiConnectTest.java
Normal file
134
Mage.Client/src/test/java/mage/client/game/MultiConnectTest.java
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
package mage.client.game;
|
||||
|
||||
import mage.client.components.MageUI;
|
||||
import mage.interfaces.MageClient;
|
||||
import mage.interfaces.callback.ClientCallback;
|
||||
import mage.remote.Connection;
|
||||
import mage.remote.Session;
|
||||
import mage.utils.MageVersion;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
/**
|
||||
* Test for emulating the connection from multi mage clients.
|
||||
*
|
||||
* @author ayratn
|
||||
*/
|
||||
public class MultiConnectTest {
|
||||
|
||||
private final static Logger logger = Logger.getLogger(MultiConnectTest.class);
|
||||
|
||||
/**
|
||||
* Amount of games to be started from this test.
|
||||
*/
|
||||
private final static Integer USER_CONNECT_COUNT = 200;
|
||||
|
||||
private final static CountDownLatch latch = new CountDownLatch(USER_CONNECT_COUNT);
|
||||
|
||||
private final static MageVersion version = new MageVersion(0, 8, 1, "");
|
||||
|
||||
private static volatile int connected;
|
||||
|
||||
private Object sync = new Object();
|
||||
private MageUI ui;
|
||||
|
||||
private class ClientMock implements MageClient {
|
||||
|
||||
private Session session;
|
||||
private String username;
|
||||
|
||||
public ClientMock(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
session = new Session(this);
|
||||
Connection connection = new Connection();
|
||||
connection.setUsername(username);
|
||||
connection.setHost("localhost");
|
||||
connection.setPort(17171);
|
||||
connection.setProxyType(Connection.ProxyType.NONE);
|
||||
|
||||
session.connect(connection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getId() {
|
||||
logger.info("getId");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MageVersion getVersion() {
|
||||
logger.info("getVersion");
|
||||
return version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connected(String message) {
|
||||
logger.info("connected: " + message);
|
||||
connected++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected() {
|
||||
logger.info("disconnected");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showMessage(String message) {
|
||||
logger.info("showMessage: " + message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showError(String message) {
|
||||
logger.info("showError: " + message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processCallback(ClientCallback callback) {
|
||||
logger.info("processCallback");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] argv) throws Exception {
|
||||
new MultiConnectTest().startMultiGames();
|
||||
}
|
||||
|
||||
public void startMultiGames() throws Exception {
|
||||
for (int i = 0; i < USER_CONNECT_COUNT; i++) {
|
||||
logger.info("Starting game");
|
||||
connect(i);
|
||||
}
|
||||
latch.await();
|
||||
logger.info("Finished");
|
||||
logger.info("Connected: " + connected + " of " + USER_CONNECT_COUNT);
|
||||
}
|
||||
|
||||
private void connect(final int index) throws Exception {
|
||||
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
logger.fatal(null, e);
|
||||
}
|
||||
});
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
String username = "player" + index;
|
||||
ClientMock client = new ClientMock(username);
|
||||
client.connect();
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void sleep(int ms) {
|
||||
try {
|
||||
Thread.sleep(ms);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue