From 74faf4c5fc2245e33a0deed840716a36249eb683 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Thu, 16 Mar 2017 18:05:33 +1100
Subject: [PATCH] unix/coverage: Enable scheduler and add tests for it.

---
 tests/unix/extra_coverage.py.exp | 11 +++++++++++
 unix/coverage.c                  | 29 +++++++++++++++++++++++++++++
 unix/mpconfigport_coverage.h     |  1 +
 3 files changed, 41 insertions(+)

diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp
index 32117aba4..416993887 100644
--- a/tests/unix/extra_coverage.py.exp
+++ b/tests/unix/extra_coverage.py.exp
@@ -46,6 +46,17 @@ Warning: test
 # binary
 122
 456
+# scheduler
+sched(0)=1
+sched(1)=1
+sched(2)=1
+sched(3)=1
+sched(4)=0
+unlocked
+3
+2
+1
+0
 0123456789 b'0123456789'
 7300
 7300
diff --git a/unix/coverage.c b/unix/coverage.c
index ca236c430..09959525a 100644
--- a/unix/coverage.c
+++ b/unix/coverage.c
@@ -292,6 +292,35 @@ STATIC mp_obj_t extra_coverage(void) {
         mp_printf(&mp_plat_print, "%.0lf\n", dar[0]);
     }
 
+    // scheduler
+    {
+        mp_printf(&mp_plat_print, "# scheduler\n");
+
+        // lock scheduler
+        mp_sched_lock();
+
+        // schedule multiple callbacks; last one should fail
+        for (int i = 0; i < 5; ++i) {
+            mp_printf(&mp_plat_print, "sched(%d)=%d\n", i, mp_sched_schedule(MP_OBJ_FROM_PTR(&mp_builtin_print_obj), MP_OBJ_NEW_SMALL_INT(i)));
+        }
+
+        // test nested locking/unlocking
+        mp_sched_lock();
+        mp_sched_unlock();
+
+        // shouldn't do anything while scheduler is locked
+        mp_handle_pending();
+
+        // unlock scheduler
+        mp_sched_unlock();
+        mp_printf(&mp_plat_print, "unlocked\n");
+
+        // drain pending callbacks
+        while (mp_sched_num_pending()) {
+            mp_handle_pending();
+        }
+    }
+
     mp_obj_streamtest_t *s = m_new_obj(mp_obj_streamtest_t);
     s->base.type = &mp_type_stest_fileio;
     s->buf = NULL;
diff --git a/unix/mpconfigport_coverage.h b/unix/mpconfigport_coverage.h
index 9df8d0fca..387e182db 100644
--- a/unix/mpconfigport_coverage.h
+++ b/unix/mpconfigport_coverage.h
@@ -32,6 +32,7 @@
 
 #include <mpconfigport.h>
 
+#define MICROPY_ENABLE_SCHEDULER       (1)
 #define MICROPY_PY_DELATTR_SETATTR     (1)
 #define MICROPY_PY_BUILTINS_HELP       (1)
 #define MICROPY_PY_BUILTINS_HELP_MODULES (1)
-- 
GitLab