|
9 | 9 | import java.util.HashMap; |
10 | 10 | import java.util.List; |
11 | 11 | import java.util.Map; |
| 12 | +import java.util.UUID; |
12 | 13 | import org.junit.jupiter.api.Test; |
| 14 | +import top.ellan.mahjong.application.InteractionHandle; |
13 | 15 | import top.ellan.mahjong.application.InteractionRouter; |
14 | 16 | import top.ellan.mahjong.application.TableActorRegistry; |
15 | 17 | import top.ellan.mahjong.domain.TableId; |
| 18 | +import top.ellan.mahjong.presentation.SceneInteractionBinding; |
16 | 19 | import top.ellan.mahjong.presentation.FurnitureNode; |
17 | 20 | import top.ellan.mahjong.presentation.SceneDiff; |
18 | 21 | import top.ellan.mahjong.presentation.SceneNode; |
19 | 22 | import top.ellan.mahjong.presentation.SceneNodeId; |
20 | 23 | import top.ellan.mahjong.presentation.SceneTransform; |
21 | 24 | import top.ellan.mahjong.presentation.SceneVisibility; |
| 25 | +import top.ellan.mahjong.spi.ActionToken; |
| 26 | +import top.ellan.mahjong.spi.PlayerId; |
22 | 27 |
|
23 | 28 | class CraftEngineSceneBackendTest { |
24 | 29 | private static final RegionKey REGION = new RegionKey("world", 0, 0); |
@@ -123,19 +128,147 @@ void removalDuringReloadStillDeletesThePreviouslyAppliedFurniture() { |
123 | 128 | assertFalse(gateway.live.getOrDefault(table, Map.of()).containsKey(node)); |
124 | 129 | } |
125 | 130 |
|
| 131 | + @Test |
| 132 | + void interactionTokensOpenOnlyAfterTheirSceneRevisionIsApplied() { |
| 133 | + ManualRegionScheduler scheduler = new ManualRegionScheduler(); |
| 134 | + RecordingGateway gateway = new RecordingGateway(); |
| 135 | + InteractionRouter interactions = new InteractionRouter(new TableActorRegistry()); |
| 136 | + CraftEngineSceneBackend backend = |
| 137 | + backend(gateway, scheduler, interactions, ignored -> {}); |
| 138 | + TableId table = TableId.random(); |
| 139 | + PlayerId player = new PlayerId(UUID.randomUUID()); |
| 140 | + InteractionHandle handle = new InteractionHandle(UUID.randomUUID()); |
| 141 | + SceneDiff initial = withBinding(diff(table, 1), handle, player, 1); |
| 142 | + |
| 143 | + backend.submit(initial); |
| 144 | + assertEquals(0, interactions.routeCount()); |
| 145 | + backend.onCraftEngineReloaded(); |
| 146 | + assertEquals(0, interactions.routeCount()); |
| 147 | + |
| 148 | + scheduler.runUntilIdle(REGION, 4); |
| 149 | + assertEquals(1, interactions.routeCount()); |
| 150 | + |
| 151 | + SceneNode changed = |
| 152 | + new FurnitureNode( |
| 153 | + new SceneNodeId("tile/0"), |
| 154 | + SceneVisibility.publicToAll(), |
| 155 | + "mahjong:tile/back", |
| 156 | + new SceneTransform(2, 0, 0, 0, 0, 0, 1)); |
| 157 | + SceneDiff next = |
| 158 | + new SceneDiff( |
| 159 | + table, |
| 160 | + 1, |
| 161 | + 2, |
| 162 | + List.of(), |
| 163 | + List.of(changed), |
| 164 | + List.of( |
| 165 | + new SceneInteractionBinding( |
| 166 | + handle, |
| 167 | + player, |
| 168 | + new ActionToken(UUID.randomUUID(), player, 2)))); |
| 169 | + |
| 170 | + backend.submit(next); |
| 171 | + assertEquals(0, interactions.routeCount()); |
| 172 | + |
| 173 | + scheduler.runUntilIdle(REGION, 4); |
| 174 | + assertEquals(1, interactions.routeCount()); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void newerSceneSubmittedDuringMutationCannotBeClearedByTheOlderResult() { |
| 179 | + ManualRegionScheduler scheduler = new ManualRegionScheduler(); |
| 180 | + RecordingGateway gateway = new RecordingGateway(); |
| 181 | + CraftEngineSceneBackend backend = backend(gateway, scheduler, ignored -> {}); |
| 182 | + TableId table = TableId.random(); |
| 183 | + SceneNodeId nodeId = new SceneNodeId("tile/0"); |
| 184 | + SceneNode newest = |
| 185 | + new FurnitureNode( |
| 186 | + nodeId, |
| 187 | + SceneVisibility.publicToAll(), |
| 188 | + "mahjong:tile/back", |
| 189 | + new SceneTransform(3, 0, 0, 0, 0, 0, 1)); |
| 190 | + gateway.duringFirstUpsert = |
| 191 | + () -> |
| 192 | + backend.submit( |
| 193 | + new SceneDiff( |
| 194 | + table, |
| 195 | + 1, |
| 196 | + 2, |
| 197 | + List.of(), |
| 198 | + List.of(newest), |
| 199 | + List.of())); |
| 200 | + |
| 201 | + backend.submit(diff(table, 1)); |
| 202 | + backend.onCraftEngineReloaded(); |
| 203 | + scheduler.runUntilIdle(REGION, 4); |
| 204 | + |
| 205 | + assertEquals(2, gateway.upserted.getOrDefault(table, 0)); |
| 206 | + assertEquals(newest, gateway.live.getOrDefault(table, Map.of()).get(nodeId)); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + void failedCraftEngineRevisionNeverOpensItsInteractionTokens() { |
| 211 | + ManualRegionScheduler scheduler = new ManualRegionScheduler(); |
| 212 | + RecordingGateway gateway = new RecordingGateway(); |
| 213 | + InteractionRouter interactions = new InteractionRouter(new TableActorRegistry()); |
| 214 | + CraftEngineSceneBackend backend = |
| 215 | + backend(gateway, scheduler, interactions, ignored -> {}); |
| 216 | + TableId table = TableId.random(); |
| 217 | + PlayerId player = new PlayerId(UUID.randomUUID()); |
| 218 | + gateway.broken = table; |
| 219 | + |
| 220 | + backend.submit( |
| 221 | + withBinding( |
| 222 | + diff(table, 1), |
| 223 | + new InteractionHandle(UUID.randomUUID()), |
| 224 | + player, |
| 225 | + 1)); |
| 226 | + backend.onCraftEngineReloaded(); |
| 227 | + scheduler.runUntilIdle(REGION, 4); |
| 228 | + |
| 229 | + assertEquals(0, interactions.routeCount()); |
| 230 | + } |
| 231 | + |
126 | 232 | private static CraftEngineSceneBackend backend( |
127 | 233 | RecordingGateway gateway, |
128 | 234 | ManualRegionScheduler scheduler, |
129 | 235 | java.util.function.Consumer<CraftEngineTableFailure> failures) { |
| 236 | + return backend( |
| 237 | + gateway, |
| 238 | + scheduler, |
| 239 | + new InteractionRouter(new TableActorRegistry()), |
| 240 | + failures); |
| 241 | + } |
| 242 | + |
| 243 | + private static CraftEngineSceneBackend backend( |
| 244 | + RecordingGateway gateway, |
| 245 | + ManualRegionScheduler scheduler, |
| 246 | + InteractionRouter interactions, |
| 247 | + java.util.function.Consumer<CraftEngineTableFailure> failures) { |
130 | 248 | return new CraftEngineSceneBackend( |
131 | 249 | gateway, |
132 | 250 | scheduler, |
133 | 251 | ignored -> REGION, |
134 | | - new InteractionRouter(new TableActorRegistry()), |
| 252 | + interactions, |
135 | 253 | new CraftEngineBackendConfig(16, 1_000_000_000L, 1_024, 256), |
136 | 254 | failures); |
137 | 255 | } |
138 | 256 |
|
| 257 | + private static SceneDiff withBinding( |
| 258 | + SceneDiff diff, InteractionHandle handle, PlayerId player, long revision) { |
| 259 | + return new SceneDiff( |
| 260 | + diff.tableId(), |
| 261 | + diff.fromRevision(), |
| 262 | + diff.toRevision(), |
| 263 | + diff.removals(), |
| 264 | + diff.upserts(), |
| 265 | + List.of( |
| 266 | + new SceneInteractionBinding( |
| 267 | + handle, |
| 268 | + player, |
| 269 | + new ActionToken(UUID.randomUUID(), player, revision)))); |
| 270 | + } |
| 271 | + |
139 | 272 | private static SceneDiff diff(TableId table, int nodes) { |
140 | 273 | List<SceneNode> upserts = new ArrayList<>(); |
141 | 274 | for (int index = 0; index < nodes; index++) { |
@@ -185,13 +318,19 @@ private static final class RecordingGateway implements CraftEngineMutationGatewa |
185 | 318 | private final Map<TableId, Integer> upserted = new HashMap<>(); |
186 | 319 | private final Map<TableId, Map<SceneNodeId, SceneNode>> live = new HashMap<>(); |
187 | 320 | private TableId broken; |
| 321 | + private Runnable duringFirstUpsert; |
188 | 322 |
|
189 | 323 | @Override |
190 | 324 | public void upsert(TableId tableId, SceneNode node) { |
191 | 325 | if (tableId.equals(broken)) { |
192 | 326 | throw new IllegalStateException("isolated CE failure"); |
193 | 327 | } |
194 | 328 | upserted.merge(tableId, 1, Integer::sum); |
| 329 | + Runnable callback = duringFirstUpsert; |
| 330 | + duringFirstUpsert = null; |
| 331 | + if (callback != null) { |
| 332 | + callback.run(); |
| 333 | + } |
195 | 334 | live.computeIfAbsent(tableId, ignored -> new HashMap<>()).put(node.id(), node); |
196 | 335 | } |
197 | 336 |
|
|
0 commit comments