Skip to content

Commit

Permalink
Fix synchronous promises being executed multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
jameskleeh committed Sep 21, 2016
1 parent 112a441 commit 4d18ef1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit
class SynchronousPromise<T> implements Promise<T> {
Closure<T> callable
def value
boolean executed = false

SynchronousPromise(Closure<T> callable) {
this.callable = callable
Expand All @@ -51,7 +52,8 @@ class SynchronousPromise<T> implements Promise<T> {
}

T get() throws Throwable {
if (value == null) {
if (!executed) {
executed = true
try {
value = callable.call()
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,18 @@ class SynchronousPromiseFactorySpec extends Specification {
then:'the closure is executed'
1 * callable.call()
}

@Issue("GRAILS-10152")
void "Test promise closure is not executed multiple times if it returns null"() {
given:
Closure callable = Mock(Closure) {
call() >> null
}

when:"A promise is created"
Promises.waitAll([Promises.createPromise(callable), Promises.createPromise(callable)])

then:'the closure is executed twice'
2 * callable.call()
}
}

0 comments on commit 4d18ef1

Please sign in to comment.