Index: tests/SystemQueue-tests.cc
===================================================================
--- tests/SystemQueue-tests.cc	(revision 12750)
+++ tests/SystemQueue-tests.cc	(revision 12751)
@@ -73,6 +73,11 @@
 public:
 
+// sleep time when waiting for the system queue (max total / each step)
+#define MAX_WAIT 100  // microseconds
+#define SLEEP_TIME 10  // microseconds
+
     SystemQueueTest() :
-        checkmark(false)
+        checkmark(false),
+        last_ordered_call(0)
     {
     }
@@ -81,15 +86,72 @@
     {
         checkmark = true;
-//         cout << "Hallo Mario ^^" << endl;
+    }
+    
+    void LongRunner()
+    {
+        usleep( MAX_WAIT / 2 );
+        
+        checkmark = true;
+    }
+    
+    
+    /// wait for the checkmark to be set by a SystemQueue call, (but not too long!)
+    void wait_for_checkmark(int max_wait = MAX_WAIT)
+    {
+        for ( int i = 0; i < max_wait / SLEEP_TIME; i++)
+        {
+            if ( checkmark )
+                break;
+            
+            cout << "/// sleeping for " << SLEEP_TIME << " microseconds ..." << endl;
+            usleep(SLEEP_TIME);
+        }
+    }
+
+    /// call that checks wheather it was performed in order
+    void OrderedCall(int num)
+    {
+        // check ordering
+        EXPECT_EQ( num, last_ordered_call + 1);
+        
+        last_ordered_call = num;
+    }
+    
+    /// like OrderedCall, but calls itself to test nested calls
+    void NestedOrderedCall(int from, int to)
+    {
+        // check ordering
+        OrderedCall(from);
+        
+        // nested call
+        if ( from < to )
+        {
+            SystemQueue::instance().scheduleCall( 
+                boost::bind(&SystemQueueTest::NestedOrderedCall,
+                            this,
+                            from+1,
+                            to)
+            );
+        }
+        else
+        {
+            /// XXX because the current/old SystemQueue does not pass the Threading test,
+            /// we have to signal, that when all nested calls are finished,
+            /// so we need to set the checkmark here..
+            
+            checkmark = true;
+        }
     }
 
     bool checkmark;
+    int last_ordered_call;
 };
 
+
+/**
+ *  schedule a call and test whether it is actually performed by the SystemQueue
+ */
 TEST_F(SystemQueueTest, ScheduleCall)
 {
-    #define MAX_WAIT 100  // microseconds
-    #define SLEEP_TIME 10  // microseconds
-
     SystemQueue& sysq = SystemQueue::instance();
     checkmark = false;  // just to be sure..
@@ -102,19 +164,113 @@
         boost::bind(&SystemQueueTest::Check, this)
     );
-    
-    // wait for the event to happen, (but not too long!)
-    for ( int i = 0; i < MAX_WAIT / SLEEP_TIME; i++)
-    {
-        if ( checkmark )
-            break;
-        
-        cout << "/// sleeping for " << SLEEP_TIME << " microseconds ..." << endl;
-        usleep(SLEEP_TIME);
-    }
-    
-    // stop
-    sysq.cancel();
-    
-    EXPECT_TRUE( checkmark ) << "Function was called within " << MAX_WAIT << " microseconds.";    
-}
-
+
+    // wait for the event to happen
+    wait_for_checkmark(MAX_WAIT);
+    
+    // stop
+    sysq.cancel();
+    
+    EXPECT_TRUE( checkmark ) << "Function was not called within " << MAX_WAIT << " microseconds.";
+}
+
+
+/**
+ *  This test actually tests two things [sorry, but it's hard to test them separately!]
+ * 
+ *  - first: the SystemQueue should not consider itself empty, while an event is processed
+ *  - second: SystemQueue events should be processed in parallel to the main thread
+ * 
+ *  NOTE: The timings here are not obvious, maybe they have to be adjusted on very slow machines
+ * 
+ *  NOTE: !! The current/old SystemQueue does NOT pass this test!!
+ * 
+ *    That's also why we need the unhandy wait_for_checkmark function,
+ *    instead a wait_until_empty function.
+ */
+TEST_F(SystemQueueTest, DISABLED_Threading)
+{
+    SystemQueue& sysq = SystemQueue::instance();
+    checkmark = false;  // just to be sure..
+    
+    // start
+    sysq.run();
+    
+    // scheduleCall
+    sysq.scheduleCall(
+        boost::bind(&SystemQueueTest::LongRunner, this)
+    );
+    
+    // SystemQueue must not be empty as long as the event is not finished
+    if ( sysq.isEmpty() )
+    {
+        // assert that this test is actually meaningful
+        ASSERT_FALSE( checkmark )
+            << "NOTE: This is not necessarily a bug, maybe the timing just have to adjusted. Try to increase MAX_WAIT.";
+
+        EXPECT_TRUE( ! sysq.isEmpty() || checkmark );
+    }
+
+    // wait for the event to finish
+    wait_for_checkmark(MAX_WAIT);
+
+    // stop
+    sysq.cancel();
+
+    // even the long-runner should finally finish
+    EXPECT_TRUE( checkmark ) << "Function has not finished within " << MAX_WAIT << " microseconds.";
+}
+
+/**
+ *  schedule multiple calls
+ * 
+ *  each call must be performed, in the correct order
+ * 
+ *  NOTE: The nested calls are not necessarily in order with calls scheduled from the main thread,
+ *  that's fine, therefore we make sure the nested calls are done, before scheduling new ones.
+ */
+TEST_F(SystemQueueTest, MultipleCalls)
+{
+    SystemQueue& sysq = SystemQueue::instance();
+    
+    // start
+    sysq.run();
+
+    sysq.scheduleCall( boost::bind(&SystemQueueTest::OrderedCall, this, 1) );
+    sysq.scheduleCall( boost::bind(&SystemQueueTest::OrderedCall, this, 2) );
+    sysq.scheduleCall( boost::bind(&SystemQueueTest::NestedOrderedCall, this, 3, 5) );
+    
+    // make sure all nested calls are done
+    wait_for_checkmark(MAX_WAIT);  // XXX should be "wait_until_empty() ...."
+
+    checkmark = false;
+    sysq.scheduleCall( boost::bind(&SystemQueueTest::OrderedCall, this, 6) );
+    
+    // XXX same here...  [ wait_until_empty() ]
+    sysq.scheduleCall( boost::bind(&SystemQueueTest::Check, this) );
+    wait_for_checkmark(MAX_WAIT);
+    
+    // evaluation
+    EXPECT_EQ( 6, last_ordered_call);
+    
+    // stop
+    sysq.cancel();
+}
+
+
+
+/**
+ *  This subclass has some special member functions suitable for timing tests
+ */
+class SystemQueueTimingTest : 
+        public SystemQueueTest
+{
+public:
+    
+};
+
+
+TEST_F(SystemQueueTimingTest, MultipleCalls)  /* TODO different name..? */
+{
+    
+}
+
