mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Restore the Spigot and Paper timeouts Iris raised when auto-configuration is off
Iris rewrote spigot.yml timeout-time and paper-global.yml early-warning-delay without keeping the old values, so turning the setting off never undid it. The originals are now recorded in server-timeouts.json before the raise and restored on later starts unless the operator has changed the value since. Writes are atomic and go through symbolic links to the real file. The recovery bookkeeping reads server YAML through a small interface that ServerConfigurator implements, so the new class carries no Bukkit coupling.
Upstream: b357eee18
This commit is contained in:
1 parent
041e11f4a9
commit
7e4c159039
3 files changed
+437
-30
No files matched your search
@@ -46,7 +46,6 @@ import lombok.NonNull;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -92,6 +91,9 @@ public class ServerConfigurator {
|
||||
private static final String COMPILER_INPUT_FINGERPRINT_CACHE = "datapack-compiler-input-fingerprint";
|
||||
static final String POST_COMPILE_RESTART_WARNING = "Iris installed updated datapack registry entries; "
|
||||
+ "restart the server before creating worlds or opening Studios.";
|
||||
private static final Path SPIGOT_CONFIGURATION = Path.of("spigot.yml");
|
||||
private static final Path PAPER_GLOBAL_CONFIGURATION = Path.of("config", "paper-global.yml");
|
||||
private static final String TIMEOUT_RECOVERY_FILE = "server-timeouts.json";
|
||||
private static final int FINGERPRINT_BUFFER_BYTES = 64 * 1024;
|
||||
private static volatile boolean loadedDatapackRuntimeReady;
|
||||
private static volatile String loadedDatapackCompilerInputFingerprint = "";
|
||||
@@ -108,11 +110,27 @@ public class ServerConfigurator {
|
||||
}
|
||||
IrisSettings.IrisSettingsAutoconfiguration s = IrisSettings.get().getAutoConfiguration();
|
||||
if (s.isConfigureSpigotTimeoutTime()) {
|
||||
J.attempt(ServerConfigurator::increaseKeepAliveSpigot);
|
||||
Throwable failure = J.attemptCatch(ServerConfigurator::increaseKeepAliveSpigot);
|
||||
if (failure != null) {
|
||||
IrisLogging.reportError("Unable to persist the Spigot timeout setting.", failure);
|
||||
}
|
||||
} else {
|
||||
Throwable failure = J.attemptCatch(() -> timeoutConfiguration().restore(ServerTimeoutConfiguration.SPIGOT_TIMEOUT_KEY));
|
||||
if (failure != null) {
|
||||
IrisLogging.reportError("Unable to restore the Spigot timeout setting.", failure);
|
||||
}
|
||||
}
|
||||
|
||||
if (s.isConfigurePaperWatchdogDelay()) {
|
||||
J.attempt(ServerConfigurator::increasePaperWatchdog);
|
||||
Throwable failure = J.attemptCatch(ServerConfigurator::increasePaperWatchdog);
|
||||
if (failure != null) {
|
||||
IrisLogging.reportError("Unable to persist the Paper watchdog setting.", failure);
|
||||
}
|
||||
} else {
|
||||
Throwable failure = J.attemptCatch(() -> timeoutConfiguration().restore(ServerTimeoutConfiguration.PAPER_WATCHDOG_KEY));
|
||||
if (failure != null) {
|
||||
IrisLogging.reportError("Unable to restore the Paper watchdog setting.", failure);
|
||||
}
|
||||
}
|
||||
|
||||
if (DefaultPackBootstrapProvisioner.wasProvisionedThisStartup()) {
|
||||
@@ -240,33 +258,81 @@ public class ServerConfigurator {
|
||||
}
|
||||
|
||||
private static void increaseKeepAliveSpigot() throws IOException, InvalidConfigurationException {
|
||||
File spigotConfig = new File("spigot.yml");
|
||||
FileConfiguration f = new YamlConfiguration();
|
||||
f.load(spigotConfig);
|
||||
long tt = f.getLong("settings.timeout-time");
|
||||
increaseKeepAliveSpigot(SPIGOT_CONFIGURATION, timeoutConfiguration());
|
||||
}
|
||||
|
||||
long spigotTimeout = TimeUnit.MINUTES.toSeconds(5);
|
||||
|
||||
if (tt < spigotTimeout) {
|
||||
IrisLogging.warn("Updating spigot.yml timeout-time: " + tt + " -> " + spigotTimeout + " (5 minutes)");
|
||||
IrisLogging.warn("You can disable this change (autoconfigureServer) in Iris settings, then change back the value.");
|
||||
f.set("settings.timeout-time", spigotTimeout);
|
||||
f.save(spigotConfig);
|
||||
static void increaseKeepAliveSpigot(Path file, ServerTimeoutConfiguration timeouts) throws IOException, InvalidConfigurationException {
|
||||
long previous = loadConfiguration(file).getLong(ServerTimeoutConfiguration.SPIGOT_TIMEOUT_KEY);
|
||||
long minimum = TimeUnit.MINUTES.toSeconds(5);
|
||||
if (previous >= minimum) {
|
||||
return;
|
||||
}
|
||||
timeouts.raise(file, ServerTimeoutConfiguration.SPIGOT_TIMEOUT_KEY, minimum);
|
||||
IrisLogging.warn("Updated " + file + " settings.timeout-time: " + previous + " -> " + minimum
|
||||
+ " seconds. Takes effect after a server restart.");
|
||||
IrisLogging.warn("Disable autoConfiguration.configureSpigotTimeoutTime in iris.json to restore the original value.");
|
||||
}
|
||||
|
||||
private static void increasePaperWatchdog() throws IOException, InvalidConfigurationException {
|
||||
File spigotConfig = new File("config/paper-global.yml");
|
||||
FileConfiguration f = new YamlConfiguration();
|
||||
f.load(spigotConfig);
|
||||
long tt = f.getLong("watchdog.early-warning-delay");
|
||||
increasePaperWatchdog(PAPER_GLOBAL_CONFIGURATION, timeoutConfiguration());
|
||||
}
|
||||
|
||||
long watchdog = TimeUnit.MINUTES.toMillis(3);
|
||||
if (tt < watchdog) {
|
||||
IrisLogging.warn("Updating paper.yml watchdog early-warning-delay: " + tt + " -> " + watchdog + " (3 minutes)");
|
||||
IrisLogging.warn("You can disable this change (autoconfigureServer) in Iris settings, then change back the value.");
|
||||
f.set("watchdog.early-warning-delay", watchdog);
|
||||
f.save(spigotConfig);
|
||||
static void increasePaperWatchdog(Path file, ServerTimeoutConfiguration timeouts) throws IOException, InvalidConfigurationException {
|
||||
long previous = loadConfiguration(file).getLong(ServerTimeoutConfiguration.PAPER_WATCHDOG_KEY);
|
||||
long minimum = TimeUnit.MINUTES.toMillis(3);
|
||||
if (previous >= minimum) {
|
||||
return;
|
||||
}
|
||||
timeouts.raise(file, ServerTimeoutConfiguration.PAPER_WATCHDOG_KEY, minimum);
|
||||
IrisLogging.warn("Updated " + file + " watchdog.early-warning-delay: " + previous + " -> " + minimum
|
||||
+ " milliseconds. Takes effect after a server restart.");
|
||||
IrisLogging.warn("Disable autoConfiguration.configurePaperWatchdogDelay in iris.json to restore the original value.");
|
||||
}
|
||||
|
||||
private static YamlConfiguration loadConfiguration(Path file) throws IOException, InvalidConfigurationException {
|
||||
YamlConfiguration configuration = new YamlConfiguration();
|
||||
configuration.load(file.toFile());
|
||||
return configuration;
|
||||
}
|
||||
|
||||
private static ServerTimeoutConfiguration timeoutConfiguration() {
|
||||
return new ServerTimeoutConfiguration(IrisPlatforms.get().dataFolder().toPath().resolve(TIMEOUT_RECOVERY_FILE), serverYaml());
|
||||
}
|
||||
|
||||
static ServerTimeoutConfiguration.ServerYaml serverYaml() {
|
||||
return file -> {
|
||||
try {
|
||||
return new BukkitYamlDocument(loadConfiguration(file));
|
||||
} catch (InvalidConfigurationException exception) {
|
||||
throw new IOException("Invalid server configuration: " + file, exception);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private record BukkitYamlDocument(YamlConfiguration configuration) implements ServerTimeoutConfiguration.Document {
|
||||
@Override
|
||||
public boolean isSet(String key) {
|
||||
return configuration.isSet(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger(String key) {
|
||||
return configuration.isInt(key) || configuration.isLong(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLong(String key) {
|
||||
return configuration.getLong(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String key, Long value) {
|
||||
configuration.set(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String saveToString() {
|
||||
return configuration.saveToString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -852,7 +918,7 @@ public class ServerConfigurator {
|
||||
|
||||
private static void writeFingerprintCache(Path cacheFile, PackFingerprint fingerprint) {
|
||||
try {
|
||||
writeFingerprintAtomic(cacheFile, fingerprint.content() + "\n" + fingerprint.metadata());
|
||||
writeStringAtomic(cacheFile, fingerprint.content() + "\n" + fingerprint.metadata());
|
||||
} catch (IOException e) {
|
||||
IrisLogging.warn("Failed to write datapack fingerprint cache: " + e.getMessage());
|
||||
}
|
||||
@@ -860,23 +926,23 @@ public class ServerConfigurator {
|
||||
|
||||
private static void writeCompilerInputFingerprintCache(Path cacheFile, String fingerprint) {
|
||||
try {
|
||||
writeFingerprintAtomic(cacheFile, fingerprint);
|
||||
writeStringAtomic(cacheFile, fingerprint);
|
||||
} catch (IOException exception) {
|
||||
IrisLogging.warn("Failed to write datapack compiler-input fingerprint cache: "
|
||||
+ exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeFingerprintAtomic(Path target, String fingerprint) throws IOException {
|
||||
static void writeStringAtomic(Path target, String content) throws IOException {
|
||||
Path absoluteTarget = target.toAbsolutePath().normalize();
|
||||
Path parent = absoluteTarget.getParent();
|
||||
if (parent == null) {
|
||||
throw new IOException("Datapack fingerprint target has no parent: " + absoluteTarget);
|
||||
throw new IOException("Atomic write target has no parent: " + absoluteTarget);
|
||||
}
|
||||
Files.createDirectories(parent);
|
||||
Path staged = Files.createTempFile(parent, ".datapack-fingerprint-", ".tmp");
|
||||
Path staged = Files.createTempFile(parent, ".iris-write-", ".tmp");
|
||||
try {
|
||||
Files.writeString(staged, fingerprint, StandardCharsets.UTF_8);
|
||||
Files.writeString(staged, content, StandardCharsets.UTF_8);
|
||||
try (FileChannel channel = FileChannel.open(staged, StandardOpenOption.WRITE)) {
|
||||
channel.force(true);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
package art.arcane.iris.core;
|
||||
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
final class ServerTimeoutConfiguration {
|
||||
static final String SPIGOT_TIMEOUT_KEY = "settings.timeout-time";
|
||||
static final String PAPER_WATCHDOG_KEY = "watchdog.early-warning-delay";
|
||||
|
||||
private static final Object MUTATION_LOCK = new Object();
|
||||
private static final Gson JSON = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
|
||||
|
||||
interface ServerYaml {
|
||||
Document load(Path file) throws IOException;
|
||||
}
|
||||
|
||||
interface Document {
|
||||
boolean isSet(String key);
|
||||
|
||||
boolean isInteger(String key);
|
||||
|
||||
long getLong(String key);
|
||||
|
||||
void set(String key, Long value);
|
||||
|
||||
String saveToString();
|
||||
}
|
||||
|
||||
private final Path recoveryFile;
|
||||
private final ServerYaml yaml;
|
||||
|
||||
ServerTimeoutConfiguration(Path recoveryFile, ServerYaml yaml) {
|
||||
this.recoveryFile = Objects.requireNonNull(recoveryFile);
|
||||
this.yaml = Objects.requireNonNull(yaml);
|
||||
}
|
||||
|
||||
void raise(Path file, String key, long minimum) throws IOException {
|
||||
synchronized (MUTATION_LOCK) {
|
||||
Path target = file.toRealPath();
|
||||
Document configuration = yaml.load(target);
|
||||
Long original = value(configuration, key);
|
||||
List<SavedTimeout> changes = readChanges();
|
||||
SavedTimeout saved = find(changes, target, key);
|
||||
if (saved == null || !Objects.equals(original, saved.original()) && !Objects.equals(original, saved.applied())) {
|
||||
changes.remove(saved);
|
||||
changes.add(new SavedTimeout(target.toString(), key, original, minimum));
|
||||
writeChanges(changes);
|
||||
}
|
||||
configuration.set(key, minimum);
|
||||
ServerConfigurator.writeStringAtomic(target, configuration.saveToString());
|
||||
}
|
||||
}
|
||||
|
||||
void restore(String key) throws IOException {
|
||||
synchronized (MUTATION_LOCK) {
|
||||
List<SavedTimeout> changes = readChanges();
|
||||
for (SavedTimeout saved : List.copyOf(changes)) {
|
||||
if (!saved.key().equals(key)) {
|
||||
continue;
|
||||
}
|
||||
restore(saved);
|
||||
changes.remove(saved);
|
||||
writeChanges(changes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void restore(SavedTimeout saved) throws IOException {
|
||||
Path target = Path.of(saved.file());
|
||||
if (!Files.exists(target)) {
|
||||
return;
|
||||
}
|
||||
Document configuration = yaml.load(target);
|
||||
if (!Objects.equals(value(configuration, saved.key()), saved.applied())) {
|
||||
IrisLogging.info("Kept the current " + target + " " + saved.key()
|
||||
+ " because it differs from the value saved by Iris.");
|
||||
return;
|
||||
}
|
||||
configuration.set(saved.key(), saved.original());
|
||||
ServerConfigurator.writeStringAtomic(target, configuration.saveToString());
|
||||
IrisLogging.info("Restored " + target + " " + saved.key() + " to "
|
||||
+ (saved.original() == null ? "its server default" : saved.original())
|
||||
+ ". Takes effect after a server restart.");
|
||||
}
|
||||
|
||||
private List<SavedTimeout> readChanges() throws IOException {
|
||||
if (!Files.exists(recoveryFile)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
try {
|
||||
SavedTimeouts saved = JSON.fromJson(Files.readString(recoveryFile), SavedTimeouts.class);
|
||||
if (saved == null) {
|
||||
throw new IOException("Empty server timeout recovery data: " + recoveryFile);
|
||||
}
|
||||
return new ArrayList<>(saved.changes());
|
||||
} catch (JsonParseException | IllegalArgumentException exception) {
|
||||
throw new IOException("Invalid server timeout recovery data: " + recoveryFile, exception);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeChanges(List<SavedTimeout> changes) throws IOException {
|
||||
ServerConfigurator.writeStringAtomic(recoveryFile, JSON.toJson(new SavedTimeouts(changes)));
|
||||
}
|
||||
|
||||
private static SavedTimeout find(List<SavedTimeout> changes, Path target, String key) {
|
||||
for (SavedTimeout change : changes) {
|
||||
if (change.file().equals(target.toString()) && change.key().equals(key)) {
|
||||
return change;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Long value(Document configuration, String key) throws IOException {
|
||||
if (!configuration.isSet(key)) {
|
||||
return null;
|
||||
}
|
||||
if (!configuration.isInteger(key)) {
|
||||
throw new IOException("Server timeout " + key + " must be an integer.");
|
||||
}
|
||||
return configuration.getLong(key);
|
||||
}
|
||||
|
||||
private record SavedTimeout(String file, String key, Long original, long applied) {
|
||||
private SavedTimeout {
|
||||
Objects.requireNonNull(file, "file");
|
||||
Objects.requireNonNull(key, "key");
|
||||
if (!Path.of(file).isAbsolute() || applied <= 0
|
||||
|| !key.equals(SPIGOT_TIMEOUT_KEY) && !key.equals(PAPER_WATCHDOG_KEY)) {
|
||||
throw new IllegalArgumentException("Invalid saved server timeout");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private record SavedTimeouts(List<SavedTimeout> changes) {
|
||||
private SavedTimeouts {
|
||||
changes = List.copyOf(changes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package art.arcane.iris.core;
|
||||
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.CALLS_REAL_METHODS;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
|
||||
public class ServerTimeoutConfigurationTest {
|
||||
private static final String SPIGOT_KEY = ServerTimeoutConfiguration.SPIGOT_TIMEOUT_KEY;
|
||||
private static final String PAPER_KEY = ServerTimeoutConfiguration.PAPER_WATCHDOG_KEY;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void originalValuesSurviveRestartsAndRestoreIndependently() throws Exception {
|
||||
Path spigot = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||
Path paper = configuration("paper-global.yml", "watchdog:\n early-warning-delay: 10000\n early-warning-every: 5000\n");
|
||||
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||
ServerConfigurator.increaseKeepAliveSpigot(spigot, timeouts());
|
||||
ServerConfigurator.increasePaperWatchdog(paper, timeouts());
|
||||
ServerConfigurator.increaseKeepAliveSpigot(spigot, timeouts());
|
||||
timeouts().restore(SPIGOT_KEY);
|
||||
assertEquals(60L, load(spigot).getLong(SPIGOT_KEY));
|
||||
assertEquals(180000L, load(paper).getLong(PAPER_KEY));
|
||||
timeouts().restore(PAPER_KEY);
|
||||
assertEquals(10000L, load(paper).getLong(PAPER_KEY));
|
||||
assertEquals(5000L, load(paper).getLong("watchdog.early-warning-every"));
|
||||
assertFalse(Files.readString(recoveryFile()).contains("spigot.yml"));
|
||||
assertFalse(Files.readString(recoveryFile()).contains("paper-global.yml"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void valueAtOrAboveTheMinimumIsLeftAlone() throws Exception {
|
||||
String original = "settings:\n timeout-time: 600\n";
|
||||
Path file = configuration("spigot.yml", original);
|
||||
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||
assertEquals(original, Files.readString(file));
|
||||
assertFalse(Files.exists(recoveryFile()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restoringAbsentSettingRemovesTheOverride() throws Exception {
|
||||
Path file = configuration("spigot.yml", "settings:\n unrelated: retained\n");
|
||||
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||
assertEquals(300L, load(file).getLong(SPIGOT_KEY));
|
||||
timeouts().restore(SPIGOT_KEY);
|
||||
assertFalse(load(file).isSet(SPIGOT_KEY));
|
||||
assertEquals("retained", load(file).getString("settings.unrelated"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restorePreservesOperatorChangesAndOtherSettings() throws Exception {
|
||||
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||
Files.writeString(file, "# Operator settings\nsettings:\n timeout-time: 90\n unrelated: retained\n");
|
||||
String changed = Files.readString(file);
|
||||
timeouts().restore(SPIGOT_KEY);
|
||||
assertEquals(changed, Files.readString(file));
|
||||
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||
timeouts().restore(SPIGOT_KEY);
|
||||
assertEquals(90L, load(file).getLong(SPIGOT_KEY));
|
||||
assertEquals("retained", load(file).getString("settings.unrelated"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void symlinkSurvivesRaiseAndRestore() throws Exception {
|
||||
Path target = configuration("shared-spigot.yml", "settings:\n timeout-time: 60\n");
|
||||
Path link = temporaryFolder.getRoot().toPath().resolve("spigot.yml");
|
||||
Files.createSymbolicLink(link, target.getFileName());
|
||||
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||
ServerConfigurator.increaseKeepAliveSpigot(link, timeouts());
|
||||
assertEquals(300L, load(target).getLong(SPIGOT_KEY));
|
||||
timeouts().restore(SPIGOT_KEY);
|
||||
assertTrue(Files.isSymbolicLink(link));
|
||||
assertEquals(60L, load(target).getLong(SPIGOT_KEY));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aLaterAdjustmentRecordsTheOperatorsNewOriginal() throws Exception {
|
||||
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||
Files.writeString(file, "settings:\n timeout-time: 90\n");
|
||||
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||
timeouts().restore(SPIGOT_KEY);
|
||||
assertEquals(90L, load(file).getLong(SPIGOT_KEY));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failedRecoveryWriteLeavesServerSettingsUntouched() throws Exception {
|
||||
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||
Path blocked = temporaryFolder.newFile("blocked").toPath();
|
||||
ServerTimeoutConfiguration timeouts = new ServerTimeoutConfiguration(blocked.resolve("server-timeouts.json"), ServerConfigurator.serverYaml());
|
||||
assertThrows(IOException.class, () -> timeouts.raise(file, SPIGOT_KEY, 300L));
|
||||
assertEquals(60L, load(file).getLong(SPIGOT_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failedServerWriteKeepsTheOriginalRecorded() throws Exception {
|
||||
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||
try (MockedStatic<ServerConfigurator> writes = mockStatic(ServerConfigurator.class, CALLS_REAL_METHODS)) {
|
||||
writes.when(() -> ServerConfigurator.writeStringAtomic(eq(file.toRealPath()), anyString()))
|
||||
.thenThrow(new IOException("blocked server write"));
|
||||
assertThrows(IOException.class, () -> timeouts().raise(file, SPIGOT_KEY, 300L));
|
||||
}
|
||||
assertEquals(60L, load(file).getLong(SPIGOT_KEY));
|
||||
assertTrue(Files.readString(recoveryFile()).contains("\"original\": 60"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void interruptedRestoreRetriesWithoutOverwritingLaterOperatorEdits() throws Exception {
|
||||
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||
try (MockedStatic<ServerConfigurator> writes = mockStatic(ServerConfigurator.class, CALLS_REAL_METHODS)) {
|
||||
writes.when(() -> ServerConfigurator.writeStringAtomic(eq(recoveryFile()), anyString()))
|
||||
.thenThrow(new IOException("interrupted recovery write"));
|
||||
assertThrows(IOException.class, () -> timeouts().restore(SPIGOT_KEY));
|
||||
}
|
||||
assertTrue(Files.readString(recoveryFile()).contains("spigot.yml"));
|
||||
Files.writeString(file, "settings:\n timeout-time: 120\n");
|
||||
timeouts().restore(SPIGOT_KEY);
|
||||
assertEquals(120L, load(file).getLong(SPIGOT_KEY));
|
||||
assertFalse(Files.readString(recoveryFile()).contains("spigot.yml"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recordForADeletedFileIsDropped() throws Exception {
|
||||
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
|
||||
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
|
||||
Files.delete(file);
|
||||
timeouts().restore(SPIGOT_KEY);
|
||||
assertFalse(Files.readString(recoveryFile()).contains("spigot.yml"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void malformedRecoveryDataPreventsNewConfigurationWrites() throws Exception {
|
||||
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
|
||||
Files.writeString(recoveryFile(), "{invalid");
|
||||
assertThrows(IOException.class, () -> timeouts().raise(file, SPIGOT_KEY, 300L));
|
||||
assertEquals(60L, load(file).getLong(SPIGOT_KEY));
|
||||
assertEquals("{invalid", Files.readString(recoveryFile()));
|
||||
}
|
||||
|
||||
private Path configuration(String name, String content) throws IOException {
|
||||
Path file = temporaryFolder.newFile(name).toPath();
|
||||
Files.writeString(file, content);
|
||||
return file;
|
||||
}
|
||||
|
||||
private ServerTimeoutConfiguration timeouts() {
|
||||
return new ServerTimeoutConfiguration(recoveryFile(), ServerConfigurator.serverYaml());
|
||||
}
|
||||
|
||||
private Path recoveryFile() {
|
||||
return temporaryFolder.getRoot().toPath().resolve("server-timeouts.json");
|
||||
}
|
||||
|
||||
private static YamlConfiguration load(Path file) throws Exception {
|
||||
YamlConfiguration configuration = new YamlConfiguration();
|
||||
configuration.load(file.toFile());
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user