Skip to content

Commit

Permalink
fix threading not releasing main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Sep 10, 2022
1 parent adf1623 commit 840b7af
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
org.gradle.jvmargs=-Xmx3G

# Mod Properties
version = 1.1.7
version = 1.1.8
maven_group = xyz.wagyourtail
archives_base_name = jsmacros-jep

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ protected void execContext(JEPScriptContext ctx, Executor exec) throws Exception
ctx.setContext(interp);

for (Map.Entry<String, BaseLibrary> lib : retrieveLibs(ctx).entrySet()) interp.set(lib.getKey(), lib.getValue());

exec.accept(interp);
ctx.leave();

try {
exec.accept(interp);
} finally {
ctx.leave();
ctx.tasks.poll().release();
}
}

@Override
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/xyz/wagyourtail/jsmacros/core/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ def add(i):
add(0)
JavaWrapper.deferCurrentTask(-2)
""";


@Language("py")
private final String TEST_SCRIPT2 = """
i = 0
def tick(a, b):
global i;
i += 1
if i == 10:
JsMacros.off("tick", tickEv)
event.putInt("i", i)
tickEv = JsMacros.on("tick", JavaWrapper.methodToJava(tick))
tick(1, 2)
""";

@Test
public void test() throws InterruptedException {
Core<ProfileStub, EventRegistryStub> core = CoreInstanceCreator.createCore();
Expand All @@ -34,4 +47,13 @@ public void test() throws InterruptedException {
assertEquals("[0, 3, 1, 2]", event.getString("test"));
}

@Test
public void test2() throws InterruptedException {
Core<ProfileStub, EventRegistryStub> core = CoreInstanceCreator.createCore();
EventCustom event = new EventCustom("test");
EventContainer<?> ev = core.exec("py", TEST_SCRIPT2, null, event, null, null);
Thread.sleep(1000);
assertEquals(10, event.getInt("i"));
}

}
12 changes: 12 additions & 0 deletions src/test/java/xyz/wagyourtail/jsmacros/stubs/EventTick.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package xyz.wagyourtail.jsmacros.stubs;

import xyz.wagyourtail.jsmacros.core.event.BaseEvent;
import xyz.wagyourtail.jsmacros.core.event.Event;

@Event("tick")
public class EventTick implements BaseEvent {

EventTick() {
profile.triggerEvent(this);
}
}
33 changes: 27 additions & 6 deletions src/test/java/xyz/wagyourtail/jsmacros/stubs/ProfileStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import xyz.wagyourtail.jsmacros.core.event.BaseEvent;
import xyz.wagyourtail.jsmacros.core.event.IEventListener;
import xyz.wagyourtail.jsmacros.core.event.impl.EventCustom;
import xyz.wagyourtail.jsmacros.core.event.impl.EventProfileLoad;
import xyz.wagyourtail.jsmacros.core.language.EventContainer;
import xyz.wagyourtail.jsmacros.core.library.impl.FJsMacros;

import java.util.LinkedList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class ProfileStub extends BaseProfile {

Expand All @@ -21,14 +23,26 @@ public class ProfileStub extends BaseProfile {

static {
th = new Thread(() -> {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
while (true) {
try {
runnables.take().run();
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
new EventTick();
try {
while (true) {
Runnable r = runnables.poll(0, TimeUnit.SECONDS);
if (r != null) {
try {
r.run();
} catch (Exception e) {
e.printStackTrace();
}
} else {
break;
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -96,4 +110,11 @@ private void runJoinedEventListener(BaseEvent event, boolean joinedMain, IEventL
}
}

@Override
protected void initRegistries() {
super.initRegistries();

this.runner.eventRegistry.addEvent(EventTick.class);
}

}

0 comments on commit 840b7af

Please sign in to comment.