|
| 1 | +package tools.jackson.databind.introspect; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Queue; |
| 6 | +import java.util.concurrent.*; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 11 | +import com.fasterxml.jackson.annotation.JsonKey; |
| 12 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 13 | +import com.fasterxml.jackson.annotation.JsonValue; |
| 14 | + |
| 15 | +import tools.jackson.databind.BeanDescription; |
| 16 | +import tools.jackson.databind.ObjectMapper; |
| 17 | +import tools.jackson.databind.ObjectMapperTestAccess; |
| 18 | +import tools.jackson.databind.testutil.DatabindTestUtil; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +// [databind#6227] |
| 23 | +public class BeanDescriptionConcurrent6227Test extends DatabindTestUtil |
| 24 | +{ |
| 25 | + static class Probe { |
| 26 | + public final String id; |
| 27 | + public final int count; |
| 28 | + |
| 29 | + @JsonCreator |
| 30 | + public Probe(@JsonProperty("id") String id, @JsonProperty("count") int count) { |
| 31 | + this.id = id; |
| 32 | + this.count = count; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Both field and getter annotated: resolution modifies accessor list |
| 37 | + static class ValueProbe { |
| 38 | + @JsonValue |
| 39 | + public String value = "x"; |
| 40 | + |
| 41 | + @JsonValue |
| 42 | + public String getValue() { return value; } |
| 43 | + } |
| 44 | + |
| 45 | + static class KeyProbe { |
| 46 | + @JsonKey |
| 47 | + public String key = "x"; |
| 48 | + |
| 49 | + @JsonKey |
| 50 | + public String getKey() { return key; } |
| 51 | + } |
| 52 | + |
| 53 | + @FunctionalInterface |
| 54 | + interface DescAction { |
| 55 | + void run(BeanDescription desc) throws Exception; |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testConcurrentFindProperties() throws Exception |
| 60 | + { |
| 61 | + ObjectMapper mapper = newJsonMapper(); |
| 62 | + |
| 63 | + // Repeat a number of times to increase odds of hitting race |
| 64 | + for (int round = 0; round < 50; ++round) { |
| 65 | + final BeanDescription shared = ObjectMapperTestAccess.beanDescriptionForSer(mapper, Probe.class); |
| 66 | + _runRound(shared, 16, BeanDescription::findProperties); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testConcurrentFindJsonValueAccessor() throws Exception |
| 72 | + { |
| 73 | + ObjectMapper mapper = newJsonMapper(); |
| 74 | + |
| 75 | + for (int round = 0; round < 50; ++round) { |
| 76 | + final BeanDescription shared = ObjectMapperTestAccess.beanDescriptionForSer(mapper, ValueProbe.class); |
| 77 | + // Getter has precedence over field |
| 78 | + _runRound(shared, 16, desc -> assertTrue( |
| 79 | + desc.findJsonValueAccessor() instanceof AnnotatedMethod)); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testConcurrentFindJsonKeyAccessor() throws Exception |
| 85 | + { |
| 86 | + ObjectMapper mapper = newJsonMapper(); |
| 87 | + |
| 88 | + for (int round = 0; round < 50; ++round) { |
| 89 | + final BeanDescription shared = ObjectMapperTestAccess.beanDescriptionForSer(mapper, KeyProbe.class); |
| 90 | + // Getter has precedence over field |
| 91 | + _runRound(shared, 16, desc -> assertTrue( |
| 92 | + desc.findJsonKeyAccessor() instanceof AnnotatedMethod)); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private void _runRound(final BeanDescription shared, int parallelism, |
| 97 | + final DescAction action) throws Exception |
| 98 | + { |
| 99 | + final CyclicBarrier barrier = new CyclicBarrier(parallelism); |
| 100 | + final Queue<Throwable> errors = new ConcurrentLinkedQueue<>(); |
| 101 | + ExecutorService pool = Executors.newFixedThreadPool(parallelism); |
| 102 | + List<Future<?>> futures = new ArrayList<>(); |
| 103 | + for (int i = 0; i < parallelism; i++) { |
| 104 | + futures.add(pool.submit(() -> { |
| 105 | + try { |
| 106 | + barrier.await(); |
| 107 | + action.run(shared); |
| 108 | + } catch (Throwable e) { |
| 109 | + errors.add(e); |
| 110 | + } |
| 111 | + })); |
| 112 | + } |
| 113 | + for (Future<?> f : futures) { |
| 114 | + f.get(30, TimeUnit.SECONDS); |
| 115 | + } |
| 116 | + pool.shutdown(); |
| 117 | + if (!errors.isEmpty()) { |
| 118 | + throw new AssertionError("Failed with "+errors.size()+" errors, first: "+errors.peek(), errors.peek()); |
| 119 | + } |
| 120 | + assertTrue(errors.isEmpty()); |
| 121 | + } |
| 122 | +} |
0 commit comments