From 2a3b0de95353a5b92ec0d5859ffe115467c083c7 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Wed, 20 Apr 2022 22:58:06 +0200 Subject: [PATCH] more tests for grouping map chunks --- .../processor/topology/GroupMapChunksTest.kt | 226 ++++++++++++------ 1 file changed, 153 insertions(+), 73 deletions(-) diff --git a/modules/events-server/src/test/kotlin/dev/adamko/kafkatorio/processor/topology/GroupMapChunksTest.kt b/modules/events-server/src/test/kotlin/dev/adamko/kafkatorio/processor/topology/GroupMapChunksTest.kt index 5c8fffe..7cf0bca 100644 --- a/modules/events-server/src/test/kotlin/dev/adamko/kafkatorio/processor/topology/GroupMapChunksTest.kt +++ b/modules/events-server/src/test/kotlin/dev/adamko/kafkatorio/processor/topology/GroupMapChunksTest.kt @@ -12,11 +12,11 @@ import dev.adamko.kafkatorio.schema.common.MapTilePosition import dev.adamko.kafkatorio.schema.common.PrototypeName import dev.adamko.kafkatorio.schema.common.SurfaceIndex import dev.adamko.kafkatorio.schema.common.Tick -import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype import dev.adamko.kafkatorio.schema.packets.KafkatorioPacket import dev.adamko.kafkatorio.schema.packets.MapChunkUpdate import dev.adamko.kafkatorio.schema.packets.MapChunkUpdateKey import dev.adamko.kafkatorio.schema.packets.PrototypesUpdate +import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype import dev.adamko.kotka.kxs.serde import io.kotest.assertions.withClue import io.kotest.core.spec.style.FunSpec @@ -32,81 +32,17 @@ import org.apache.kafka.streams.TopologyTestDriver class GroupMapChunksTest : FunSpec({ - test("basic test") { - val streamsBuilder = StreamsBuilder() + test("proto, then map chunk") { - val prototypesTopicName = "kafkatorio.packet.prototypes" - val mapChunkUpdatesTopicName = "kafkatorio.packet.map-chunk" + val scenario = Scenario() - val topology = groupMapChunks(streamsBuilder) - - val testDriver = TopologyTestDriver(topology) - - val prototypesTestInputTopic: TestInputTopic = - testDriver.createInputTopic( - prototypesTopicName, - jsonMapper.serde().serializer(), - jsonMapper.serde().serializer(), - ) - - val serverId = FactorioServerId("test-server-id") - - prototypesTestInputTopic.pipeInput( - serverId, - KafkatorioPacket( - modVersion = "1.2.3", - tick = Tick(22u), - PrototypesUpdate( - listOf( - FactorioPrototype.MapTile( - PrototypeName("fake-proto"), - layer = 1u, - mapColour = Colour(0.3f, 0.2f, 0.1f, 1.0f), - collisionMasks = listOf("fake-mask"), - order = "fake-order", - canBeMined = false, - ) - ) - ) - ) + scenario.pipePrototypePacket( + PrototypesUpdate(listOf(testMockTile)) ) - val mapChunkUpdatesInputTopic: TestInputTopic = - testDriver.createInputTopic( - mapChunkUpdatesTopicName, - jsonMapper.serde().serializer(), - jsonMapper.serde().serializer(), - ) + scenario.pipeMapChunkPacket(testMapChunkUpdate) - mapChunkUpdatesInputTopic.pipeInput( - serverId, - KafkatorioPacket( - modVersion = "1.2.3", - tick = Tick(44u), - MapChunkUpdate( - key = MapChunkUpdateKey( - MapChunkPosition(11, 22), - SurfaceIndex(1u), - ), - tileDictionary = MapTileDictionary( - tilesXY = mapOf( - "1" to mapOf("2" to PrototypeKey(33)) - ), - protos = mapOf(PrototypeName("fake-proto") to PrototypeKey(33)) - ) - ) - ) - ) - - val outputTopic: TestOutputTopic> = - testDriver - .createOutputTopic( - TOPIC_GROUPED_MAP_CHUNKS_STATE, - kxsBinary.serde().deserializer(), - kxsBinary.serde>().deserializer(), - ) - - val records = outputTopic.readRecordsToList() + val records = scenario.mapTilesOutputTopic.readRecordsToList() withClue(records) { records shouldHaveSize 1 @@ -114,7 +50,7 @@ class GroupMapChunksTest : FunSpec({ records.map { it.key to it.value }.first() should { (key, value) -> key.shouldNotBeNull() key shouldBeEqualToComparingFields ServerMapChunkId( - serverId = serverId, + serverId = FactorioServerId("test-server-id"), chunkPosition = MapChunkPosition(0, 0), surfaceIndex = SurfaceIndex(1u), chunkSize = ChunkSize.CHUNK_512, @@ -129,4 +65,148 @@ class GroupMapChunksTest : FunSpec({ } } } -}) + + test("map chunk, then proto") { + + val scenario = Scenario() + + scenario.pipePrototypePacket( + PrototypesUpdate(listOf(testMockTile.copy(mapColour = COLOUR_ONE))) + ) + + scenario.pipeMapChunkPacket(testMapChunkUpdate) + + var records = scenario.mapTilesOutputTopic.readRecordsToList() + + withClue(records) { + records shouldHaveSize 1 + + records.map { it.key to it.value }.first() should { (key, value) -> + key.shouldNotBeNull() + key shouldBeEqualToComparingFields ServerMapChunkId( + serverId = FactorioServerId("test-server-id"), + chunkPosition = MapChunkPosition(0, 0), + surfaceIndex = SurfaceIndex(1u), + chunkSize = ChunkSize.CHUNK_512, + ) + value.shouldNotBeNull() + value shouldBeEqualToComparingFields ServerMapChunkTiles( + chunkId = key, + map = mapOf( + MapTilePosition(1, 2) to ColourHex(25u, 25u, 25u, 25u) + ) + ) + } + } + + scenario.pipePrototypePacket( + PrototypesUpdate(listOf(testMockTile.copy(mapColour = COLOUR_TWO))) + ) + + records = scenario.mapTilesOutputTopic.readRecordsToList() + + withClue(records) { + records shouldHaveSize 1 + + records.map { it.key to it.value }.first() should { (key, value) -> + key.shouldNotBeNull() + key shouldBeEqualToComparingFields ServerMapChunkId( + serverId = FactorioServerId("test-server-id"), + chunkPosition = MapChunkPosition(0, 0), + surfaceIndex = SurfaceIndex(1u), + chunkSize = ChunkSize.CHUNK_512, + ) + value.shouldNotBeNull() + value shouldBeEqualToComparingFields ServerMapChunkTiles( + chunkId = key, + map = mapOf( + MapTilePosition(1, 2) to ColourHex(51u, 51u, 51u, 51u) + ) + ) + } + } + } +}) { + + class Scenario( + streamsBuilder: StreamsBuilder = StreamsBuilder(), + + prototypesTopicName: String = "kafkatorio.packet.prototypes", + mapChunkUpdatesTopicName: String = "kafkatorio.packet.map-chunk", + + private val serverId: FactorioServerId = FactorioServerId("test-server-id"), + ) { + private val topology = groupMapChunks(streamsBuilder) + private val testDriver: TopologyTestDriver = TopologyTestDriver(topology) + + private val prototypesTestInputTopic: TestInputTopic = + testDriver.createInputTopic( + prototypesTopicName, + jsonMapper.serde().serializer(), + jsonMapper.serde().serializer(), + ) + + private val mapChunkUpdatesInputTopic: TestInputTopic = + testDriver.createInputTopic( + mapChunkUpdatesTopicName, + jsonMapper.serde().serializer(), + jsonMapper.serde().serializer(), + ) + + val mapTilesOutputTopic: TestOutputTopic> = + testDriver.createOutputTopic( + TOPIC_GROUPED_MAP_CHUNKS_STATE, + kxsBinary.serde().deserializer(), + kxsBinary.serde>().deserializer(), + ) + + fun pipePrototypePacket(prototypeUpdate: PrototypesUpdate) { + prototypesTestInputTopic.pipeInput( + serverId, + KafkatorioPacket( + modVersion = "1.2.3", + tick = Tick(22u), + data = prototypeUpdate, + ) + ) + } + + fun pipeMapChunkPacket(mapChunkUpdate: MapChunkUpdate) { + mapChunkUpdatesInputTopic.pipeInput( + serverId, + KafkatorioPacket( + modVersion = "1.2.3", + tick = Tick(44u), + data = mapChunkUpdate + ) + ) + } + } + + companion object { + val COLOUR_ONE = Colour(0.1f, 0.1f, 0.1f, 0.1f) + val COLOUR_TWO = Colour(0.2f, 0.2f, 0.2f, 0.2f) + + val testMockTile = FactorioPrototype.MapTile( + PrototypeName("fake-proto"), + layer = 1u, + mapColour = Colour(0.3f, 0.2f, 0.1f, 1.0f), + collisionMasks = listOf("fake-mask"), + order = "fake-order", + canBeMined = false, + ) + + val testMapChunkUpdate = MapChunkUpdate( + key = MapChunkUpdateKey( + MapChunkPosition(11, 22), + SurfaceIndex(1u), + ), + tileDictionary = MapTileDictionary( + tilesXY = mapOf( + "1" to mapOf("2" to PrototypeKey(33)) + ), + protos = mapOf(PrototypeName("fake-proto") to PrototypeKey(33)) + ) + ) + } +}