astyle, optimizations and test removed

This commit is contained in:
Albert Medela
2016-07-30 17:23:16 +02:00
parent 159fd1e094
commit 35f0521bbd
6 changed files with 9 additions and 256 deletions
@@ -1,11 +0,0 @@
package de.pokerth.test;
import org.junit.Test;
public class GuestLoginThreadedTest extends TestBase {
@Test
public void testInitThreadedMessage() throws Exception {
guestInitThreaded();
}
}
-239
View File
@@ -389,243 +389,4 @@ public abstract class TestBase {
return cipher.doFinal(ciphertext);
}
/**
* Description:
*
* This test runs several guests users in PokerTH Server.
* Starts N threads thats connects to server "at once": There is a delay between each connection, to avoid error 133
* (NOTE: I didn't succeed avoid this message. So, it's better to disable "ServerBruteForceProtection" in config.xml)
* Once connected, each thread waits until all other connects to simulate N users at once
*
* When all threads are connected, each thread gets unlocked and close connections, in an orderly way.
*
*
* Running:
*
* Tests accepts JVM property options:
* -Dpthserver.type, type of server: 'S' for dedicated, 'O' for official (default)
* -Dpthserver.passwd, password for dedicated server
* -Dpthserver.numthreads, number of threads (guests users) launched (5 default)
* -Dpthserver.waitfor, time all users remain connected until released.
* Format: XXX[m,s,M] for m(milliseconds), s(seconds), M(minutes). Eg: 1M (1 minute), 45s (45 seconds)
* Default: 1s
*
* For DEDICATED_SERVER Server must be faked to accept guest connections (aren't allowed by default).
*
* Purpose:
*
* Final purpose of this test, is check whether a guest connection isn't permitted
* once the maximum number of guests players allowed is reached
* To achieve that, N (NUM_THREADS) must be SERVER_MAX_GUEST_USERS + 1. That value is defined in ServerLobbyThread
* In that case, test will fail, because it's not supposed to receive that error message.
* Test must be corrected:
* - Last thread launched (or the one that makes SERVER_MAX_GUEST_USERS + 1, and next ones), must receive an error
* - To do that, msg.hasErrorMessage() must be true, and reason (msg.getErrorMessage().getErrorReason())
* must be ERR_NET_SERVER_FULL (error gameIsFull)
*
*
* TODO:
*
* This test must be modified to adapt to:
* - Java8 (much simple threads) Runnable name = () -> {code}
* - Modern concurrency design: using Executors, Barriers and CountDownLatch
* - Test a guarantee exception when limit is reached.
*
* @author albmed
*
* @return
* @throws Exception
*/
public int guestInitThreaded() throws Exception {
String serverType = System.getProperty("pthserver.type");
String serverPasswd = System.getProperty("pthserver.passwd");
String numThreadsStr = System.getProperty("pthserver.numthreads");
String sleepForStr = System.getProperty("pthserver.waitfor");
// if serverType not provided or wrong, suppose official server
if (serverType == null || !serverType.matches("[OoSs]")) {
serverType = "O";
serverPasswd = "";
}
// if dedicated server passwd is mandatory
if (serverType.equalsIgnoreCase("S") && (serverPasswd == null || serverPasswd.trim().length() == 0))
fail("Server Passwd needed on Dedicated server");
// if numThreads not provided supposed 5 (must match with SERVER_MAX_GUEST_USERS)
int numThreads = 5;
if (numThreadsStr != null && numThreadsStr.matches("[0-9]+"))
numThreads = Integer.valueOf(numThreadsStr).intValue();
// waits for, at least 1sec, when all threads are alive and waiting
// can wait longer if, a one want to do a test with a real client.
// Format is: time[unit], where unit is "m" (milli), "s" (seconds) or "M" (minutes)
long sleepFor = 1000L;
if (sleepForStr != null && sleepForStr.matches("([0-9]+)([msM]?)")) {
long units = 1L;
if ((sleepForStr.charAt(sleepForStr.length() - 1) + "").matches("[msM]")) {
switch (sleepForStr.charAt(sleepForStr.length() - 1)) {
case 'm': units = 1L; break; // milliseconds
case 's': units = 1000L; break; // seconds
case 'M': units = 60L*1000L; break; // minutes
default: break;
}
sleepForStr = sleepForStr.substring(0, sleepForStr.length() -1);
}
long tmp = Long.valueOf(sleepForStr).longValue() * units;
if (tmp > sleepFor) sleepFor = tmp;
}
class ThreadGuest implements Runnable {
Thread t;
Object lock;
String name;
String type;
Socket s;
String passwd;
public ThreadGuest(String _name, String _type, String _passwd, Object _lock) {
this.name = _name;
if (_lock == null) lock = new Object();
this.lock = _lock;
this.type = _type;
this.passwd = _passwd;
}
public Object getLock() { return this.lock; }
public Thread getThread() { return this.t; }
public void start() {
if (t == null) {
t = new Thread(this);
t.start();
}
}
@Override
public void run() {
int playerId = connect();
System.out.println("[" + System.currentTimeMillis() + " - " + name + "]: PlayerId: " + playerId + " connected");
// Wait to make all guests users connected at once
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("[" + System.currentTimeMillis() + " - " + name + "]: PlayerId: " + playerId + " released");
// Close conn
if (s != null && !s.isClosed()) try {s.close();} catch (IOException e) {}
}
public int connect() {
Exception excep = null;
int playerId = -1;
try {
s = new Socket("localhost", 7234);
PokerTHMessage msg = receiveMessage(s);
assertTrue(msg.hasAnnounceMessage());
if (type.equalsIgnoreCase("S")) assertTrue(msg.getAnnounceMessage().getServerType() == ServerType.serverTypeInternetNoAuth);
AnnounceMessage.Version requestedVersion = AnnounceMessage.Version.newBuilder()
.setMajorVersion(PROTOCOL_VERSION_MAJOR)
.setMinorVersion(PROTOCOL_VERSION_MINOR)
.build();
InitMessage init;
if (type.equalsIgnoreCase("S")) {
init = InitMessage.newBuilder()
.setBuildId(0)
.setLogin(InitMessage.LoginType.unauthenticatedLogin)
.setRequestedVersion(requestedVersion)
.setNickName(name).setAuthServerPassword(passwd)
.build();
}
else {
init = InitMessage.newBuilder()
.setBuildId(0)
.setLogin(InitMessage.LoginType.guestLogin)
.setRequestedVersion(requestedVersion)
.setNickName(name)
.build();
}
msg = PokerTHMessage.newBuilder()
.setMessageType(PokerTHMessageType.Type_InitMessage)
.setInitMessage(init)
.build();
sendMessage(msg, s);
msg = receiveMessage(s);
failOnErrorMessage(msg);
if (msg.hasInitAckMessage() && msg.getMessageType() == PokerTHMessageType.Type_InitAckMessage) {
InitAckMessage initAck = msg.getInitAckMessage();
assertTrue(initAck.getYourPlayerId() != 0L);
assertTrue(!initAck.hasYourAvatarHash());
playerId = initAck.getYourPlayerId();
}
else {
failOnErrorMessage(msg);
fail("Invalid message.");
}
}
catch (Exception e) {
excep = e;
e.printStackTrace();
}
finally {
if (excep != null) {
if (s != null) try {s.close();} catch (IOException e) {}
}
}
return playerId;
}
}
ThreadGuest[] threads = new ThreadGuest[numThreads];
for (int i = 0; i < threads.length; i++) {
threads[i] = new ThreadGuest("Guest" + String.format("%05d",i), serverType, serverPasswd, new Object());
threads[i].start();
// Must wait over a second, before start next thread, to avoid error 133 (ERR_NET_INIT_BLOCKED)
// Don't know why this don't works. I had to disable ServerBruteForceProtection on server config file.
// So, this can be reduced to a few milliseconds (better not to comment or remove).
Thread.sleep(1500L);
}
// All threads are running and waiting.
Thread.sleep(sleepFor);
// Unlock, and let threads die.
for (int i = 0; i < threads.length; i++) {
Object lock = threads[i].getLock();
synchronized (lock) {
lock.notify();
}
try {
threads[i].getThread().join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return -1;
}
}