-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2be5041
commit 7fa29bb
Showing
2 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |