fixed tests

This commit is contained in:
Oleg Agafonov 2024-06-07 13:34:49 +04:00
parent c2ae1386ff
commit de323a3577
6 changed files with 38 additions and 13 deletions

View file

@ -31,6 +31,7 @@ import mage.target.TargetAmount;
import mage.target.TargetCard; import mage.target.TargetCard;
import mage.util.CardUtil; import mage.util.CardUtil;
import mage.util.RandomUtil; import mage.util.RandomUtil;
import mage.util.ThreadUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.util.*; import java.util.*;
@ -61,7 +62,7 @@ public class ComputerPlayer6 extends ComputerPlayer {
new LinkedBlockingQueue<>(), new LinkedBlockingQueue<>(),
r -> { r -> {
Thread thread = new Thread(r); Thread thread = new Thread(r);
thread.setName("AI-SIM-" + thread.getId()); thread.setName(ThreadUtils.THREAD_PREFIX_AI_SIMULATION + "-" + thread.getId());
return thread; return thread;
}); });
protected int maxDepth; protected int maxDepth;

View file

@ -9,6 +9,7 @@ import mage.game.events.TableEvent;
import mage.players.Player; import mage.players.Player;
import mage.server.game.GameController; import mage.server.game.GameController;
import mage.server.managers.ManagerFactory; import mage.server.managers.ManagerFactory;
import mage.util.ThreadUtils;
import mage.view.DraftPickView; import mage.view.DraftPickView;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -124,7 +125,7 @@ public class DraftController {
} }
private void startDraft() { private void startDraft() {
Thread.currentThread().setName("TOURNEY DRAFT " + tableId); Thread.currentThread().setName(ThreadUtils.THREAD_PREFIX_TOURNEY_DRAFT + " " + tableId);
for (final Entry<UUID, DraftSession> entry : draftSessions.entrySet()) { for (final Entry<UUID, DraftSession> entry : draftSessions.entrySet()) {
if (!entry.getValue().init()) { if (!entry.getValue().init()) {
logger.fatal("Unable to initialize client for playerId " + entry.getKey()); logger.fatal("Unable to initialize client for playerId " + entry.getKey());

View file

@ -2,6 +2,7 @@ package mage.server.game;
import mage.MageException; import mage.MageException;
import mage.game.Game; import mage.game.Game;
import mage.util.ThreadUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.util.UUID; import java.util.UUID;
@ -30,7 +31,7 @@ public class GameWorker implements Callable<Boolean> {
public Boolean call() { public Boolean call() {
try { try {
// play game // play game
Thread.currentThread().setName("GAME " + game.getId()); Thread.currentThread().setName(ThreadUtils.THREAD_PREFIX_GAME + " " + game.getId());
game.start(choosingPlayerId); game.start(choosingPlayerId);
// save result and start next game or close finished table // save result and start next game or close finished table

View file

@ -23,6 +23,7 @@ import mage.server.User;
import mage.server.draft.DraftController; import mage.server.draft.DraftController;
import mage.server.managers.TableManager; import mage.server.managers.TableManager;
import mage.server.managers.ManagerFactory; import mage.server.managers.ManagerFactory;
import mage.util.ThreadUtils;
import mage.view.ChatMessage.MessageColor; import mage.view.ChatMessage.MessageColor;
import mage.view.ChatMessage.MessageType; import mage.view.ChatMessage.MessageType;
import mage.view.ChatMessage.SoundToPlay; import mage.view.ChatMessage.SoundToPlay;
@ -191,7 +192,7 @@ public class TournamentController {
} }
private synchronized void startTournament() { private synchronized void startTournament() {
Thread.currentThread().setName("TOURNEY " + tableId); Thread.currentThread().setName(ThreadUtils.THREAD_PREFIX_TOURNEY + " " + tableId);
for (final TournamentSession tournamentSession : tournamentSessions.values()) { for (final TournamentSession tournamentSession : tournamentSessions.values()) {
if (!tournamentSession.init()) { if (!tournamentSession.init()) {
logger.fatal("Unable to initialize client userId: " + tournamentSession.userId + " tournamentId " + tournament.getId()); logger.fatal("Unable to initialize client userId: " + tournamentSession.userId + " tournamentId " + tournament.getId());

View file

@ -41,31 +41,31 @@ public class ThreadExecutorImpl implements ThreadExecutor {
callExecutor = new CachedThreadPoolWithException(); callExecutor = new CachedThreadPoolWithException();
((ThreadPoolExecutor) callExecutor).setKeepAliveTime(60, TimeUnit.SECONDS); ((ThreadPoolExecutor) callExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor) callExecutor).allowCoreThreadTimeOut(true); ((ThreadPoolExecutor) callExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor) callExecutor).setThreadFactory(new XMageThreadFactory("CALL")); ((ThreadPoolExecutor) callExecutor).setThreadFactory(new XMageThreadFactory(ThreadUtils.THREAD_PREFIX_CALL_REQUEST));
//gameExecutor = Executors.newFixedThreadPool(config.getMaxGameThreads()); //gameExecutor = Executors.newFixedThreadPool(config.getMaxGameThreads());
gameExecutor = new FixedThreadPoolWithException(config.getMaxGameThreads()); gameExecutor = new FixedThreadPoolWithException(config.getMaxGameThreads());
((ThreadPoolExecutor) gameExecutor).setKeepAliveTime(60, TimeUnit.SECONDS); ((ThreadPoolExecutor) gameExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor) gameExecutor).allowCoreThreadTimeOut(true); ((ThreadPoolExecutor) gameExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor) gameExecutor).setThreadFactory(new XMageThreadFactory("GAME")); ((ThreadPoolExecutor) gameExecutor).setThreadFactory(new XMageThreadFactory(ThreadUtils.THREAD_PREFIX_GAME));
//tourney = Executors.newFixedThreadPool(config.getMaxGameThreads() / GAMES_PER_TOURNEY_RATIO); //tourney = Executors.newFixedThreadPool(config.getMaxGameThreads() / GAMES_PER_TOURNEY_RATIO);
tourneyExecutor = new FixedThreadPoolWithException(config.getMaxGameThreads() / GAMES_PER_TOURNEY_RATIO); tourneyExecutor = new FixedThreadPoolWithException(config.getMaxGameThreads() / GAMES_PER_TOURNEY_RATIO);
((ThreadPoolExecutor) tourneyExecutor).setKeepAliveTime(60, TimeUnit.SECONDS); ((ThreadPoolExecutor) tourneyExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor) tourneyExecutor).allowCoreThreadTimeOut(true); ((ThreadPoolExecutor) tourneyExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor) tourneyExecutor).setThreadFactory(new XMageThreadFactory("TOURNEY")); ((ThreadPoolExecutor) tourneyExecutor).setThreadFactory(new XMageThreadFactory(ThreadUtils.THREAD_PREFIX_TOURNEY));
timeoutExecutor = Executors.newScheduledThreadPool(4); timeoutExecutor = Executors.newScheduledThreadPool(4);
((ThreadPoolExecutor) timeoutExecutor).setKeepAliveTime(60, TimeUnit.SECONDS); ((ThreadPoolExecutor) timeoutExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor) timeoutExecutor).allowCoreThreadTimeOut(true); ((ThreadPoolExecutor) timeoutExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor) timeoutExecutor).setThreadFactory(new XMageThreadFactory("XMAGE TIMEOUT")); ((ThreadPoolExecutor) timeoutExecutor).setThreadFactory(new XMageThreadFactory(ThreadUtils.THREAD_PREFIX_TIMEOUT));
timeoutIdleExecutor = Executors.newScheduledThreadPool(4); timeoutIdleExecutor = Executors.newScheduledThreadPool(4);
((ThreadPoolExecutor) timeoutIdleExecutor).setKeepAliveTime(60, TimeUnit.SECONDS); ((ThreadPoolExecutor) timeoutIdleExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor) timeoutIdleExecutor).allowCoreThreadTimeOut(true); ((ThreadPoolExecutor) timeoutIdleExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor) timeoutIdleExecutor).setThreadFactory(new XMageThreadFactory("XMAGE TIMEOUT_IDLE")); ((ThreadPoolExecutor) timeoutIdleExecutor).setThreadFactory(new XMageThreadFactory(ThreadUtils.THREAD_PREFIX_TIMEOUT_IDLE));
serverHealthExecutor = Executors.newSingleThreadScheduledExecutor(new XMageThreadFactory("HEALTH")); serverHealthExecutor = Executors.newSingleThreadScheduledExecutor(new XMageThreadFactory(ThreadUtils.THREAD_PREFIX_SERVICE_HEALTH));
} }
static class CachedThreadPoolWithException extends ThreadPoolExecutor { static class CachedThreadPoolWithException extends ThreadPoolExecutor {
@ -157,7 +157,8 @@ class XMageThreadFactory implements ThreadFactory {
@Override @Override
public Thread newThread(Runnable r) { public Thread newThread(Runnable r) {
Thread thread = new Thread(r); Thread thread = new Thread(r);
thread.setName(prefix + ' ' + thread.getThreadGroup().getName() + '-' + thread.getId()); // default name, but threads can change it (example: on game or tourney start)
thread.setName(prefix + " " + thread.getThreadGroup().getName() + "-" + thread.getId());
return thread; return thread;
} }
} }

View file

@ -14,6 +14,23 @@ import java.util.concurrent.Future;
*/ */
public final class ThreadUtils { public final class ThreadUtils {
// basic
public final static String THREAD_PREFIX_GAME = "GAME";
public final static String THREAD_PREFIX_AI_SIMULATION = "AI-SIM";
public final static String THREAD_PREFIX_CALL_REQUEST = "CALL";
public final static String THREAD_PREFIX_TOURNEY = "TOURNEY";
public final static String THREAD_PREFIX_TOURNEY_DRAFT = "TOURNEY DRAFT";
// services
public final static String THREAD_PREFIX_SERVICE_HEALTH = "XMAGE HEALTH";
// etc
public final static String THREAD_PREFIX_TIMEOUT = "XMAGE TIMEOUT";
public final static String THREAD_PREFIX_TIMEOUT_IDLE = "XMAGE TIMEOUT_IDLE";
public static void sleep(int millis) { public static void sleep(int millis) {
try { try {
Thread.sleep(millis); Thread.sleep(millis);
@ -68,9 +85,12 @@ public final class ThreadUtils {
public static boolean isRunGameThread() { public static boolean isRunGameThread() {
String name = Thread.currentThread().getName(); String name = Thread.currentThread().getName();
if (name.startsWith("GAME ")) { if (name.startsWith(THREAD_PREFIX_GAME)) {
// server game // server game
return true; return true;
} else if (name.startsWith(THREAD_PREFIX_AI_SIMULATION)) {
// ai simulation
return true;
} else if (name.equals("main")) { } else if (name.equals("main")) {
// unit test // unit test
return true; return true;
@ -81,7 +101,7 @@ public final class ThreadUtils {
public static void ensureRunInCallThread() { public static void ensureRunInCallThread() {
String name = Thread.currentThread().getName(); String name = Thread.currentThread().getName();
if (!name.startsWith("CALL")) { if (!name.startsWith(THREAD_PREFIX_CALL_REQUEST)) {
// how-to fix: something wrong in your code logic // how-to fix: something wrong in your code logic
throw new IllegalArgumentException("Wrong code usage: client commands code must run in CALL threads, but used in " + name, new Throwable()); throw new IllegalArgumentException("Wrong code usage: client commands code must run in CALL threads, but used in " + name, new Throwable());
} }