@@ -357,6 +357,68 @@ public void testScheduledCallableCompareToIsStable() throws Exception {
357357 }
358358 }
359359
360+ @ Test
361+ public void testHighPriorityTasksExecuteFirst () {
362+ YdocScheduledExecutorService service = new YdocScheduledExecutorService ();
363+ List <String > executionOrder = new ArrayList <>();
364+
365+ // Submit regular tasks first
366+ service .submit (() -> executionOrder .add ("regular1" ));
367+ service .submit (() -> executionOrder .add ("regular2" ));
368+
369+ // Submit high-priority tasks via execute() with HighPriorityRunnable
370+ service .execute (
371+ new YdocScheduledExecutorService .HighPriorityRunnable (() -> executionOrder .add ("high1" )));
372+ service .execute (
373+ new YdocScheduledExecutorService .HighPriorityRunnable (() -> executionOrder .add ("high2" )));
374+
375+ service .processPendingTasks ();
376+
377+ // High-priority tasks should execute before regular tasks
378+ assertEquals (List .of ("high1" , "high2" , "regular1" , "regular2" ), executionOrder );
379+ }
380+
381+ @ Test
382+ public void testHighPriorityViewRoutesToHighPriorityQueue () {
383+ YdocScheduledExecutorService service = new YdocScheduledExecutorService ();
384+ var highPriorityView = service .createHighPriorityView ();
385+ List <String > executionOrder = new ArrayList <>();
386+
387+ // Submit regular tasks directly
388+ service .submit (() -> executionOrder .add ("regular1" ));
389+ service .submit (() -> executionOrder .add ("regular2" ));
390+
391+ // Submit via high-priority view
392+ highPriorityView .execute (() -> executionOrder .add ("high1" ));
393+ highPriorityView .execute (() -> executionOrder .add ("high2" ));
394+
395+ service .processPendingTasks ();
396+
397+ // View tasks should execute before regular tasks
398+ assertEquals (List .of ("high1" , "high2" , "regular1" , "regular2" ), executionOrder );
399+ }
400+
401+ @ Test
402+ public void testHighPriorityTasksInterleavedDuringProcessing () {
403+ YdocScheduledExecutorService service = new YdocScheduledExecutorService ();
404+ var highPriorityView = service .createHighPriorityView ();
405+ List <String > executionOrder = new ArrayList <>();
406+
407+ // Submit regular tasks where the first one submits a high-priority task during execution
408+ service .submit (
409+ () -> {
410+ executionOrder .add ("regular1" );
411+ // Simulate a WebSocket message arriving while processing regular tasks
412+ highPriorityView .execute (() -> executionOrder .add ("high-interleaved" ));
413+ });
414+ service .submit (() -> executionOrder .add ("regular2" ));
415+
416+ service .processPendingTasks ();
417+
418+ // The high-priority task submitted during regular1 should execute before regular2
419+ assertEquals (List .of ("regular1" , "high-interleaved" , "regular2" ), executionOrder );
420+ }
421+
360422 @ Test
361423 public void testEventLoopPattern () throws InterruptedException {
362424 YdocScheduledExecutorService service = new YdocScheduledExecutorService ();
0 commit comments