Skip to content

Commit 6f020ce

Browse files
committed
fixing issue with rxjava based promise implementations not handling a void/null promise return time
1 parent c4c29da commit 6f020ce

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

grails-async/rxjava2/src/main/groovy/org/grails/async/factory/rxjava2/RxPromise.groovy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ class RxPromise<T> implements Promise<T> {
4141
protected boolean finished = false
4242

4343
RxPromise(RxPromiseFactory promiseFactory, Closure callable, Scheduler scheduler) {
44+
4445
this(promiseFactory, Single.create( { SingleEmitter<? super T> singleSubscriber ->
4546
try {
46-
singleSubscriber.onSuccess((T)callable.call())
47+
singleSubscriber.onSuccess((T)runCallable(callable))
4748
} catch (Throwable t) {
4849
singleSubscriber.onError(t)
4950
}
@@ -158,4 +159,13 @@ class RxPromise<T> implements Promise<T> {
158159
}
159160
}
160161
}
162+
163+
static Object runCallable(Closure callable) {
164+
Object rtn = callable.call()
165+
if(rtn == null) {
166+
return Void
167+
} else {
168+
return rtn
169+
}
170+
}
161171
}

grails-async/rxjava2/src/test/groovy/org/grails/async/factory/rxjava2/RxPromiseSpec.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ class RxPromiseSpec extends Specification {
5454
result == [one: 1, two: 2, four: 4]
5555
}
5656

57+
void 'Test promise null handling'() {
58+
59+
when: 'a promise map is created'
60+
def promise = Promises.createPromise {
61+
sleep 1000
62+
return null
63+
}
64+
def result = promise.get()
65+
66+
then: 'result is void'
67+
result == Void
68+
}
69+
5770
void 'Test promise list handling'() {
5871

5972
when: 'a promise list is created from two promises'

grails-async/rxjava3/src/main/groovy/org/grails/async/factory/rxjava3/RxPromise.groovy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class RxPromise<T> implements Promise<T> {
4141
RxPromise(RxPromiseFactory promiseFactory, Closure callable, Scheduler scheduler) {
4242
this(promiseFactory, Single.create( { SingleEmitter<? super T> singleSubscriber ->
4343
try {
44-
singleSubscriber.onSuccess((T)callable.call())
44+
singleSubscriber.onSuccess((T)runCallable(callable))
4545
} catch (Throwable t) {
4646
singleSubscriber.onError(t)
4747
}
@@ -156,4 +156,13 @@ class RxPromise<T> implements Promise<T> {
156156
}
157157
}
158158
}
159+
160+
static Object runCallable(Closure callable) {
161+
Object rtn = callable.call()
162+
if(rtn == null) {
163+
return Void
164+
} else {
165+
return rtn
166+
}
167+
}
159168
}

grails-async/rxjava3/src/test/groovy/org/grails/async/factory/rxjava3/RxPromiseSpec.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ class RxPromiseSpec extends Specification {
4141

4242
}
4343

44+
void 'Test promise null handling'() {
45+
46+
when: 'a promise map is created'
47+
def promise = Promises.createPromise {
48+
sleep 1000
49+
return null
50+
}
51+
def result = promise.get()
52+
53+
then: 'result is void'
54+
result == Void
55+
}
56+
4457
void 'Test promise map handling'() {
4558

4659
when: 'a promise map is created'

0 commit comments

Comments
 (0)