Skip to content

Commit

Permalink
feat: mdc missing map get function
Browse files Browse the repository at this point in the history
  • Loading branch information
programadorthi committed Aug 19, 2024
1 parent e915c87 commit 3d65d82
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dev.programadorthi.routing.callloging
public expect object MDC {
public fun clear()

public fun get(key: String): String?

public fun getCopyOfContextMap(): Map<String, String>?

public fun remove(key: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ public actual object MDC {
current.clear()
}

public actual fun get(key: String): String? {
return current[key]
}

public actual fun getCopyOfContextMap(): Map<String, String>? {
return current.toMap()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ public actual object MDC {
JvmMDC.clear()
}

public actual fun get(key: String): String? {
return JvmMDC.get(key)
}

public actual fun getCopyOfContextMap(): Map<String, String>? {
return JvmMDC.getCopyOfContextMap()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package dev.programadorthi.routing.callloging

import io.ktor.util.collections.ConcurrentMap

public actual object MDC {
// TODO: Well, how to implement MDC in native?
// TODO: Well, how to implement MDC Thread Local map in native?
private val current = ConcurrentMap<String, String>()

public actual fun clear() {
// no-op for now
current.clear()
}

public actual fun get(key: String): String? {
return current[key]
}

public actual fun getCopyOfContextMap(): Map<String, String>? {
return null
return current.toMap()
}

public actual fun remove(key: String) {
// no-op for now
current.remove(key)
}

public actual fun setContextMap(contextMap: Map<String, String>?) {
// no-op for now
val other = contextMap ?: return
current.clear()
current.putAll(other)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import dev.programadorthi.routing.core.application.call
import dev.programadorthi.routing.core.call
import dev.programadorthi.routing.core.install
import dev.programadorthi.routing.core.routing
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.test.advanceTimeBy
Expand All @@ -19,12 +25,6 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(RobolectricTestRunner::class)
Expand All @@ -43,7 +43,9 @@ internal class AndroidRoutingTest {
manager = fakeActivityManager
}

activity<FakeActivityA>(path = "/fakeA")
activity(path = "/fakeA") {
Intent(call.currentActivity, FakeActivityA::class.java)
}
}

// WHEN
Expand Down Expand Up @@ -97,8 +99,13 @@ internal class AndroidRoutingTest {
manager = fakeActivityManager
}

activity<FakeActivityB>(path = "/fakeB")
activity<FakeActivityC>(path = "/fakeC")
activity(path = "/fakeB") {
Intent(call.currentActivity, FakeActivityB::class.java)
}

activity(path = "/fakeC") {
Intent(call.currentActivity, FakeActivityC::class.java)
}
}

// WHEN
Expand Down Expand Up @@ -179,7 +186,9 @@ internal class AndroidRoutingTest {
manager = fakeActivityManager
}

activity<FakeActivityB>(path = "/fakeB")
activity(path = "/fakeB") {
Intent(call.currentActivity, FakeActivityB::class.java)
}
}

// WHEN
Expand Down Expand Up @@ -223,7 +232,9 @@ internal class AndroidRoutingTest {
manager = fakeActivityManager
}

activity<FakeActivityB>(path = "/fakeB")
activity(path = "/fakeB") {
Intent(call.currentActivity, FakeActivityB::class.java)
}
}

// WHEN
Expand Down Expand Up @@ -259,7 +270,9 @@ internal class AndroidRoutingTest {
manager = fakeActivityManager
}

activity<FakeActivityB>(path = "/fakeB")
activity(path = "/fakeB") {
Intent(call.currentActivity, FakeActivityB::class.java)
}
}

// WHEN
Expand Down

0 comments on commit 3d65d82

Please sign in to comment.