title: Robolectric test needs to wait for something on a thread
body: <p>My class does this:</p>
  
  <pre><code>public void doThing() {
      Doer doer = new Doer();
      Thread thread = new Thread(doer);
      thread.start();
  }
  </code></pre>
  
  <p>The 'Doer' class is an inner class:</p>
  
  <pre><code>private class Doer implements Runnable {
      public void run() {
          Intent myIntent = new Intent(mContext, MyService.class);
          mContext.startService(myIntent);
  
          ...Some more stuff...
      }
  </code></pre>
  
  <p>This works fine.</p>
  
  <p>I need to test this using Robolectric.  Naturally <code>doThing()</code> returns immediately and I need to give the thread a chance to run before I do</p>
  
  <pre><code>ShadowApplication.getInstance().getNextStartedService()
  </code></pre>
  
  <p>How can I wait for the thread to run?</p>
  
  <p>I have tried:</p>
  
  <pre><code>Robolectric.flushForegroundThreadScheduler();
  Robolectric.flushBackgroundThreadScheduler();
  </code></pre>
  
  <p>and neither has the desired effect: they both return before my Intent has been sent.</p>
  
  <p>At the moment I have worked around it by putting a sleep into my test:</p>
  
  <pre><code>Thread.sleep(10);
  </code></pre>
  
  <p>and it does the trick, but it's clearly horrible - it's a race condition waiting to cause me grief.</p>