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:
John Watson 2020-11-21 09:29:47 -08:00 committed by GitHub
parent 1828938263
commit 7f4396219e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 0 deletions

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}