add a couple of context tests around keys with colliding hashcodes (#2114)
* add a couple of context tests around keys with colliding hashcodes * add a test for an empty context
This commit is contained in:
parent
1828938263
commit
7f4396219e
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.context;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DefaultContextTest {
|
||||
|
||||
@Test
|
||||
void emptyContext() {
|
||||
assertThat(new DefaultContext().get(new HashCollidingKey())).isEqualTo(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hashcodeCollidingKeys() {
|
||||
DefaultContext context = new DefaultContext();
|
||||
HashCollidingKey cheese = new HashCollidingKey();
|
||||
HashCollidingKey wine = new HashCollidingKey();
|
||||
|
||||
Context twoKeys = context.with(cheese, "whiz").with(wine, "boone's farm");
|
||||
|
||||
assertThat(twoKeys.get(wine)).isEqualTo("boone's farm");
|
||||
assertThat(twoKeys.get(cheese)).isEqualTo("whiz");
|
||||
}
|
||||
|
||||
private static class HashCollidingKey implements ContextKey<String> {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.context;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PersistentHashArrayMappedTrieTest {
|
||||
|
||||
@Test
|
||||
void hashCollisions() {
|
||||
HashCollidingKey cheese = new HashCollidingKey();
|
||||
HashCollidingKey wine = new HashCollidingKey();
|
||||
PersistentHashArrayMappedTrie.Node<HashCollidingKey, String> root =
|
||||
PersistentHashArrayMappedTrie.put(null, cheese, "cheddar");
|
||||
|
||||
PersistentHashArrayMappedTrie.Node<HashCollidingKey, String> child =
|
||||
PersistentHashArrayMappedTrie.put(root, wine, "Pinot Noir");
|
||||
|
||||
assertThat(PersistentHashArrayMappedTrie.get(child, cheese)).isEqualTo("cheddar");
|
||||
assertThat(PersistentHashArrayMappedTrie.get(child, wine)).isEqualTo("Pinot Noir");
|
||||
}
|
||||
|
||||
private static class HashCollidingKey {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue