|
12 | 12 | import java.nio.file.attribute.PosixFileAttributeView; |
13 | 13 | import java.nio.file.attribute.PosixFilePermission; |
14 | 14 | import java.util.Set; |
| 15 | +import java.util.concurrent.CyclicBarrier; |
| 16 | +import java.util.concurrent.Executors; |
| 17 | +import java.util.concurrent.TimeUnit; |
15 | 18 |
|
16 | 19 | import org.junit.jupiter.api.Test; |
17 | 20 | import org.junit.jupiter.api.io.TempDir; |
@@ -140,6 +143,78 @@ class YamlConfigurationTest { |
140 | 143 | assertEquals(8, YamlConfiguration.load(path()).getInt("Points")); |
141 | 144 | } |
142 | 145 |
|
| 146 | + @Test void simultaneousFirstLoadsCannotBothPublishDifferentDefaults() throws Exception { |
| 147 | + var workers = Executors.newFixedThreadPool(2); |
| 148 | + try { |
| 149 | + for (int trial = 0; trial < 50; trial++) { |
| 150 | + Path path = directory.resolve("initialize-" + trial + ".yml"); |
| 151 | + var gate = new CyclicBarrier(2); |
| 152 | + var first = workers.submit(() -> initialize(path, "Points: 1\n", gate)); |
| 153 | + var second = workers.submit(() -> initialize(path, "Points: 2\n", gate)); |
| 154 | + Integer one = first.get(10, TimeUnit.SECONDS); |
| 155 | + Integer two = second.get(10, TimeUnit.SECONDS); |
| 156 | + assertNotEquals(one == null, two == null, "Exactly one missing-file initializer must succeed"); |
| 157 | + assertEquals(one == null ? two.intValue() : one.intValue(), YamlConfiguration.load(path).getInt("Points")); |
| 158 | + } |
| 159 | + } finally { |
| 160 | + workers.shutdownNow(); |
| 161 | + assertTrue(workers.awaitTermination(5, TimeUnit.SECONDS)); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @Test void simultaneousSameRevisionEditsCannotBothSucceedAndLoseOneChange() throws Exception { |
| 166 | + var workers = Executors.newFixedThreadPool(2); |
| 167 | + try { |
| 168 | + for (int trial = 0; trial < 50; trial++) { |
| 169 | + Path path = directory.resolve("update-" + trial + ".yml"); |
| 170 | + Files.writeString(path, "First: 0\nSecond: 0\n"); |
| 171 | + var first = YamlConfiguration.load(path); |
| 172 | + var second = YamlConfiguration.load(path); |
| 173 | + var gate = new CyclicBarrier(2); |
| 174 | + var one = workers.submit(() -> editAtBarrier(first, "First", gate)); |
| 175 | + var two = workers.submit(() -> editAtBarrier(second, "Second", gate)); |
| 176 | + boolean firstSucceeded = one.get(10, TimeUnit.SECONDS); |
| 177 | + boolean secondSucceeded = two.get(10, TimeUnit.SECONDS); |
| 178 | + assertNotEquals(firstSucceeded, secondSucceeded, "One stale writer must be rejected"); |
| 179 | + var disk = YamlConfiguration.load(path); |
| 180 | + assertEquals(firstSucceeded ? 1 : 0, disk.getInt("First")); |
| 181 | + assertEquals(secondSucceeded ? 1 : 0, disk.getInt("Second")); |
| 182 | + } |
| 183 | + } finally { |
| 184 | + workers.shutdownNow(); |
| 185 | + assertTrue(workers.awaitTermination(5, TimeUnit.SECONDS)); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private static Integer initialize(Path path, String text, CyclicBarrier gate) throws Exception { |
| 190 | + InputStream stream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)) { |
| 191 | + @Override public byte[] readNBytes(int limit) throws IOException { |
| 192 | + await(gate); |
| 193 | + return super.readNBytes(limit); |
| 194 | + } |
| 195 | + }; |
| 196 | + try { return YamlConfiguration.load(path, stream).getInt("Points"); } |
| 197 | + catch (IOException stale) { return null; } |
| 198 | + } |
| 199 | + |
| 200 | + private static boolean editAtBarrier(YamlConfiguration config, String key, CyclicBarrier gate) throws Exception { |
| 201 | + try { |
| 202 | + config.update(edit -> { |
| 203 | + try { await(gate); } catch (IOException failure) { throw new AssertionError(failure); } |
| 204 | + edit.set(key, 1); |
| 205 | + }); |
| 206 | + return true; |
| 207 | + } catch (IOException stale) { return false; } |
| 208 | + } |
| 209 | + |
| 210 | + private static void await(CyclicBarrier gate) throws IOException { |
| 211 | + try { gate.await(5, TimeUnit.SECONDS); } |
| 212 | + catch (InterruptedException interrupted) { Thread.currentThread().interrupt(); throw new IOException(interrupted); } |
| 213 | + catch (java.util.concurrent.BrokenBarrierException | java.util.concurrent.TimeoutException failure) { |
| 214 | + throw new IOException(failure); |
| 215 | + } |
| 216 | + } |
| 217 | + |
143 | 218 | private static final class TrackedStream extends ByteArrayInputStream { |
144 | 219 | private final int initialSize; |
145 | 220 | private boolean closed; |
|
0 commit comments