Skip to content

Instantly share code, notes, and snippets.

@Seggan
Created December 23, 2021 03:02
Show Gist options
  • Save Seggan/f7fb4690dfb799eb94ff1006bcf6ab86 to your computer and use it in GitHub Desktop.
Save Seggan/f7fb4690dfb799eb94ff1006bcf6ab86 to your computer and use it in GitHub Desktop.
Proving Thread-Safety of SimplexOctaveGenerator#generateNoise
import org.bukkit.util.noise.SimplexOctaveGenerator;
import org.junit.jupiter.api.Assertions;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
public class OctaveGeneratorThreadTest {
public static void main(String[] args) throws InterruptedException {
int threadCount = 500; // set this to the number of threads to use in testing
CountDownLatch start = new CountDownLatch(1);
CountDownLatch end = new CountDownLatch(threadCount);
SimplexOctaveGenerator generator = new SimplexOctaveGenerator(100, 8);
generator.setScale(0.05);
for (int h = 0; h < threadCount; h++) {
new Thread(() -> {
try {
int x = ThreadLocalRandom.current().nextInt(100_000);
int y = ThreadLocalRandom.current().nextInt(100_000);
int cmp = height(generator, x, y);
// tell the main thread setup is done
end.countDown();
// wait for the start signal
start.await();
// and we're off!
for (int i = 0; i < 1000; i++) {
Assertions.assertEquals(cmp, height(generator, x, y));
}
// tell the main thread we're done
end.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
// wait for everyone to finish setup
end.await();
// give the start signal
start.countDown();
// wait for everyone to finish
end.await();
}
// get the height of the terrain at the given coordinates
private static int height(SimplexOctaveGenerator generator, int x, int y) {
double d = generator.noise(x, y, 0.5, 0.5, true);
double temp = 50 + 20 * d;
return temp >= 0 ? (int) temp : (int) temp - 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment