From 7fa29bb22e6dea35efd139617ba7d63cab163f9f Mon Sep 17 00:00:00 2001 From: Nikolai Sviridov Date: Mon, 22 Jan 2024 20:57:06 +0100 Subject: [PATCH] feat: add request, buffer tests --- core/src/test/kotlin/TestJsonObjectBuffer.kt | 87 ++++++++++++++++++++ core/src/test/kotlin/TestRequest.kt | 54 ++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 core/src/test/kotlin/TestJsonObjectBuffer.kt create mode 100644 core/src/test/kotlin/TestRequest.kt diff --git a/core/src/test/kotlin/TestJsonObjectBuffer.kt b/core/src/test/kotlin/TestJsonObjectBuffer.kt new file mode 100644 index 0000000..7bc4930 --- /dev/null +++ b/core/src/test/kotlin/TestJsonObjectBuffer.kt @@ -0,0 +1,87 @@ +import entity.rest.gerrit.ChangeGerrit +import entity.rest.gerrit.Reviewers +import entity.rest.gerrit.UserAccountGerrit +import extractor.ExtractorUtil +import extractor.gerrit.JsonObjectsMapBuffer +import kotlinx.coroutines.* +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json +import loader.gerrit.iterators.ChangeFilesValueIterator +import java.io.File +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicInteger +import kotlin.random.Random +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class TestJsonObjectBuffer { + + private fun randomField(base: String) = "${base}_${Random.nextInt(0, 5)}" + private val tmpFolder = File("src/test/tmp/") + + private fun generateChange(number: Int) = + ChangeGerrit( + randomField("id"), + randomField("project"), + randomField("branch"), + randomField("status"), + randomField("created"), + randomField("updated"), + Random.nextInt(0, 10), + number, + UserAccountGerrit(Random.nextInt(0, 10)), + Reviewers((0..Random.nextInt(0, 3)).map { UserAccountGerrit(it) }), + mapOf(), + randomField("subject"), + null + ) + + private suspend fun massiveRun(numberOfCoroutines: Int, numberOfActions: Int, action: suspend () -> Unit) { + coroutineScope { + repeat(numberOfCoroutines) { + launch { + repeat(numberOfActions) { action() } + } + } + } + } + + private fun cleanup() = tmpFolder.deleteRecursively() + + @Test + fun testJsonObjectBuffer() { + val counter = AtomicInteger() + val jsonObjectBuffer = JsonObjectsMapBuffer(tmpFolder) + tmpFolder.mkdirs() + val map = ConcurrentHashMap() + val numberOfCoroutines = 100 + val numberOfActions = 50 + + runBlocking { + withContext(Dispatchers.Default) { + massiveRun(numberOfCoroutines, numberOfActions) { + val number = counter.incrementAndGet() + val generatedChange = generateChange(number) + val rawJson = Json.encodeToString(generatedChange) + jsonObjectBuffer.addEntry(rawJson, number) + map[number] = generatedChange + } + } + } + jsonObjectBuffer.close() + + assertEquals(numberOfActions * numberOfCoroutines, map.size) + + val files = ExtractorUtil.getFilesIgnoreHidden(tmpFolder) + val iterator = ChangeFilesValueIterator(files) + while (iterator.hasNext()) { + val savedChange = iterator.next() + val generatedChange = map.remove(savedChange.number) + assertEquals(generatedChange, savedChange) + } + assertTrue(map.isEmpty()) + cleanup() + } + +} \ No newline at end of file diff --git a/core/src/test/kotlin/TestRequest.kt b/core/src/test/kotlin/TestRequest.kt new file mode 100644 index 0000000..a052ee4 --- /dev/null +++ b/core/src/test/kotlin/TestRequest.kt @@ -0,0 +1,54 @@ +import client.ClientGerritREST +import entity.rest.gerrit.ChangeGerrit +import entity.rest.gerrit.ChangeMetaData +import entity.rest.gerrit.CommentsREST +import kotlinx.coroutines.runBlocking +import kotlinx.serialization.json.Json +import kotlin.test.Test + +class TestRequest { + + private val json = Json { ignoreUnknownKeys = true } + private val baseUrl = "http://review.openstack.org" + + + private fun findChangeWithComments(): ChangeMetaData { + var offset = 0 + val client = ClientGerritREST() + var change: ChangeMetaData? = null + + while (change == null) { + val rawBatch = runBlocking { + client.getChangesRawLightNew( + baseUrl, + offset + ) + } + val batch = json.decodeFromString>(rawBatch) + change = batch.find { it.totalCommentCount > 0 } + offset += batch.size + } + return change + + } + + @Test + fun testServerJsonFormat() { + val client = ClientGerritREST() + val changeWithCommentsMetaData = findChangeWithComments() + + val rawChangeJson = runBlocking { + client.getChangeRaw(baseUrl, changeWithCommentsMetaData.number) + } + json.decodeFromString(rawChangeJson) + + val rawCommentsJson = runBlocking { + client.getCommentsRaw( + baseUrl, + changeId = changeWithCommentsMetaData.number + ) + } + json.decodeFromString(rawCommentsJson) + } + +} \ No newline at end of file