ReactiveX/RxJava — pull request #1955 by akarnokd (2014-12-11 21:23:58+00:00)
Title: Fixed race & late termination condition.

This is quite a complex operator with lots of cases.

Properties:
1) If there aren't any elements queued up and nothing is requested but terminal event received, emit terminal event and quit.
2) If there are elements in the queue and a terminal flag, and at least the same amount is requested, deliver the events and the terminal event.
3) If more was requested and more became available just after the loop and before the synchronized block, keep looping.
3.a) If more was requested but nothing is available or nothing was requested and something is available: quit and let either the onNext or request do the subsequent drain.
3.b) If elements and termination was produced but not requested: quit and let the request do the drain
3.c) If termination was requested and no elements produced: loop , emit terminal event and quit.

In table form:
```
Available | Terminated | Requested | Action | Reason
yes yes yes loop can deliver available + termination
yes yes false quit can't deliver available
yes no yes loop can deliver available
yes no no quit can't deliver available
no yes yes loop loop will deliver termination only
no yes no loop loop will deliver termination only
no no yes quit nothing to deliver
no no no quit nothing to deliver
```