Guest User

Untitled

a guest
Apr 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. package com.example.chiljagossow.yeay
  2.  
  3. import org.junit.Before
  4. import org.junit.Test
  5. import org.junit.runner.RunWith
  6. import org.junit.runners.JUnit4
  7.  
  8. import io.reactivex.observers.TestObserver
  9. import io.reactivex.subjects.PublishSubject
  10.  
  11. import org.hamcrest.CoreMatchers.`is`
  12. import org.junit.Assert.assertThat
  13.  
  14.  
  15. @RunWith(JUnit4::class)
  16. class RxSemaphoreTest {
  17.  
  18. private lateinit var lock: PublishSubject<Boolean>
  19. private lateinit var observer: TestObserver<Boolean>
  20.  
  21. @Before
  22. fun setup() {
  23.  
  24. observer = TestObserver.create()
  25. lock = PublishSubject.create<Boolean>()
  26.  
  27. lock.scan(1, { counter, flag -> if (flag) counter - 1 else counter + 1 })
  28. .skip(1)
  29. .map { counter -> (counter > 0) }
  30. .subscribe(observer)
  31. }
  32.  
  33. @Test
  34. @Throws(Exception::class)
  35. fun singleEvent_result_locked() {
  36.  
  37. lock.onNext(true)
  38.  
  39. with(observer) {
  40. assertNotComplete()
  41. assertNoErrors()
  42. assertValueCount(1)
  43. assertValue(false)
  44. }
  45. }
  46.  
  47. @Test
  48. @Throws(Exception::class)
  49. fun lockAndUnlock_result_unlocked() {
  50.  
  51. with(lock) {
  52. onNext(true)
  53. onNext(false)
  54. }
  55.  
  56. with(observer) {
  57. assertNotComplete()
  58. assertNoErrors()
  59. assertValueCount(2)
  60. assertThat(values()[0], `is`(false))
  61. assertThat(values()[1], `is`(true))
  62. }
  63. }
  64.  
  65. @Test
  66. @Throws(Exception::class)
  67. fun multipleLock_singleUnlock_result_locked() {
  68.  
  69. with(lock) {
  70. onNext(true)
  71. onNext(true)
  72. onNext(true)
  73. onNext(false)
  74. }
  75.  
  76. with(observer) {
  77. assertNotComplete()
  78. assertNoErrors()
  79. assertValueCount(4)
  80. assertThat(values()[0], `is`(false))
  81. assertThat(values()[1], `is`(false))
  82. assertThat(values()[2], `is`(false))
  83. assertThat(values()[3], `is`(false))
  84. }
  85. }
  86.  
  87. @Test
  88. @Throws(Exception::class)
  89. fun multipleLock_multipleUnlock_result_unlocked() {
  90.  
  91. with(lock) {
  92. onNext(true)
  93. onNext(true)
  94. onNext(true)
  95. onNext(false)
  96. onNext(false)
  97. onNext(false)
  98. }
  99.  
  100. with(observer) {
  101. assertNotComplete()
  102. assertNoErrors()
  103. assertValueCount(6)
  104. assertThat(values()[0], `is`(false))
  105. assertThat(values()[1], `is`(false))
  106. assertThat(values()[2], `is`(false))
  107. assertThat(values()[3], `is`(false))
  108. assertThat(values()[4], `is`(false))
  109. assertThat(values()[5], `is`(true))
  110. }
  111. }
  112.  
  113. @Test
  114. @Throws(Exception::class)
  115. fun singleLock_multipleUnlock_result_unlock() {
  116.  
  117. with(lock) {
  118. onNext(true)
  119. onNext(false)
  120. onNext(false)
  121. onNext(false)
  122. }
  123.  
  124. with(observer) {
  125. assertNotComplete()
  126. assertNoErrors()
  127. assertValueCount(4)
  128. assertThat(values()[0], `is`(false))
  129. assertThat(values()[1], `is`(true))
  130. assertThat(values()[2], `is`(true))
  131. assertThat(values()[3], `is`(true))
  132. }
  133. }
  134.  
  135. @Test
  136. @Throws(Exception::class)
  137. fun singleLock_unsubscribe_result_locked() {
  138.  
  139. with(lock) {
  140. onNext(true)
  141. onComplete()
  142. onNext(false)
  143. }
  144.  
  145. with(observer) {
  146. assertComplete()
  147. assertNoErrors()
  148. assertValueCount(1)
  149. assertThat(values()[0], `is`(false))
  150. }
  151. }
  152.  
  153. @Test
  154. @Throws(Exception::class)
  155. fun singleLock_error_result_locked() {
  156. val t = Throwable()
  157.  
  158. with(lock) {
  159. onNext(true)
  160. onError(t)
  161. onNext(false)
  162. }
  163.  
  164. with(observer) {
  165. assertTerminated()
  166. assertError(t)
  167. assertValueCount(1)
  168. assertThat(values()[0], `is`(false))
  169. }
  170. }
  171.  
  172. @Test
  173. fun multipleUnlock_singleLock_result_unlocked() {
  174.  
  175. with(lock) {
  176. onNext(false)
  177. onNext(false)
  178. onNext(false)
  179. onNext(true)
  180. }
  181.  
  182. with(observer) {
  183. assertNotComplete()
  184. assertNoErrors()
  185. assertValueCount(4)
  186. assertThat(values()[0], `is`(true))
  187. assertThat(values()[1], `is`(true))
  188. assertThat(values()[2], `is`(true))
  189. assertThat(values()[3], `is`(true))
  190. }
  191. }
  192.  
  193. @Test
  194. fun disposed_observer_result_noEvents() {
  195. with(observer) {
  196. onComplete()
  197. dispose()
  198.  
  199. lock.onNext(true)
  200.  
  201. assertNoErrors()
  202. assertComplete()
  203. assertNoValues()
  204. }
  205. }
  206.  
  207. @Test
  208. fun unlock_multipleSubscribersDisposed_result_noValuesEmitted() {
  209.  
  210. val observer2 = TestObserver.create<Boolean>()
  211. lock.subscribe(observer2)
  212.  
  213. observer.dispose()
  214. observer2.dispose()
  215.  
  216. lock.onNext(false)
  217.  
  218. with(observer) {
  219. assertNoValues()
  220. assertNoErrors()
  221. }
  222.  
  223. with(observer2) {
  224. assertNoValues()
  225. assertNoErrors()
  226. }
  227. }
  228.  
  229. @Test
  230. fun unlock_singleSubscribersDisposed_result_valuesEmittedForOne() {
  231.  
  232. // I do not understand this test
  233.  
  234. val observer2 = TestObserver.create<Boolean>()
  235.  
  236. with (lock) {
  237. subscribe(observer2)
  238. onNext(true)
  239. observer2.dispose()
  240. onNext(false)
  241. }
  242.  
  243. with(observer) {
  244. assertNotComplete()
  245. assertNoErrors()
  246. assertValueCount(2)
  247. assertThat(values()[0], `is`(false))
  248. assertThat(values()[1], `is`(true))
  249. }
  250.  
  251. with(observer2) {
  252. assertNoErrors()
  253. assertValueCount(1)
  254. assertThat(values()[0], `is`(false))
  255. }
  256. }
  257. }
Add Comment
Please, Sign In to add comment