2525
2626import java .io .InputStream ;
2727import java .util .concurrent .atomic .AtomicBoolean ;
28+ import java .util .concurrent .atomic .AtomicInteger ;
2829import java .util .concurrent .atomic .AtomicLong ;
2930import java .util .concurrent .atomic .AtomicReference ;
3031
@@ -225,12 +226,19 @@ public boolean callOnGameThread() {
225226 public void testInvokeScriptBeginNotificationOnGameThread () throws Exception {
226227 final int expectedScriptId = scriptingEngine .compileScript (getDefaultScript ());
227228 final AtomicBoolean notificationReceived = new AtomicBoolean (false );
229+ final AtomicLong notificationThreadId = new AtomicLong (-1 );
230+ final AtomicBoolean beginResult = new AtomicBoolean (false );
228231
229232 scriptingEngine .invokeCompiledScript (expectedScriptId , scriptBindings , new ScriptInvocationListener () {
230233
231234 @ Override
232235 public void onScriptBegin (int scriptId ) {
236+ try {
237+ Thread .sleep (500 );
238+ } catch (Exception e ) {}
239+ scriptBindings .put ("beginNotificationResult" , true );
233240 notificationReceived .set (true );
241+ notificationThreadId .set (Thread .currentThread ().getId ());
234242 }
235243
236244 @ Override
@@ -244,6 +252,9 @@ public void onScriptSuccess(int scriptId, ScriptExecutionResult executionResult)
244252 } else {
245253 scriptResult .set (ScriptResult .SUCCESS );
246254 scriptExecuted .set (true );
255+
256+ //Ensure begin is processed before execution
257+ beginResult .set (executionResult .containsKey ("beginNotificationResult" ));
247258 }
248259 }
249260
@@ -266,6 +277,84 @@ public boolean callOnGameThread() {
266277 }
267278 });
268279 while (!scriptExecuted .get ()) {
280+ if (!notificationReceived .get ()) {
281+ try {
282+ Thread .sleep (100 );
283+ } catch (Exception e ) {}
284+ }
285+
286+ scriptingEngine .update (1f );
287+ }
288+ Assert .assertEquals (ScriptResult .SUCCESS , scriptResult .get ());
289+ Assert .assertEquals (true , gameFuture .isUpdated ());
290+ Assert .assertEquals (false , gameFuture .waitOccurred ());
291+ Assert .assertEquals (false , gameFuture .isFutureSkipped ());
292+ Assert .assertEquals (false , gameFuture .isScriptSkipped ());
293+ Assert .assertEquals (true , notificationReceived .get ());
294+ Assert .assertEquals (Thread .currentThread ().getId (), notificationThreadId .get ());
295+ }
296+
297+ @ Test
298+ public void testInvokeInteractiveScriptBeginNotificationOnGameThread () throws Exception {
299+ final int expectedScriptId = scriptingEngine .compileScript (getDefaultScript ());
300+ final AtomicBoolean notificationReceived = new AtomicBoolean (false );
301+ final AtomicLong notificationThreadId = new AtomicLong (-1 );
302+ final AtomicBoolean beginResult = new AtomicBoolean (false );
303+
304+ scriptingEngine .invokeCompiledScript (expectedScriptId , scriptBindings , new ScriptInvocationListener () {
305+
306+ @ Override
307+ public void onScriptBegin (int scriptId ) {
308+ try {
309+ Thread .sleep (500 );
310+ } catch (Exception e ) {}
311+ scriptBindings .put ("beginNotificationResult" , true );
312+ notificationReceived .set (true );
313+ notificationThreadId .set (Thread .currentThread ().getId ());
314+ }
315+
316+ @ Override
317+ public void onScriptSuccess (int scriptId , ScriptExecutionResult executionResult ) {
318+ if (scriptId != expectedScriptId ) {
319+ scriptResult .set (ScriptResult .INCORRECT_SCRIPT_ID );
320+ scriptExecuted .set (true );
321+ } else if (!checkExpectedScriptResults (executionResult )) {
322+ scriptResult .set (ScriptResult .INCORRECT_VARIABLES );
323+ scriptExecuted .set (true );
324+ } else {
325+ scriptResult .set (ScriptResult .SUCCESS );
326+ scriptExecuted .set (true );
327+
328+ //Ensure begin is processed before execution
329+ beginResult .set (executionResult .containsKey ("beginNotificationResult" ));
330+ }
331+ }
332+
333+ @ Override
334+ public void onScriptSkipped (int scriptId ) {
335+ scriptResult .set (ScriptResult .SKIPPED );
336+ scriptExecuted .set (true );
337+ }
338+
339+ @ Override
340+ public void onScriptException (int scriptId , Exception e ) {
341+ e .printStackTrace ();
342+ scriptResult .set (ScriptResult .EXCEPTION );
343+ scriptExecuted .set (true );
344+ }
345+
346+ @ Override
347+ public boolean callOnGameThread () {
348+ return true ;
349+ }
350+ }, 0 , true );
351+ while (!scriptExecuted .get ()) {
352+ if (!notificationReceived .get ()) {
353+ try {
354+ Thread .sleep (100 );
355+ } catch (Exception e ) {}
356+ }
357+
269358 scriptingEngine .update (1f );
270359 }
271360 Assert .assertEquals (ScriptResult .SUCCESS , scriptResult .get ());
@@ -274,6 +363,7 @@ public boolean callOnGameThread() {
274363 Assert .assertEquals (false , gameFuture .isFutureSkipped ());
275364 Assert .assertEquals (false , gameFuture .isScriptSkipped ());
276365 Assert .assertEquals (true , notificationReceived .get ());
366+ Assert .assertEquals (Thread .currentThread ().getId (), notificationThreadId .get ());
277367 }
278368
279369 @ Test
0 commit comments