-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #24 LGTM @estebanstatesidecr
- Loading branch information
Showing
9 changed files
with
279 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
common/src/main/kotlin/com/noheltcj/rxcommon/operators/OnErrorReturn.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.noheltcj.rxcommon.operators | ||
|
||
import com.noheltcj.rxcommon.Source | ||
import com.noheltcj.rxcommon.disposables.Disposable | ||
import com.noheltcj.rxcommon.disposables.Disposables | ||
import com.noheltcj.rxcommon.emitters.ColdEmitter | ||
import com.noheltcj.rxcommon.emitters.Emitter | ||
import com.noheltcj.rxcommon.observers.AllObserver | ||
import com.noheltcj.rxcommon.observers.Observer | ||
|
||
class OnErrorReturn<U>( | ||
private val upstream: Source<U>, | ||
private val onErrorResolveNewSource: (Throwable) -> Source<U> | ||
) : Operator<U>() { | ||
override val emitter: Emitter<U> = ColdEmitter {} | ||
|
||
override fun subscribe(observer: Observer<U>): Disposable { | ||
emitter.addObserver(observer) | ||
|
||
val upstreamDisposable = upstream.subscribe(AllObserver( | ||
onNext = { emitter.next(it) }, | ||
onError = { upstreamThrowable -> | ||
onErrorResolveNewSource(upstreamThrowable).subscribe(AllObserver( | ||
onNext = { emitter.next(it) }, | ||
onError = { emitter.terminate(it) }, | ||
onComplete = { emitter.complete() } | ||
)) | ||
}, | ||
onComplete = { emitter.complete() } | ||
)) | ||
|
||
return Disposables.create { | ||
emitter.removeObserver(observer) | ||
upstreamDisposable.dispose() | ||
} | ||
} | ||
} |
194 changes: 194 additions & 0 deletions
194
common/src/test/kotlin/com/noheltcj/rxcommon/operators/OnErrorReturnIntegrationTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package com.noheltcj.rxcommon.operators | ||
|
||
import com.noheltcj.rxcommon.disposables.Disposables | ||
import com.noheltcj.rxcommon.emitters.Emitter | ||
import com.noheltcj.rxcommon.observables.Observable | ||
import com.noheltcj.rxcommon.observables.Single | ||
import com.noheltcj.rxcommon.observers.NextObserver | ||
import com.noheltcj.rxcommon.subjects.PublishSubject | ||
import com.noheltcj.rxcommon.utility.JsName | ||
import com.noheltcj.rxcommon.utility.TestObserver | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class OnErrorReturnIntegrationTests { | ||
lateinit var testObserver: TestObserver<String> | ||
|
||
@BeforeTest | ||
fun beforeEach() { | ||
testObserver = TestObserver() | ||
} | ||
|
||
@Test | ||
@JsName("givenSourceOnError_whenSubscribing_shouldNotEmit") | ||
fun `given source to on error, when subscribing should emit nothing`() { | ||
Observable<String>() | ||
.onErrorReturn { Observable("") } | ||
.subscribe(testObserver) | ||
|
||
testObserver.assertNoEmission() | ||
} | ||
|
||
@Test | ||
@JsName("givenSourceOnError_whenSourceEmits_shouldEmitOriginalValue") | ||
fun `given source to on error, when the source emits, should emit original value`() { | ||
Observable(just = "ten") | ||
.onErrorReturn { Observable("not ten") } | ||
.subscribe(testObserver) | ||
|
||
testObserver.assertValue("ten") | ||
} | ||
|
||
@Test | ||
@JsName("givenSourceOnError_whenSourceTerminates_shouldResolveToNewSource") | ||
fun `given source to on error, when the source terminates, should resolve to new source`() { | ||
val expectedThrowable = Throwable("poof") | ||
Observable<String>(error = expectedThrowable) | ||
.onErrorReturn { Observable("not poof") } | ||
.subscribe(testObserver) | ||
|
||
testObserver.assertValue("not poof") | ||
} | ||
|
||
@Test | ||
@JsName("givenSourceOnError_whenSourceCompletes_shouldNotify") | ||
fun `given source to on error, when the source completes, should notify`() { | ||
Observable<String>(completeOnSubscribe = true) | ||
.onErrorReturn { Single("") } | ||
.subscribe(testObserver) | ||
|
||
testObserver.assertComplete() | ||
} | ||
|
||
@Test | ||
@JsName("givenDisposableSourceOnError_whenSourceCompletes_shouldNotify") | ||
fun `given disposable source to on error, when the source completes, should notify`() { | ||
val source = PublishSubject<String>() | ||
source.onErrorReturn { Single("") }.subscribe(testObserver) | ||
source.onComplete() | ||
|
||
testObserver.assertComplete() | ||
} | ||
|
||
@Test | ||
@JsName("givenSourceOnError_whenSourceTerminates_shouldNotEmit") | ||
fun `given source to on error, when the source terminates, should not emit`() { | ||
val source = PublishSubject<String>() | ||
source.onErrorReturn { Single() }.subscribe(testObserver) | ||
source.onError(RuntimeException()) | ||
|
||
testObserver.assertNoEmission() | ||
testObserver.assertNotTerminated() | ||
} | ||
|
||
@Test | ||
@JsName("givenSourceOnError_whenSourceTerminates_shouldResolveNewSourceWithUpstreamThrowable") | ||
fun `given source to on error, when the source terminates, should resolve new source upstream throwable`() { | ||
val expectedThrowable = RuntimeException("should be me") | ||
var capturedUpstreamThrowable: Throwable? = null | ||
val source = PublishSubject<String>() | ||
source.onErrorReturn { | ||
capturedUpstreamThrowable = it | ||
Single() | ||
}.subscribe(testObserver) | ||
source.onError(expectedThrowable) | ||
|
||
assertEquals(expectedThrowable, capturedUpstreamThrowable) | ||
} | ||
|
||
@Test | ||
@JsName("givenOriginalSourceTerminatedWithOnError_whenResolvedSourceEmits_shouldEmit") | ||
fun `given original source terminated with on error, when resolved source emits, should emit`() { | ||
val source = PublishSubject<String>() | ||
source.onErrorReturn { Single(just = "") }.subscribe(testObserver) | ||
source.onError(RuntimeException()) | ||
|
||
testObserver.assertValue("") | ||
} | ||
|
||
@Test | ||
@JsName("givenOriginalSourceTerminatedWithOnError_whenResolvedSourceTerminates_shouldNotify") | ||
fun `given original source terminated with on error, when resolved source terminates, should notify`() { | ||
val expectedThrowable = RuntimeException("woops") | ||
val source = PublishSubject<String>() | ||
source.onErrorReturn { Single(error = expectedThrowable) }.subscribe(testObserver) | ||
source.onError(RuntimeException()) | ||
|
||
testObserver.assertTerminated(expectedThrowable) | ||
} | ||
|
||
@Test | ||
@JsName("givenOriginalSourceTerminatedWithOnError_whenResolvedSourceCompletes_shouldNotify") | ||
fun `given original source terminated with on error, when resolved source completes, should notify`() { | ||
val source = PublishSubject<String>() | ||
source.onErrorReturn { Observable(completeOnSubscribe = true) }.subscribe(testObserver) | ||
source.onError(RuntimeException()) | ||
|
||
testObserver.assertComplete() | ||
} | ||
|
||
@Test // Test added for posterity's sake | ||
@JsName("givenOriginalSourceEmittedAndFlatMapped_andResolvedSourceTerminatedWithOnError_shouldNotDisposeFlatMapOrOriginalSource") | ||
fun `given original source emitted and flatmapped and resolved source terminated with on error, should not dispose flatmap or original source`() { | ||
lateinit var emitter: Emitter<String> | ||
val flatmapTestObserver = TestObserver<String>() | ||
val source = Observable<String>(createWithEmitter = { | ||
emitter = it | ||
Disposables.empty() | ||
}) | ||
source.subscribe(testObserver) | ||
source.flatMap { | ||
Observable<String>(error = RuntimeException()) | ||
.onErrorReturn { Single(just = "hmm") } | ||
}.subscribe(flatmapTestObserver) | ||
emitter.next("yup") | ||
|
||
testObserver.assertNotDisposed() | ||
flatmapTestObserver.assertNotDisposed() | ||
flatmapTestObserver.assertValue("hmm") | ||
} | ||
|
||
@Test | ||
@JsName("givenColdSourceOnError_whenOnlyObserverDisposed_shouldDisposeSource") | ||
fun `given cold source to on error, when the only observer disposed, should dispose source`() { | ||
val source = Observable<String>() | ||
source.onErrorReturn { Single("hi") } | ||
.subscribe(testObserver) | ||
.dispose() | ||
|
||
val sourceTestObserver = TestObserver<String>() | ||
source.subscribe(sourceTestObserver) | ||
|
||
sourceTestObserver.assertDisposed() | ||
} | ||
|
||
@Test | ||
@JsName("givenHotSourceOnError_whenOnlyObserverDisposed_shouldDisposeOperator") | ||
fun `given hot source to on error, when the only observer disposed, should dispose operator`() { | ||
val source = PublishSubject<String>() | ||
source.onErrorReturn { Single("no") }.apply { | ||
subscribe(TestObserver()).dispose() | ||
subscribe(testObserver) | ||
} | ||
|
||
testObserver.assertDisposed() | ||
} | ||
|
||
@Test | ||
@JsName("givenHotSourceOnError_whenOnlyObserverDisposed_shouldNotDisposeSource") | ||
fun `given hot source to on error, when the only observer disposed, should not dispose source`() { | ||
val source = PublishSubject<String>() | ||
source.onErrorReturn { Single("yes") }.apply { | ||
val emptyObserver = NextObserver<String> {} | ||
subscribe(emptyObserver).dispose() | ||
} | ||
|
||
val sourceTestObserver = TestObserver<String>() | ||
source.subscribe(sourceTestObserver) | ||
|
||
source.onNext("one") | ||
|
||
sourceTestObserver.assertValue("one") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
plugins { | ||
id 'kotlin-platform-js' version '1.2.70' | ||
id 'kotlin-platform-js' version '1.3.11' | ||
} | ||
|
||
dependencies { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
plugins { | ||
id 'kotlin-platform-jvm' version '1.2.70' | ||
id 'kotlin-platform-jvm' version '1.3.11' | ||
} | ||
|
||
dependencies { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters