-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix TagsCache concurrency bug (#189)
Fixes #185 Use flyweight and cache only when running on standard context in event loop. Otherwise, the cache might return wrong tags (e.g. when invoked inside executeBlocking on standard context). Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
- Loading branch information
1 parent
2b839de
commit b64df78
Showing
2 changed files
with
89 additions
and
16 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
78 changes: 78 additions & 0 deletions
78
src/test/java/io/vertx/micrometer/impl/meters/TagsCacheTest.java
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,78 @@ | ||
package io.vertx.micrometer.impl.meters; | ||
|
||
import io.micrometer.core.instrument.Tags; | ||
import io.vertx.core.AbstractVerticle; | ||
import io.vertx.core.Promise; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.ext.unit.Async; | ||
import io.vertx.ext.unit.TestContext; | ||
import io.vertx.ext.unit.junit.VertxUnitRunner; | ||
import io.vertx.micrometer.Label; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
@RunWith(VertxUnitRunner.class) | ||
public class TagsCacheTest { | ||
|
||
private Vertx vertx = Vertx.vertx(); | ||
|
||
@After | ||
public void tearDown(TestContext tc) throws Exception { | ||
vertx.close().onComplete(tc.asyncAssertSuccess()); | ||
} | ||
|
||
@Test | ||
public void testCallsFromELThreadOnELContext(TestContext tc) { | ||
Promise<Void> promise = Promise.promise(); | ||
Label[] labels = Label.values(); | ||
vertx.runOnContext(v -> { | ||
Label label = labels[ThreadLocalRandom.current().nextInt(0, labels.length)]; | ||
String value = String.valueOf(label.ordinal()); | ||
Tags expectedTags = TagsCache.createTags(null, new Label[]{label}, new String[]{value}); | ||
Tags first = TagsCache.getOrCreate(null, new Label[]{label}, new String[]{value}); | ||
tc.assertEquals(expectedTags, first); | ||
Tags second = TagsCache.getOrCreate(null, new Label[]{label}, new String[]{value}); | ||
tc.assertTrue(first == second); | ||
promise.complete(); | ||
}); | ||
promise.future().onComplete(tc.asyncAssertSuccess()); | ||
} | ||
|
||
@Test | ||
public void testConcurrentCallsFromWorkerThreadsOnELContext(TestContext tc) { | ||
Async async = tc.async(10000); | ||
vertx.deployVerticle(new MyAbstractVerticle(tc, async)).onComplete(tc.asyncAssertSuccess()); | ||
async.awaitSuccess(); | ||
} | ||
|
||
private static class MyAbstractVerticle extends AbstractVerticle { | ||
|
||
final TestContext tc; | ||
final Async async; | ||
final int count; | ||
|
||
MyAbstractVerticle(TestContext tc, Async async) { | ||
this.tc = tc; | ||
this.async = async; | ||
count = async.count(); | ||
} | ||
|
||
@Override | ||
public void start() throws Exception { | ||
Label[] labels = Label.values(); | ||
for (int i = 0; i < count; i++) { | ||
vertx.executeBlocking(() -> { | ||
Label label = labels[ThreadLocalRandom.current().nextInt(0, labels.length)]; | ||
String value = String.valueOf(label.ordinal()); | ||
Tags expectedTags = TagsCache.createTags(null, new Label[]{label}, new String[]{value}); | ||
Tags actual = TagsCache.getOrCreate(null, new Label[]{label}, new String[]{value}); | ||
tc.assertEquals(expectedTags, actual); | ||
return null; | ||
}, false).onSuccess(v -> async.countDown()); | ||
} | ||
} | ||
} | ||
} |