Skip to content

Fix for multithreaded execution #336

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

Merged
merged 2 commits into from
Feb 9, 2024
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 @@ -35,7 +35,7 @@ private TypeSet() {}
* Build a type with the provided name. If the provided name is null or
* the empty string, the empty type will be returned.
*/
public static Type type(String value) {
public static synchronized Type type(String value) {
return implementation.makeOrGetType(value);
}

Expand Down
24 changes: 24 additions & 0 deletions core/src/test/java/com/github/gumtreediff/test/TestTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.github.gumtreediff.tree.*;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.junit.jupiter.api.Assertions.*;

public class TestTree {
Expand Down Expand Up @@ -285,4 +288,25 @@ public void testToString() {
t3.setLength(2);
assertEquals("foo: hello [1,3]", t3.toString());
}

@Test
public void testTypeThreading() throws InterruptedException {
int n = 20;
ExecutorService exec = Executors.newFixedThreadPool(n);
List<Type> types = new ArrayList<>();

for (int i = 0; i < n; i++) {
exec.submit(() -> {
types.add(TypeSet.type("foo"));
});
}

exec.awaitTermination(1, java.util.concurrent.TimeUnit.SECONDS);

for (Type t1 : types) {
for (Type t2: types) {
assertSame(t1, t2);
}
}
}
}