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

Fixed concurrency issue in ObjectDeserializer #449

Merged
merged 5 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -78,8 +78,8 @@ public <T> DeserBean<T> getDeserializableBean(Argument<T> type, DecoderContext d
DeserBean<T> deserBeanSupplier = (DeserBean) deserBeanMap.get(key);
if (deserBeanSupplier == null) {
deserBeanSupplier = createDeserBean(type, decoderContext);
deserBeanMap.put(key, (DeserBean) deserBeanSupplier);
deserBeanSupplier.initialize(decoderContext);
deserBeanMap.put(key, (DeserBean) deserBeanSupplier);
}
return deserBeanSupplier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import jakarta.inject.Inject
import spock.lang.Specification

import java.nio.charset.StandardCharsets
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.stream.IntStream

@MicronautTest
class CoreTypeSerdeSpec extends Specification {
Expand Down Expand Up @@ -86,4 +91,28 @@ class CoreTypeSerdeSpec extends Specification {
then:
read == node
}

void "test read concurrency"() {
given:
def results = new ConcurrentHashMap<Integer, SimpleInfo>()

when:
ExecutorService threadPool = Executors.newFixedThreadPool(1000)
IntStream.range(0, 1000).forEach(i -> threadPool.execute(new Runnable() {
@Override
void run() {
results.put(i, jsonMapper.readValue('{"info":"test' + i + '"}', Argument.of(SimpleInfo)))
}
}));
threadPool.shutdown()
try {
threadPool.awaitTermination(1, TimeUnit.MINUTES)
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while waiting", e)
}

then:
results.size() == 1000
results.each { assert it.value.getInfo() == "test" + it.key }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.micronaut.serde.support;

import io.micronaut.serde.annotation.Serdeable;

@Serdeable
public class SimpleInfo {
private final String info;

public SimpleInfo(String info) {
this.info = info;
}

public String getInfo() {
return info;
}
}