fix metric saving for running in tests
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 2m10s

This commit is contained in:
Lee 2024-04-19 22:42:32 +01:00
parent 5871c64582
commit e5935c6696
5 changed files with 23 additions and 10 deletions

@ -4,7 +4,7 @@ import lombok.Getter;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
@UtilityClass @UtilityClass
public final class EnvironmentUtils { public final class AppConfig {
/** /**
* Is the app running in a production environment? * Is the app running in a production environment?
*/ */
@ -14,4 +14,17 @@ public final class EnvironmentUtils {
String env = System.getenv("ENVIRONMENT"); String env = System.getenv("ENVIRONMENT");
production = env != null && (env.equals("production")); production = env != null && (env.equals("production"));
} }
/**
* Is the app running in a test environment?
*/
@Getter
private static boolean isRunningTest = true;
static {
try {
Class.forName("org.junit.Test");
} catch (ClassNotFoundException e) {
isRunningTest = false;
}
}
} }

@ -6,7 +6,7 @@ import com.influxdb.spring.influx.InfluxDB2AutoConfiguration;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import xyz.mcutils.backend.common.EnvironmentUtils; import xyz.mcutils.backend.common.AppConfig;
import xyz.mcutils.backend.common.Timer; import xyz.mcutils.backend.common.Timer;
import xyz.mcutils.backend.repository.mongo.MetricsRepository; import xyz.mcutils.backend.repository.mongo.MetricsRepository;
import xyz.mcutils.backend.service.metric.Metric; import xyz.mcutils.backend.service.metric.Metric;
@ -56,7 +56,7 @@ public class MetricService {
collectorEnabled.put(metric, metric.isCollector()); collectorEnabled.put(metric, metric.isCollector());
} }
if (EnvironmentUtils.isProduction()) { if (!AppConfig.isRunningTest()) {
// Load the metrics from Redis // Load the metrics from Redis
loadMetrics(); loadMetrics();

@ -11,7 +11,7 @@ import lombok.extern.log4j.Log4j2;
import net.jodah.expiringmap.ExpirationPolicy; import net.jodah.expiringmap.ExpirationPolicy;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import xyz.mcutils.backend.common.EnvironmentUtils; import xyz.mcutils.backend.common.AppConfig;
import xyz.mcutils.backend.common.ExpiringSet; import xyz.mcutils.backend.common.ExpiringSet;
import xyz.mcutils.backend.common.WebRequest; import xyz.mcutils.backend.common.WebRequest;
import xyz.mcutils.backend.model.cache.CachedEndpointStatus; import xyz.mcutils.backend.model.cache.CachedEndpointStatus;
@ -186,7 +186,7 @@ public class MojangService {
public CachedEndpointStatus getMojangApiStatus() { public CachedEndpointStatus getMojangApiStatus() {
log.info("Getting Mojang API status"); log.info("Getting Mojang API status");
Optional<CachedEndpointStatus> endpointStatus = mojangEndpointStatusRepository.findById(MOJANG_ENDPOINT_STATUS_KEY); Optional<CachedEndpointStatus> endpointStatus = mojangEndpointStatusRepository.findById(MOJANG_ENDPOINT_STATUS_KEY);
if (endpointStatus.isPresent() && EnvironmentUtils.isProduction()) { if (endpointStatus.isPresent() && AppConfig.isProduction()) {
log.info("Got cached Mojang API status"); log.info("Got cached Mojang API status");
return endpointStatus.get(); return endpointStatus.get();
} }

@ -61,7 +61,7 @@ public class PlayerService {
} }
Optional<CachedPlayer> cachedPlayer = playerCacheRepository.findById(uuid); Optional<CachedPlayer> cachedPlayer = playerCacheRepository.findById(uuid);
if (cachedPlayer.isPresent() && EnvironmentUtils.isProduction()) { // Return the cached player if it exists if (cachedPlayer.isPresent() && AppConfig.isProduction()) { // Return the cached player if it exists
log.info("Player {} is cached", id); log.info("Player {} is cached", id);
return cachedPlayer.get(); return cachedPlayer.get();
} }
@ -105,7 +105,7 @@ public class PlayerService {
log.info("Getting UUID from username: {}", username); log.info("Getting UUID from username: {}", username);
String id = username.toUpperCase(); String id = username.toUpperCase();
Optional<CachedPlayerName> cachedPlayerName = playerNameCacheRepository.findById(id); Optional<CachedPlayerName> cachedPlayerName = playerNameCacheRepository.findById(id);
if (cachedPlayerName.isPresent() && EnvironmentUtils.isProduction()) { if (cachedPlayerName.isPresent() && AppConfig.isProduction()) {
return cachedPlayerName.get(); return cachedPlayerName.get();
} }
try { try {
@ -155,7 +155,7 @@ public class PlayerService {
Optional<CachedPlayerSkinPart> cache = playerSkinPartCacheRepository.findById(key); Optional<CachedPlayerSkinPart> cache = playerSkinPartCacheRepository.findById(key);
// The skin part is cached // The skin part is cached
if (cache.isPresent() && EnvironmentUtils.isProduction()) { if (cache.isPresent() && AppConfig.isProduction()) {
log.info("Skin part {} for player {} is cached", name, player.getUniqueId()); log.info("Skin part {} for player {} is cached", name, player.getUniqueId());
return cache.get(); return cache.get();
} }

@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import xyz.mcutils.backend.common.DNSUtils; import xyz.mcutils.backend.common.DNSUtils;
import xyz.mcutils.backend.common.EnumUtils; import xyz.mcutils.backend.common.EnumUtils;
import xyz.mcutils.backend.common.EnvironmentUtils; import xyz.mcutils.backend.common.AppConfig;
import xyz.mcutils.backend.exception.impl.BadRequestException; import xyz.mcutils.backend.exception.impl.BadRequestException;
import xyz.mcutils.backend.exception.impl.ResourceNotFoundException; import xyz.mcutils.backend.exception.impl.ResourceNotFoundException;
import xyz.mcutils.backend.model.cache.CachedMinecraftServer; import xyz.mcutils.backend.model.cache.CachedMinecraftServer;
@ -67,7 +67,7 @@ public class ServerService {
// Check if the server is cached // Check if the server is cached
Optional<CachedMinecraftServer> cached = serverCacheRepository.findById(key); Optional<CachedMinecraftServer> cached = serverCacheRepository.findById(key);
if (cached.isPresent() && EnvironmentUtils.isProduction()) { if (cached.isPresent() && AppConfig.isProduction()) {
log.info("Server {}:{} is cached", hostname, port); log.info("Server {}:{} is cached", hostname, port);
return cached.get(); return cached.get();
} }