|
18 | 18 |
|
19 | 19 | import com.caoccao.javet.BaseTestJavet;
|
20 | 20 | import com.caoccao.javet.enums.JSRuntimeType;
|
| 21 | +import com.caoccao.javet.enums.V8AwaitMode; |
21 | 22 | import com.caoccao.javet.exceptions.JavetError;
|
22 | 23 | import com.caoccao.javet.exceptions.JavetException;
|
| 24 | +import com.caoccao.javet.interop.converters.JavetProxyConverter; |
23 | 25 | import com.caoccao.javet.interop.options.NodeRuntimeOptions;
|
24 | 26 | import com.caoccao.javet.node.modules.NodeModuleAny;
|
25 | 27 | import com.caoccao.javet.node.modules.NodeModuleProcess;
|
|
35 | 37 | import java.nio.file.Path;
|
36 | 38 | import java.nio.file.StandardOpenOption;
|
37 | 39 | import java.text.MessageFormat;
|
| 40 | +import java.util.concurrent.atomic.AtomicInteger; |
38 | 41 |
|
39 | 42 | import static org.junit.jupiter.api.Assertions.*;
|
40 | 43 |
|
@@ -82,6 +85,81 @@ protected void internalTest(String fileName, String expectedJsonString) throws J
|
82 | 85 | }
|
83 | 86 | }
|
84 | 87 |
|
| 88 | + @Test |
| 89 | + public void testAwaitRunNoWait() { |
| 90 | + try { |
| 91 | + nodeRuntime.setConverter(new JavetProxyConverter()); |
| 92 | + AtomicInteger counter = new AtomicInteger(); |
| 93 | + nodeRuntime.getGlobalObject().set("counter", counter); |
| 94 | + nodeRuntime.getExecutor("let timeoutIDs = [];" + |
| 95 | + "function a() {\n" + |
| 96 | + " counter.incrementAndGet();\n" + |
| 97 | + " if (counter.get() < 10) {\n" + |
| 98 | + " timeoutIDs.push(setTimeout(a, 1000));\n" + |
| 99 | + " }\n" + |
| 100 | + "};\n" + |
| 101 | + "a();").executeVoid(); |
| 102 | + assertTrue(nodeRuntime.await(V8AwaitMode.RunNoWait)); |
| 103 | + nodeRuntime.getExecutor("timeoutIDs.forEach(id => clearTimeout(id));").executeVoid(); |
| 104 | + assertEquals(1, counter.get()); |
| 105 | + nodeRuntime.getGlobalObject().delete("counter"); |
| 106 | + } catch (Throwable t) { |
| 107 | + fail(t); |
| 108 | + } finally { |
| 109 | + nodeRuntime.lowMemoryNotification(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testAwaitRunOnce() { |
| 115 | + try { |
| 116 | + nodeRuntime.setConverter(new JavetProxyConverter()); |
| 117 | + AtomicInteger counter = new AtomicInteger(); |
| 118 | + nodeRuntime.getGlobalObject().set("counter", counter); |
| 119 | + nodeRuntime.getExecutor("let timeoutIDs = [];" + |
| 120 | + "function a() {\n" + |
| 121 | + " counter.incrementAndGet();\n" + |
| 122 | + " if (counter.get() < 10) {\n" + |
| 123 | + " timeoutIDs.push(setTimeout(a, 0));\n" + |
| 124 | + " }\n" + |
| 125 | + "};\n" + |
| 126 | + "a();").executeVoid(); |
| 127 | + while (counter.get() < 2) { |
| 128 | + assertTrue(nodeRuntime.await(V8AwaitMode.RunOnce)); |
| 129 | + } |
| 130 | + nodeRuntime.getExecutor("timeoutIDs.forEach(id => clearTimeout(id));").executeVoid(); |
| 131 | + assertEquals(2, counter.get()); |
| 132 | + nodeRuntime.getGlobalObject().delete("counter"); |
| 133 | + } catch (Throwable t) { |
| 134 | + fail(t); |
| 135 | + } finally { |
| 136 | + nodeRuntime.lowMemoryNotification(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testAwaitRunTillNoMoreTasks() { |
| 142 | + try { |
| 143 | + nodeRuntime.setConverter(new JavetProxyConverter()); |
| 144 | + AtomicInteger counter = new AtomicInteger(); |
| 145 | + nodeRuntime.getGlobalObject().set("counter", counter); |
| 146 | + nodeRuntime.getExecutor("function a() {\n" + |
| 147 | + " counter.incrementAndGet();\n" + |
| 148 | + " if (counter.get() < 10) {\n" + |
| 149 | + " setTimeout(a, 0);\n" + |
| 150 | + " }\n" + |
| 151 | + "};\n" + |
| 152 | + "a();").executeVoid(); |
| 153 | + assertFalse(nodeRuntime.await(V8AwaitMode.RunTillNoMoreTasks)); |
| 154 | + assertEquals(10, counter.get()); |
| 155 | + nodeRuntime.getGlobalObject().delete("counter"); |
| 156 | + } catch (Throwable t) { |
| 157 | + fail(t); |
| 158 | + } finally { |
| 159 | + nodeRuntime.lowMemoryNotification(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
85 | 163 | @Test
|
86 | 164 | public void testConsoleArguments() throws JavetException {
|
87 | 165 | NodeRuntimeOptions runtimeOptions = new NodeRuntimeOptions();
|
|
0 commit comments