Skip to content

Commit df6f2f4

Browse files
committed
tests: Add utilities logging specific RestActions
1 parent 4fe4eab commit df6f2f4

File tree

1 file changed

+39
-0
lines changed
  • src/test/kotlin/io/github/freya022/botcommands/test/utils

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.github.freya022.botcommands.test.utils
2+
3+
import ch.qos.logback.classic.Level
4+
import ch.qos.logback.classic.Logger
5+
import dev.minn.jda.ktx.coroutines.await
6+
import io.github.freya022.botcommands.api.core.utils.awaitCatching
7+
import kotlinx.coroutines.sync.Mutex
8+
import kotlinx.coroutines.sync.withLock
9+
import net.dv8tion.jda.api.requests.RestAction
10+
import net.dv8tion.jda.internal.requests.Requester
11+
import org.slf4j.LoggerFactory
12+
13+
private val mutex = Mutex()
14+
15+
suspend fun <T> RestAction<T>.awaitAndLog(): T {
16+
val logger = LoggerFactory.getLogger(Requester::class.java.name) as Logger
17+
return mutex.withLock {
18+
val oldLevel = logger.level
19+
try {
20+
logger.level = Level.TRACE
21+
await()
22+
} finally {
23+
logger.level = oldLevel
24+
}
25+
}
26+
}
27+
28+
suspend fun RestAction<*>.awaitAndLogCatching() {
29+
val logger = LoggerFactory.getLogger(Requester::class.java.name) as Logger
30+
val oldLevel = logger.level
31+
mutex.withLock {
32+
try {
33+
logger.level = Level.TRACE
34+
awaitCatching().onFailure { it.printStackTrace() }
35+
} finally {
36+
logger.level = oldLevel
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)