Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition in InstrumentsConcurrencyTests #100518

Merged
merged 2 commits into from
Oct 9, 2023
Merged
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 @@ -28,12 +28,13 @@
import static org.hamcrest.Matchers.sameInstance;

public class InstrumentsConcurrencyTests extends ESTestCase {
String name = "name";
String description = "desc";
String unit = "kg";
Meter noopMeter = OpenTelemetry.noop().getMeter("noop");
CountDownLatch registerLatch = new CountDownLatch(1);
Meter lockingMeter = new Meter() {
private final String name = "name";
private final String description = "desc";
private final String unit = "kg";
private final Meter noopMeter = OpenTelemetry.noop().getMeter("noop");
private final CountDownLatch buildLatch = new CountDownLatch(1);
private final CountDownLatch registerLatch = new CountDownLatch(1);
private final Meter lockingMeter = new Meter() {
@Override
public LongCounterBuilder counterBuilder(String name) {
return new LockingLongCounterBuilder();
Expand Down Expand Up @@ -75,6 +76,7 @@ public DoubleCounterBuilder ofDoubles() {
@Override
public LongCounter build() {
try {
buildLatch.countDown();
registerLatch.await();
} catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -94,6 +96,8 @@ public void testLockingWhenRegistering() throws Exception {
var registerThread = new Thread(() -> instruments.registerLongCounter(name, description, unit));
// registerThread has a countDown latch that is simulating a long-running registration
registerThread.start();
buildLatch.await(); // wait for registerThread to hold the lock

var setProviderThread = new Thread(() -> instruments.setProvider(noopMeter));
// a setProviderThread will attempt to override a meter, but will wait to acquireLock
setProviderThread.start();
Expand Down