Iris binds AtomicCache's failure handler to its logging and error reporting at startup

This commit is contained in:
piexel committed 2026-10-10 15:18:05 +02:00
1 parent 80d50b5cff
commit 9d886e3c69
11 files changed
+56 -944

No files matched your search

@@ -19,6 +19,7 @@
package art.arcane.iris;
import art.arcane.iris.engine.IrisEngineEffects;
import art.arcane.iris.engine.data.cache.AtomicCacheFailureReporting;
import art.arcane.iris.engine.IrisWorldManager;
import art.arcane.iris.engine.framework.EngineComponentCleanup;
@@ -804,6 +805,7 @@ public class Iris extends VolmitPlugin implements Listener, ReloadAware {
public void onEnable() {
IrisPlatforms.bind(new BukkitPlatform());
AtomicCacheFailureReporting.bind();
IrisStartupValidation.begin();
Bukkit.getPluginManager().registerEvents(new IrisStartupAdmissionListener(), this);
Bukkit.getPluginManager().registerEvents(pendingWorldReplacements, this);
@@ -20,6 +20,7 @@ package art.arcane.iris.modded;
import art.arcane.iris.core.gui.GuiHost;
import art.arcane.iris.core.localization.IrisLanguage;
import art.arcane.iris.engine.data.cache.AtomicCacheFailureReporting;
import art.arcane.iris.engine.decorator.DecoratorPlatformHooks;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.framework.EngineEffectsProvider;
@@ -348,6 +349,7 @@ public final class ModdedEngineBootstrap {
IrisPlatform previousPlatform = IrisPlatforms.isBound() ? IrisPlatforms.get() : null;
IrisPlatforms.bind(created);
AtomicCacheFailureReporting.bind();
rollback.add(() -> restorePlatform(previousPlatform));
ModdedServerAccess previousServerAccess = ModdedDimensionManager.bindAccess(
@@ -1,197 +0,0 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2022 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.engine.data.cache;
import art.arcane.iris.spi.IrisLogging;
import art.arcane.volmlib.util.function.NastySupplier;
import java.util.function.Supplier;
public class AtomicCache<T> {
private static final Object NULL_VALUE = new Object();
private transient final Object initLock = new Object();
private transient final boolean nullSupport;
private transient volatile Object value;
private transient volatile Throwable failure;
public AtomicCache() {
this(false);
}
public AtomicCache(boolean nullSupport) {
this.nullSupport = nullSupport;
}
public void reset() {
synchronized (initLock) {
value = null;
failure = null;
}
}
public T getIfPresent() {
return unwrap(value);
}
public T aquireNasty(NastySupplier<T> t) {
return aquire(() -> {
try {
return t.get();
} catch (Throwable e) {
return null;
}
});
}
public T aquireNastyPrint(NastySupplier<T> t) {
return aquire(() -> {
try {
return t.get();
} catch (Throwable e) {
IrisLogging.reportError("Atomic cache supplier failed.", e);
return null;
}
});
}
/**
* Like {@link #aquire(Supplier)} but propagates a supplier failure to the caller instead
* of swallowing it into a null return. For values that are mandatory: a caller of a
* "@NotNull" accessor should see the supplier's real exception, not a downstream NPE.
* <p>
* The supplier is retried on every call until it produces a value, which is what a caller that can
* repair the cause between attempts needs. Use {@link #aquireOnceOrThrow(Supplier)} when re-running a
* failed supplier is itself the problem.
*/
public T aquireOrThrow(Supplier<T> t) {
Object v = value;
if (v != null) {
return unwrap(v);
}
synchronized (initLock) {
v = value;
if (v != null) {
return unwrap(v);
}
T computed = t.get();
if (computed == null) {
throw new IllegalStateException("Atomic cache supplier produced null");
}
value = computed;
return computed;
}
}
/**
* Like {@link #aquireOrThrow(Supplier)} but memoizes the failure as well as the value, so a supplier
* that cannot produce one is run exactly once and every later caller is told the same reason. For
* values whose supplier rebuilds shared state, where re-running it per call repeats that work and
* hides the original cause behind whatever the retry happens to fail on. {@link #reset()} clears it.
*/
public T aquireOnceOrThrow(Supplier<T> t) {
Object v = value;
if (v != null) {
return unwrap(v);
}
rethrowFailure(failure);
synchronized (initLock) {
v = value;
if (v != null) {
return unwrap(v);
}
rethrowFailure(failure);
T computed;
try {
computed = t.get();
if (computed == null) {
throw new IllegalStateException("Atomic cache supplier produced null");
}
} catch (Throwable e) {
failure = e;
throw e;
}
value = computed;
return computed;
}
}
private static void rethrowFailure(Throwable memoized) {
if (memoized == null) {
return;
}
if (memoized instanceof RuntimeException runtimeFailure) {
throw runtimeFailure;
}
if (memoized instanceof Error error) {
throw error;
}
throw new IllegalStateException("Atomic cache supplier failed", memoized);
}
public T aquire(Supplier<T> t) {
Object v = value;
if (v != null) {
return unwrap(v);
}
synchronized (initLock) {
v = value;
if (v != null) {
return unwrap(v);
}
try {
T computed = t.get();
if (computed != null) {
value = computed;
return computed;
}
if (nullSupport) {
value = NULL_VALUE;
}
} catch (Throwable e) {
// aquire retries on every call, so a supplier that stays broken is reached per sample.
// The first statement of it carries the stack; the rest are traces of the same cause.
if (IrisLogging.warnOnce("atomic-cache:" + e.getClass().getName() + ":" + e.getMessage(),
"Atomic cache supplier failed: %s: %s", e.getClass().getSimpleName(), e.getMessage())) {
IrisLogging.reportError(e);
}
}
return null;
}
}
@SuppressWarnings("unchecked")
private T unwrap(Object v) {
return v == null || v == NULL_VALUE ? null : (T) v;
}
}
@@ -0,0 +1,51 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2022 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.engine.data.cache;
import art.arcane.iris.spi.IrisLogging;
import art.arcane.volmlib.util.cache.AtomicCache;
import art.arcane.volmlib.util.cache.AtomicCacheFailureHandler;
/**
* Sends the failures of {@link AtomicCache} suppliers to Iris's logging and error reporting. Bound once at startup;
* it needs no platform of its own because {@link IrisLogging} falls back to the process streams when none is bound.
*/
public final class AtomicCacheFailureReporting implements AtomicCacheFailureHandler {
private AtomicCacheFailureReporting() {
}
public static void bind() {
AtomicCache.setFailureHandler(new AtomicCacheFailureReporting());
}
@Override
public void supplierFailed(Throwable failure) {
IrisLogging.reportError("Atomic cache supplier failed.", failure);
}
@Override
public void optionalSupplierFailed(Throwable failure) {
// aquire retries on every call, so a supplier that stays broken is reached per sample.
// The first statement of it carries the stack; the rest are traces of the same cause.
if (IrisLogging.warnOnce("atomic-cache:" + failure.getClass().getName() + ":" + failure.getMessage(),
"Atomic cache supplier failed: %s: %s", failure.getClass().getSimpleName(), failure.getMessage())) {
IrisLogging.reportError(failure);
}
}
}
@@ -1,47 +0,0 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2022 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.engine.data.cache;
import art.arcane.volmlib.util.cache.CacheKey;
public interface Cache<V> {
static long key(int x, int z) {
return CacheKey.key(x, z);
}
static int keyX(long key) {
return CacheKey.keyX(key);
}
static int keyZ(long key) {
return CacheKey.keyZ(key);
}
static int to1D(int x, int y, int z, int w, int h) {
return CacheKey.to1D(x, y, z, w, h);
}
static int[] to3D(int idx, int w, int h) {
return CacheKey.to3D(idx, w, h);
}
int getId();
V get(int x, int z);
}
@@ -1,331 +0,0 @@
package art.arcane.iris.util.common.data;
import art.arcane.volmlib.util.collection.KMap;
import lombok.NonNull;
import art.arcane.iris.util.common.math.IrisBlockVector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.BiConsumer;
import java.util.function.Function;
public class VectorMap<T> implements Iterable<Map.Entry<IrisBlockVector, T>> {
private final Map<Key, Map<Key, T>> map = new KMap<>();
private transient volatile long modificationRevision;
public long modificationRevision() {
return modificationRevision;
}
public int size() {
return map.values().stream().mapToInt(Map::size).sum();
}
public boolean isEmpty() {
return map.values().stream().allMatch(Map::isEmpty);
}
public boolean containsKey(@NonNull IrisBlockVector vector) {
if (map.isEmpty()) return false;
var chunk = map.get(chunk(vector));
return chunk != null && chunk.containsKey(relative(vector));
}
public boolean containsValue(@NonNull T value) {
return map.values().stream().anyMatch(m -> m.containsValue(value));
}
public @Nullable T get(@NonNull IrisBlockVector vector) {
if (map.isEmpty()) return null;
var chunk = map.get(chunk(vector));
return chunk == null ? null : chunk.get(relative(vector));
}
public @Nullable T put(@NonNull IrisBlockVector vector, @NonNull T value) {
T previous = map.computeIfAbsent(chunk(vector), k -> new KMap<>())
.put(relative(vector), value);
modificationRevision++;
return previous;
}
public @Nullable T computeIfAbsent(@NonNull IrisBlockVector vector, @NonNull Function<@NonNull IrisBlockVector, @NonNull T> mappingFunction) {
boolean[] inserted = new boolean[1];
T value = map.computeIfAbsent(chunk(vector), key -> new KMap<>())
.computeIfAbsent(relative(vector), key -> {
inserted[0] = true;
return mappingFunction.apply(vector);
});
if (inserted[0]) {
modificationRevision++;
}
return value;
}
@SuppressWarnings("unchecked")
public @Nullable T remove(@NonNull IrisBlockVector vector) {
if (map.isEmpty()) return null;
Key relative = relative(vector);
Object[] removed = new Object[1];
// computeIfPresent so the emptied bucket is pruned atomically against a concurrent put.
map.computeIfPresent(chunk(vector), (key, chunk) -> {
removed[0] = chunk.remove(relative);
return chunk.isEmpty() ? null : chunk;
});
if (removed[0] != null) {
modificationRevision++;
}
return (T) removed[0];
}
public void putAll(@NonNull VectorMap<T> map) {
map.forEach(this::put);
}
public void clear() {
if (!map.isEmpty()) {
modificationRevision++;
}
map.clear();
}
public void forEach(@NonNull BiConsumer<@NonNull IrisBlockVector, @NonNull T> consumer) {
map.forEach((chunk, values) -> {
int rX = chunk.x << 10;
int rY = chunk.y << 10;
int rZ = chunk.z << 10;
values.forEach((relative, value) -> consumer.accept(
relative.resolve(rX, rY, rZ),
value
));
});
}
private static Key chunk(IrisBlockVector vector) {
return new Key(vector.getBlockX() >> 10, vector.getBlockY() >> 10, vector.getBlockZ() >> 10);
}
private static Key relative(IrisBlockVector vector) {
return new Key(vector.getBlockX() & 0x3FF, vector.getBlockY() & 0x3FF, vector.getBlockZ() & 0x3FF);
}
@Override
public @NotNull EntryIterator iterator() {
return new EntryIterator();
}
/**
* Allocation-free entry walk. {@link EntryIterator} allocates a resolved vector plus a Map.Entry per element;
* the cursor resolves into one reused vector instead. Only valid for callers that do not retain the vector
* returned by {@link Cursor#key()} beyond the current step - clone it if it must outlive the next
* {@link Cursor#next()}.
*/
public @NotNull Cursor cursor() {
return new Cursor();
}
public @NotNull KeyIterator keys() {
return new KeyIterator();
}
public @NotNull ValueIterator values() {
return new ValueIterator();
}
public final class Cursor {
private final Iterator<Map.Entry<Key, Map<Key, T>>> chunkIterator = map.entrySet().iterator();
private final IrisBlockVector position = new IrisBlockVector(0, 0, 0);
private Iterator<Map.Entry<Key, T>> relativeIterator;
private int rX, rY, rZ;
private T value;
public boolean next() {
while (relativeIterator == null || !relativeIterator.hasNext()) {
if (!chunkIterator.hasNext()) {
value = null;
return false;
}
Map.Entry<Key, Map<Key, T>> chunk = chunkIterator.next();
rX = chunk.getKey().x << 10;
rY = chunk.getKey().y << 10;
rZ = chunk.getKey().z << 10;
relativeIterator = chunk.getValue().entrySet().iterator();
}
Map.Entry<Key, T> entry = relativeIterator.next();
Key relative = entry.getKey();
position.setX(rX + relative.x);
position.setY(rY + relative.y);
position.setZ(rZ + relative.z);
value = entry.getValue();
return true;
}
/**
* The position of the current element. The same instance is returned every step.
*/
public @NotNull IrisBlockVector key() {
return position;
}
public T value() {
return value;
}
}
public class EntryIterator implements Iterator<Map.Entry<IrisBlockVector, T>> {
private final Iterator<Map.Entry<Key, Map<Key, T>>> chunkIterator = map.entrySet().iterator();
private Iterator<Map.Entry<Key, T>> relativeIterator;
private int rX, rY, rZ;
@Override
public boolean hasNext() {
return advance();
}
@Override
public Map.Entry<IrisBlockVector, T> next() {
if (!advance()) throw new NoSuchElementException();
var entry = relativeIterator.next();
return Map.entry(entry.getKey().resolve(rX, rY, rZ), entry.getValue());
}
private boolean advance() {
while (relativeIterator == null || !relativeIterator.hasNext()) {
if (!chunkIterator.hasNext()) return false;
var chunk = chunkIterator.next();
rX = chunk.getKey().x << 10;
rY = chunk.getKey().y << 10;
rZ = chunk.getKey().z << 10;
relativeIterator = chunk.getValue().entrySet().iterator();
}
return true;
}
@Override
public void remove() {
if (relativeIterator == null) throw new IllegalStateException("No element to remove");
relativeIterator.remove();
modificationRevision++;
}
}
public class KeyIterator implements Iterator<IrisBlockVector>, Iterable<IrisBlockVector> {
private final Iterator<Map.Entry<Key, Map<Key, T>>> chunkIterator = map.entrySet().iterator();
private Iterator<Key> relativeIterator;
private int rX, rY, rZ;
@Override
public boolean hasNext() {
return advance();
}
@Override
public IrisBlockVector next() {
if (!advance()) throw new NoSuchElementException();
return relativeIterator.next().resolve(rX, rY, rZ);
}
private boolean advance() {
while (relativeIterator == null || !relativeIterator.hasNext()) {
if (!chunkIterator.hasNext()) return false;
var chunk = chunkIterator.next();
rX = chunk.getKey().x << 10;
rY = chunk.getKey().y << 10;
rZ = chunk.getKey().z << 10;
relativeIterator = chunk.getValue().keySet().iterator();
}
return true;
}
@Override
public void remove() {
if (relativeIterator == null) throw new IllegalStateException("No element to remove");
relativeIterator.remove();
modificationRevision++;
}
@Override
public @NotNull Iterator<IrisBlockVector> iterator() {
return this;
}
}
public class ValueIterator implements Iterator<T>, Iterable<T> {
private final Iterator<Map<Key, T>> chunkIterator = map.values().iterator();
private Iterator<T> relativeIterator;
@Override
public boolean hasNext() {
return advance();
}
@Override
public T next() {
if (!advance()) throw new NoSuchElementException();
return relativeIterator.next();
}
private boolean advance() {
while (relativeIterator == null || !relativeIterator.hasNext()) {
if (!chunkIterator.hasNext()) return false;
relativeIterator = chunkIterator.next().values().iterator();
}
return true;
}
@Override
public void remove() {
if (relativeIterator == null) throw new IllegalStateException("No element to remove");
relativeIterator.remove();
modificationRevision++;
}
@Override
public @NotNull Iterator<T> iterator() {
return this;
}
}
private static final class Key {
private final int x;
private final int y;
private final int z;
private final int hashCode;
private Key(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
this.hashCode = (x << 20) | (y << 10) | z;
}
private IrisBlockVector resolve(int rX, int rY, int rZ) {
return new IrisBlockVector(rX + x, rY + y, rZ + z);
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Key key)) return false;
return x == key.x && y == key.y && z == key.z;
}
}
}
@@ -1,34 +0,0 @@
package art.arcane.iris.util.common.math;
public class IrisBlockVector extends IrisVector {
public IrisBlockVector() {
super();
}
public IrisBlockVector(int x, int y, int z) {
super(x, y, z);
}
public IrisBlockVector(double x, double y, double z) {
super(x, y, z);
}
@Override
public IrisBlockVector clone() {
return (IrisBlockVector) super.clone();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof IrisBlockVector other)) {
return false;
}
return (int) other.getX() == (int) this.x && (int) other.getY() == (int) this.y && (int) other.getZ() == (int) this.z;
}
@Override
public int hashCode() {
return (Integer.hashCode((int) x) >> 13) ^ (Integer.hashCode((int) y) >> 7) ^ Integer.hashCode((int) z);
}
}
@@ -1,163 +0,0 @@
package art.arcane.iris.util.common.math;
public class IrisVector implements Cloneable {
private static final double EPSILON = 0.000001;
protected double x;
protected double y;
protected double z;
public IrisVector() {
this.x = 0;
this.y = 0;
this.z = 0;
}
public IrisVector(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public IrisVector(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public int getBlockX() {
return floor(x);
}
public int getBlockY() {
return floor(y);
}
public int getBlockZ() {
return floor(z);
}
public IrisVector setX(double x) {
this.x = x;
return this;
}
public IrisVector setY(double y) {
this.y = y;
return this;
}
public IrisVector setZ(double z) {
this.z = z;
return this;
}
public IrisVector add(IrisVector vec) {
x += vec.x;
y += vec.y;
z += vec.z;
return this;
}
public IrisVector subtract(IrisVector vec) {
x -= vec.x;
y -= vec.y;
z -= vec.z;
return this;
}
public IrisVector multiply(double m) {
x *= m;
y *= m;
z *= m;
return this;
}
public double distance(IrisVector o) {
return Math.sqrt(square(x - o.x) + square(y - o.y) + square(z - o.z));
}
public double distanceSquared(IrisVector o) {
return square(x - o.x) + square(y - o.y) + square(z - o.z);
}
public IrisVector rotateAroundX(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double y = angleCos * this.y - angleSin * this.z;
double z = angleSin * this.y + angleCos * this.z;
return setY(y).setZ(z);
}
public IrisVector rotateAroundY(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double x = angleCos * this.x + angleSin * this.z;
double z = -angleSin * this.x + angleCos * this.z;
return setX(x).setZ(z);
}
public IrisVector rotateAroundZ(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double x = angleCos * this.x - angleSin * this.y;
double y = angleSin * this.x + angleCos * this.y;
return setX(x).setY(y);
}
public IrisBlockVector toBlockVector() {
return new IrisBlockVector(x, y, z);
}
@Override
public IrisVector clone() {
try {
return (IrisVector) super.clone();
} catch (CloneNotSupportedException e) {
throw new Error(e);
}
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof IrisVector other)) {
return false;
}
return Math.abs(x - other.x) < EPSILON && Math.abs(y - other.y) < EPSILON && Math.abs(z - other.z) < EPSILON && this.getClass().equals(obj.getClass());
}
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
hash = 79 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
hash = 79 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
return hash;
}
@Override
public String toString() {
return x + "," + y + "," + z;
}
protected static int floor(double num) {
int floor = (int) num;
return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63);
}
private static double square(double num) {
return num * num;
}
}
@@ -1,65 +0,0 @@
package art.arcane.iris.util.common.math;
public class Vector3i implements Cloneable {
private final int x;
private final int y;
private final int z;
public Vector3i(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
public int getBlockX() {
return x;
}
public int getBlockY() {
return y;
}
public int getBlockZ() {
return z;
}
@Override
public Vector3i clone() {
try {
return (Vector3i) super.clone();
} catch (CloneNotSupportedException e) {
throw new Error(e);
}
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Vector3i other)) {
return false;
}
return x == other.x && y == other.y && z == other.z;
}
@Override
public int hashCode() {
return x ^ (z << 12) ^ (y << 24);
}
@Override
public String toString() {
return x + "," + y + "," + z;
}
}
@@ -33,7 +33,7 @@ public class DiagnosticSeverityPolicyTest {
assertLoggedWith("engine/mantle/MantleWriter.java", "Mantle Writer Accessed chunk out of bounds", "debug");
assertLoggedWith("engine/decorator/ResolvedDecorator.java", "Empty Block Data for ", "warnOnce");
assertLoggedWith("engine/object/compatibility/IrisCompat.java", "Can't find block data for ", "warnOnce");
assertLoggedWith("engine/data/cache/AtomicCache.java", "Atomic cache supplier failed: %s: %s", "warnOnce");
assertLoggedWith("engine/data/cache/AtomicCacheFailureReporting.java", "Atomic cache supplier failed: %s: %s", "warnOnce");
assertLoggedWith("engine/IrisComplex.java", "Failed to sample interpolated biome bounds", "warnOnce");
assertLoggedWith("engine/framework/placer/WorldObjectPlacer.java", "Tried to place custom block at", "warnOnce");
}
@@ -1,106 +0,0 @@
package art.arcane.iris.engine.data.cache;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
public class AtomicCacheTest {
@Test
public void aquireOnceOrThrowCachesTheComputedValue() {
AtomicCache<String> cache = new AtomicCache<>();
AtomicInteger calls = new AtomicInteger();
assertEquals("value", cache.aquireOnceOrThrow(() -> {
calls.incrementAndGet();
return "value";
}));
assertEquals("value", cache.aquireOnceOrThrow(() -> {
calls.incrementAndGet();
return "other";
}));
assertEquals(1, calls.get());
}
/**
* The retrying variant is what a caller that can repair the cause between attempts needs, and the
* world generator relies on it, so memoization must stay confined to aquireOnceOrThrow.
*/
@Test
public void aquireOrThrowStillRetriesAFailedSupplier() {
AtomicCache<String> cache = new AtomicCache<>();
AtomicInteger calls = new AtomicInteger();
assertThrows(IllegalStateException.class, () -> cache.aquireOrThrow(() -> {
calls.incrementAndGet();
throw new IllegalStateException("repairable");
}));
assertEquals("repaired", cache.aquireOrThrow(() -> {
calls.incrementAndGet();
return "repaired";
}));
assertEquals(2, calls.get());
}
@Test
public void aquireOnceOrThrowMemoizesTheSupplierFailureInsteadOfRerunningIt() {
AtomicCache<String> cache = new AtomicCache<>();
AtomicInteger calls = new AtomicInteger();
IllegalStateException cause = new IllegalStateException("Frozen Iris pack snapshot is missing");
IllegalStateException first = assertThrows(IllegalStateException.class, () -> cache.aquireOnceOrThrow(() -> {
calls.incrementAndGet();
throw cause;
}));
IllegalStateException second = assertThrows(IllegalStateException.class, () -> cache.aquireOnceOrThrow(() -> {
calls.incrementAndGet();
return "recovered";
}));
assertSame(cause, first);
assertSame(cause, second);
assertEquals("the failure is stated once and replayed, never recomputed", 1, calls.get());
}
@Test
public void aquireOnceOrThrowRejectsANullValueAndRemembersThat() {
AtomicCache<String> cache = new AtomicCache<>();
AtomicInteger calls = new AtomicInteger();
assertThrows(IllegalStateException.class, () -> cache.aquireOnceOrThrow(() -> {
calls.incrementAndGet();
return null;
}));
assertThrows(IllegalStateException.class, () -> cache.aquireOnceOrThrow(() -> {
calls.incrementAndGet();
return "recovered";
}));
assertEquals(1, calls.get());
}
@Test
public void resetClearsAMemoizedFailure() {
AtomicCache<String> cache = new AtomicCache<>();
assertThrows(IllegalStateException.class, () -> cache.aquireOnceOrThrow(() -> {
throw new IllegalStateException("transient");
}));
cache.reset();
assertEquals("recovered", cache.aquireOnceOrThrow(() -> "recovered"));
}
@Test
public void aquireStillSwallowsFailuresForOptionalValues() {
AtomicCache<String> cache = new AtomicCache<>();
assertNull(cache.aquire(() -> {
throw new IllegalStateException("optional value");
}));
assertEquals("later", cache.aquire(() -> "later"));
}
}