Skip to content

Commit

Permalink
Apply Spotless for Kotlin (#3272)
Browse files Browse the repository at this point in the history
This commit also applies its formatting.
  • Loading branch information
izeye authored Jul 7, 2022
1 parent 13f388d commit e83d8c7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 38 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ buildscript {
classpath 'me.champeau.gradle:japicmp-gradle-plugin:0.4.0'
classpath 'de.undercouch:gradle-download-task:5.0.2'
classpath 'io.spring.javaformat:spring-javaformat-gradle-plugin:0.0.34'
classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.8.0'

constraints {
classpath('org.ow2.asm:asm:7.3.1') {
Expand Down Expand Up @@ -48,6 +49,7 @@ allprojects {
subprojects {
apply plugin: 'signing'
apply plugin: 'io.spring.javaformat'
apply plugin: 'com.diffplug.spotless'

if (project.name != 'micrometer-bom') {
if (project.name.contains('samples') || project.name.contains('benchmarks')) {
Expand Down Expand Up @@ -184,6 +186,12 @@ subprojects {
exclude '**/*.json' // comments not supported
}

spotless {
kotlin {
ktlint().editorConfigOverride([disabled_rules: "no-wildcard-imports"])
}
}

// Publish resolved versions.
plugins.withId('maven-publish') {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down
9 changes: 9 additions & 0 deletions buildscript-gradle.lockfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import kotlin.coroutines.CoroutineContext
* @since 1.10.0
*/
fun ObservationRegistry.asContextElement(): CoroutineContext {
return KotlinObservationContextElement(this, ContextRegistry.getInstance())
return KotlinObservationContextElement(this, ContextRegistry.getInstance())
}

/**
Expand All @@ -38,9 +38,9 @@ fun ObservationRegistry.asContextElement(): CoroutineContext {
* @since 1.10.0
*/
fun CoroutineContext.currentObservation(): Observation? {
val element = get(KotlinObservationContextElement.KEY)
if (element is KotlinObservationContextElement) {
return element.currentObservation
}
return null
val element = get(KotlinObservationContextElement.KEY)
if (element is KotlinObservationContextElement) {
return element.currentObservation
}
return null
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,40 @@ import org.junit.jupiter.api.Test

internal class AsContextElementKtTests {

@Test
fun `should return current observation from context`(): Unit = runBlocking {
val observationRegistry = ObservationRegistry.create()
observationRegistry.observationConfig().observationHandler { true }
val nextObservation = Observation.start("name", observationRegistry)
val inScope = nextObservation.openScope()
var observationInGlobalScopeLaunch: Observation? = null
var observationInGlobalScopeAsync: Observation? = null
val asContextElement = observationRegistry.asContextElement()
@Test
fun `should return current observation from context`(): Unit = runBlocking {
val observationRegistry = ObservationRegistry.create()
observationRegistry.observationConfig().observationHandler { true }
val nextObservation = Observation.start("name", observationRegistry)
val inScope = nextObservation.openScope()
var observationInGlobalScopeLaunch: Observation? = null
var observationInGlobalScopeAsync: Observation? = null
val asContextElement = observationRegistry.asContextElement()

@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch(asContextElement) {
observationInGlobalScopeLaunch = coroutineContext.currentObservation()
}
@OptIn(DelicateCoroutinesApi::class)
GlobalScope.async(asContextElement) {
observationInGlobalScopeAsync = coroutineContext.currentObservation()
}.await()
@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch(asContextElement) {
observationInGlobalScopeLaunch = coroutineContext.currentObservation()
}
@OptIn(DelicateCoroutinesApi::class)
GlobalScope.async(asContextElement) {
observationInGlobalScopeAsync = coroutineContext.currentObservation()
}.await()

inScope.close();
inScope.close()

then(observationInGlobalScopeLaunch).isSameAs(nextObservation)
then(observationInGlobalScopeAsync).isSameAs(nextObservation)
}
then(observationInGlobalScopeLaunch).isSameAs(nextObservation)
then(observationInGlobalScopeAsync).isSameAs(nextObservation)
}

@Test
fun `should return observation from coroutine context when KotlinContextElement present`(): Unit = runBlocking {
val observationRegistry = ObservationRegistry.create()
observationRegistry.observationConfig().observationHandler { true }
val nextObservation = Observation.start("name", observationRegistry)
val inScope = nextObservation.openScope()
val element = KotlinObservationContextElement(observationRegistry, ContextRegistry.getInstance())

then(element.currentObservation()).isSameAs(nextObservation)
inScope.close()
}
@Test
fun `should return observation from coroutine context when KotlinContextElement present`(): Unit = runBlocking {
val observationRegistry = ObservationRegistry.create()
observationRegistry.observationConfig().observationHandler { true }
val nextObservation = Observation.start("name", observationRegistry)
val inScope = nextObservation.openScope()
val element = KotlinObservationContextElement(observationRegistry, ContextRegistry.getInstance())

then(element.currentObservation()).isSameAs(nextObservation)
inScope.close()
}
}

0 comments on commit e83d8c7

Please sign in to comment.