Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@SuppressFBWarnings("DM_GC")
@SuppressWarnings({"UnusedAssignment", "ConstantValue"})
Expand Down Expand Up @@ -158,6 +167,50 @@ void removeStaleEntries() {
visited.clear();
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
Comment thread
mcculls marked this conversation as resolved.
void competingComputationsReturnTheInstalledValue(boolean systemLoader) throws Exception {
ClassLoader loader = systemLoader ? ClassLoader.getSystemClassLoader() : null;
int threadCount = 8;
CountDownLatch computing = new CountDownLatch(threadCount);
AtomicInteger computeCalls = new AtomicInteger();
ClassLoaderValue<Object> values =
new ClassLoaderValue<Object>() {
@Override
protected Object computeValue(ClassLoader cl) {
assertThat(cl).isSameAs(loader);
computeCalls.incrementAndGet();
// Nobody can install a value until every contender is already computing one.
computing.countDown();
try {
assertThat(computing.await(10, TimeUnit.SECONDS)).isTrue();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new AssertionError(e);
}
return new Object();
}
};

ExecutorService executor = Executors.newFixedThreadPool(threadCount);
List<Future<Object>> results = new ArrayList<>();
try {
for (int i = 0; i < threadCount; i++) {
results.add(executor.submit(() -> values.get(loader)));
}
Object winner = results.get(0).get(10, TimeUnit.SECONDS);
assertThat(winner).isNotNull();
for (Future<Object> result : results) {
assertThat(result.get(10, TimeUnit.SECONDS)).isSameAs(winner);
}
assertThat(values.get(loader)).isSameAs(winner);
assertThat(computeCalls).hasValue(threadCount);
assertThat(values.size()).isEqualTo(1);
} finally {
executor.shutdownNow();
}
}

private static ClassLoader newCL(String name) {
return new ClassLoader() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datadog.instrument.utils;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -15,6 +16,8 @@
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class ClassNameFilterTest {

Expand All @@ -36,24 +39,37 @@ void basicOperation() {
assertFalse(filter.contains("example.test.NotMyClass"));
}

@Test
void uniqueClassCodes() {
@ParameterizedTest
@CsvSource({
"example.GAnxX, example.ZvaQyFGz", // different simple-name lengths
"example.test.FB, example.test.Ea", // different first characters
"example.AaAa, example.AaBB" // same first character, different last characters
})
Comment thread
mcculls marked this conversation as resolved.
void uniqueClassCodes(String stored, String collision) {
ClassNameFilter filter = new ClassNameFilter(8);

assertEquals("example.test.FB".hashCode(), "example.test.Ea".hashCode());
assertEquals(stored.hashCode(), collision.hashCode());
assertFalse(filter.contains(stored));
assertFalse(filter.contains(collision));

assertFalse(filter.contains("example.test.FB"));
assertFalse(filter.contains("example.test.Ea"));
filter.add(stored);

filter.add("example.test.FB");
assertTrue(filter.contains(stored));
assertFalse(filter.contains(collision));

assertTrue(filter.contains("example.test.FB"));
assertFalse(filter.contains("example.test.Ea"));
filter.add(collision);

filter.add("example.test.Ea");
assertTrue(filter.contains(stored));
assertFalse(filter.contains(collision));
}

assertTrue(filter.contains("example.test.FB"));
assertFalse(filter.contains("example.test.Ea"));
@Test
void rejectsInvalidFileMagic() throws IOException {
try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(new byte[4]))) {
assertThatThrownBy(() -> ClassNameFilter.readFrom(input))
.isInstanceOf(IOException.class)
.hasMessage("Unexpected file magic 0");
}
}

@Test
Expand Down
Loading