Skip to content

Commit

Permalink
feat: add request, buffer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaiSviridov committed Jan 22, 2024
1 parent 2be5041 commit 7fa29bb
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
87 changes: 87 additions & 0 deletions core/src/test/kotlin/TestJsonObjectBuffer.kt
Original file line number Diff line number Diff line change
@@ -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<Int, ChangeGerrit>()
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()
}

}
54 changes: 54 additions & 0 deletions core/src/test/kotlin/TestRequest.kt
Original file line number Diff line number Diff line change
@@ -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<List<ChangeMetaData>>(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<ChangeGerrit>(rawChangeJson)

val rawCommentsJson = runBlocking {
client.getCommentsRaw(
baseUrl,
changeId = changeWithCommentsMetaData.number
)
}
json.decodeFromString<CommentsREST>(rawCommentsJson)
}

}

0 comments on commit 7fa29bb

Please sign in to comment.