-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate the traceId in the DdTracer using the new strategy in the Co…
…reTracer
- Loading branch information
Showing
9 changed files
with
218 additions
and
39 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
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
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
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
81 changes: 81 additions & 0 deletions
81
...es/dd-sdk-android-trace/src/test/kotlin/com/datadog/trace/api/IdGenerationStrategyTest.kt
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,81 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.trace.api | ||
|
||
import com.datadog.android.utils.forge.Configurator | ||
import fr.xgouchet.elmyr.junit5.ForgeConfiguration | ||
import fr.xgouchet.elmyr.junit5.ForgeExtension | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.junit.jupiter.api.extension.Extensions | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.ValueSource | ||
import org.mockito.junit.jupiter.MockitoExtension | ||
import org.mockito.junit.jupiter.MockitoSettings | ||
import org.mockito.kotlin.mock | ||
import org.mockito.quality.Strictness | ||
import java.security.SecureRandom | ||
|
||
@Extensions( | ||
ExtendWith(MockitoExtension::class), | ||
ExtendWith(ForgeExtension::class) | ||
) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
@ForgeConfiguration(Configurator::class) | ||
internal class IdGenerationStrategyTest { | ||
|
||
// Please note that these tests were just ported from the Groovy CoreTracer tests in APM java code | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = ["RANDOM", "SEQUENTIAL", "SECURE_RANDOM"]) | ||
fun `M generate id with strategyName and tIdSize bits`(strategyName: String) { | ||
val isTid128 = listOf(false, true) | ||
isTid128.forEach { tId128b -> | ||
val strategy = IdGenerationStrategy.fromName(strategyName, tId128b) | ||
val traceIds = (0..32768).map { strategy.generateTraceId() } | ||
val checked = HashSet<DDTraceId>() | ||
|
||
traceIds.forEach { traceId -> | ||
assertThat(traceId).isNotNull | ||
assertThat(traceId.equals("foo")).isFalse() | ||
assertThat(traceId.equals(DDTraceId.ZERO)).isFalse() | ||
assertThat(traceId.equals(traceId)).isTrue() | ||
val hashCode = | ||
( | ||
traceId.toHighOrderLong() xor (traceId.toHighOrderLong() ushr 32) | ||
xor traceId.toLong() xor (traceId.toLong() ushr 32) | ||
).toInt() | ||
assertThat(traceId.hashCode()).isEqualTo(hashCode) | ||
assertThat(checked).doesNotContain(traceId) | ||
checked.add(traceId) | ||
} | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = ["SOME", "UNKNOWN", "STRATEGIES"]) | ||
fun `M return null for non existing strategy strategyName`(strategyName: String) { | ||
val strategy = IdGenerationStrategy.fromName(strategyName) | ||
assertThat(strategy).isNull() | ||
} | ||
|
||
@Test | ||
fun `exception created on SecureRandom strategy`() { | ||
// Given | ||
val illegalArgumentException = IllegalArgumentException("SecureRandom init exception") | ||
val provider: IdGenerationStrategy.ThrowingSupplier<SecureRandom> = mock { | ||
on { get() }.thenThrow(illegalArgumentException) | ||
} | ||
|
||
// Then | ||
assertThatThrownBy { IdGenerationStrategy.SRandom(true, provider) } | ||
.isInstanceOf(ExceptionInInitializerError::class.java) | ||
.hasCause(illegalArgumentException) | ||
} | ||
} |
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
Oops, something went wrong.