Getting all emissions as iterable - blockingIterable operator

So, we fetched the first emitted item, we fetched the last emitted item, but what if we want all the items emitted for testing? The blockingIterable operator gets you with the same. The blockingIterable operator works in an interesting way, it passes an emission to the Iterable, then the Iterable will keep blocking the iterating thread until the next emission is available. This operator queues up unconsumed values until the Iterator can consume them, and this can cause OutOfMemory exceptions.

So following is an example, where we are obtaining the complete list and then we are checking if the emissions were sorted by converting the returned Iterable to List and checking equality with the source list after sorting. Please refer to the following code:

    @Test 
    fun `test with blockingIterable`() { 
      val list = listOf(2,10,5,6,9,8,7,1,4,3) 
 
      val observable = list.toObservable() 
               .sorted() 
 
     val iterable = observable.blockingIterable() 
     assertEquals(list.sorted(),iterable.toList()) 
   } 

If the emissions were sorted, the iterable, when converted to list, should be equal to list.sorted().

Following is the screenshot of the test result:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.232.189