Fix MinestomThread local cache

This commit is contained in:
themode 2021-12-25 05:51:59 +01:00 committed by TheMode
parent a97fda7ef0
commit 660994443b
2 changed files with 44 additions and 1 deletions

View File

@ -33,7 +33,7 @@ public class MinestomThread extends Thread {
final int requiredLength = index + 1;
if (array.length < requiredLength) {
Object[] temp = new Object[requiredLength];
System.arraycopy(temp, 0, temp, 0, array.length);
System.arraycopy(array, 0, temp, 0, index);
array = temp;
this.locals = array;
}

View File

@ -0,0 +1,43 @@
package thread;
import net.minestom.server.thread.MinestomThread;
import org.junit.jupiter.api.Test;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class LocalThreadCache {
@Test
public void testLocalThreadCache() throws InterruptedException {
AtomicBoolean result = new AtomicBoolean(false);
var thread = new MinestomThread("name") {
@Override
public void run() {
final int dummy = -1;
var value = localCache(0, () -> 5);
assertEquals(5, value);
value = localCache(0, () -> dummy);
assertEquals(5, value);
value = localCache(1, () -> 7);
assertEquals(7, value);
assertEquals(5, localCache(0, () -> dummy));
value = localCache(2, () -> 5);
assertEquals(5, value);
assertEquals(7, localCache(1, () -> dummy));
assertEquals(5, localCache(0, () -> dummy));
result.set(true);
}
};
thread.start();
thread.join(1500);
assertTrue(result.get(), "Thread didn't start");
}
}