mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-27 03:27:56 +01:00
Fix MinestomThread local cache
This commit is contained in:
parent
a97fda7ef0
commit
660994443b
@ -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;
|
||||
}
|
||||
|
43
src/test/java/thread/LocalThreadCache.java
Normal file
43
src/test/java/thread/LocalThreadCache.java
Normal 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");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user