From 4d18ef1fb9adcadf4ba0b02c6ecf9af87cda64be Mon Sep 17 00:00:00 2001 From: James Kleeh Date: Wed, 21 Sep 2016 14:46:37 -0400 Subject: [PATCH] Fix synchronous promises being executed multiple times --- .../grails/async/factory/SynchronousPromise.groovy | 4 +++- .../async/SynchronousPromiseFactorySpec.groovy | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/grails-async/src/main/groovy/org/grails/async/factory/SynchronousPromise.groovy b/grails-async/src/main/groovy/org/grails/async/factory/SynchronousPromise.groovy index 92e72bc51f2..56d66c603be 100644 --- a/grails-async/src/main/groovy/org/grails/async/factory/SynchronousPromise.groovy +++ b/grails-async/src/main/groovy/org/grails/async/factory/SynchronousPromise.groovy @@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit class SynchronousPromise implements Promise { Closure callable def value + boolean executed = false SynchronousPromise(Closure callable) { this.callable = callable @@ -51,7 +52,8 @@ class SynchronousPromise implements Promise { } T get() throws Throwable { - if (value == null) { + if (!executed) { + executed = true try { value = callable.call() } catch (e) { diff --git a/grails-async/src/test/groovy/grails/async/SynchronousPromiseFactorySpec.groovy b/grails-async/src/test/groovy/grails/async/SynchronousPromiseFactorySpec.groovy index af93e97a85b..18a581d247f 100644 --- a/grails-async/src/test/groovy/grails/async/SynchronousPromiseFactorySpec.groovy +++ b/grails-async/src/test/groovy/grails/async/SynchronousPromiseFactorySpec.groovy @@ -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() + } } \ No newline at end of file