From 956b0c6cddbb25374f0bde08095ac49b13f4351e Mon Sep 17 00:00:00 2001 From: Mattias Reichel Date: Tue, 21 Nov 2023 12:02:35 +0100 Subject: [PATCH] Some more cleanup --- .../init/pubsub/demo/Application.groovy | 2 +- .../services/pubsub/demo/BookService.groovy | 1 - .../services/pubsub/demo/SumService.groovy | 2 +- .../groovy/pubsub/demo/PubSubSpec.groovy | 33 ++++++++++--------- .../pubsub/demo/TaskControllerSpec.groovy | 6 ++-- .../main/groovy/pubsub/demo/BookChecks.groovy | 10 +++--- .../groovy/pubsub/demo/BookSubscriber.groovy | 4 +-- .../groovy/grails/async/PromiseFactory.groovy | 4 +-- .../grails/async/factory/BoundPromise.groovy | 2 +- .../factory/PromiseFactoryBuilder.groovy | 2 +- .../async/factory/SynchronousPromise.groovy | 10 +++--- .../factory/SynchronousPromiseFactory.groovy | 2 +- .../CachedThreadPoolPromiseFactory.groovy | 4 +-- .../future/ExecutorPromiseFactory.groovy | 1 - .../future/FutureTaskChildPromise.groovy | 4 +-- .../factory/future/FutureTaskPromise.groovy | 4 +-- .../grails/async/PromiseListSpec.groovy | 6 ++-- .../groovy/grails/async/PromiseMapSpec.groovy | 10 +++--- 18 files changed, 54 insertions(+), 53 deletions(-) diff --git a/examples/pubsub-demo/grails-app/init/pubsub/demo/Application.groovy b/examples/pubsub-demo/grails-app/init/pubsub/demo/Application.groovy index 5c9fe817..7cc02536 100644 --- a/examples/pubsub-demo/grails-app/init/pubsub/demo/Application.groovy +++ b/examples/pubsub-demo/grails-app/init/pubsub/demo/Application.groovy @@ -9,6 +9,6 @@ import org.springframework.context.annotation.ComponentScan @CompileStatic class Application extends GrailsAutoConfiguration { static void main(String[] args) { - GrailsApp.run Application, args + GrailsApp.run(Application, args) } } \ No newline at end of file diff --git a/examples/pubsub-demo/grails-app/services/pubsub/demo/BookService.groovy b/examples/pubsub-demo/grails-app/services/pubsub/demo/BookService.groovy index e16d68d7..e6c9d907 100644 --- a/examples/pubsub-demo/grails-app/services/pubsub/demo/BookService.groovy +++ b/examples/pubsub-demo/grails-app/services/pubsub/demo/BookService.groovy @@ -2,7 +2,6 @@ package pubsub.demo import grails.events.annotation.Publisher import grails.gorm.services.Service -import groovy.transform.CompileStatic @Service(Book) interface BookService { diff --git a/examples/pubsub-demo/grails-app/services/pubsub/demo/SumService.groovy b/examples/pubsub-demo/grails-app/services/pubsub/demo/SumService.groovy index 23979c35..2b5b6fd3 100644 --- a/examples/pubsub-demo/grails-app/services/pubsub/demo/SumService.groovy +++ b/examples/pubsub-demo/grails-app/services/pubsub/demo/SumService.groovy @@ -10,6 +10,6 @@ class SumService { @Publisher @Transactional int sum(int a, int b) { - a + b + return a + b } } diff --git a/examples/pubsub-demo/src/integration-test/groovy/pubsub/demo/PubSubSpec.groovy b/examples/pubsub-demo/src/integration-test/groovy/pubsub/demo/PubSubSpec.groovy index bcfa1294..0a073bd5 100644 --- a/examples/pubsub-demo/src/integration-test/groovy/pubsub/demo/PubSubSpec.groovy +++ b/examples/pubsub-demo/src/integration-test/groovy/pubsub/demo/PubSubSpec.groovy @@ -4,6 +4,7 @@ import grails.gorm.transactions.Rollback import grails.testing.mixin.integration.Integration import jakarta.inject.Inject import spock.lang.Specification +import spock.util.concurrent.PollingConditions /** * Created by graemerocher on 03/04/2017. @@ -19,43 +20,45 @@ class PubSubSpec extends Specification { void 'Test event bus within Grails'() { when: 'we invoke methods on the publisher' - sumService.sum 1, 2 - sleep 500 - sumService.sum 1, 2 - sleep 500 + sumService.sum(1, 2) + sumService.sum(1, 2) then: 'the subscriber should receive the events' - totalService.accumulatedTotal == 6 + new PollingConditions().eventually { + totalService.accumulatedTotal == 6 + } } @Rollback void 'Test event from data service with rollback'() { when: 'a transaction is rolled back' - bookService.saveBook 'The Stand' - sleep 500 + bookService.saveBook('The Stand') then: 'no event is fired' - bookSubscriber.newBooks == [] - bookSubscriber.insertEvents.empty + new PollingConditions(initialDelay: 0.5).eventually { + bookSubscriber.newBooks == [] + bookSubscriber.insertEvents.empty + } } void 'Test event from data service'() { when: 'a transaction is committed' - bookService.saveBook'The Stand' - sleep 500 + bookService.saveBook('The Stand') then: 'the event is fired and received' - bookSubscriber.newBooks == ['The Stand'] - bookSubscriber.insertEvents.size() == 1 + new PollingConditions().eventually { + bookSubscriber.newBooks == ['The Stand'] + bookSubscriber.insertEvents.size() == 1 + } } @Rollback void 'Test modify property event listener'() { when: 'when an event listener modifies a property' - bookService.saveBook'funny book' + bookService.saveBook('funny book') then: 'the property was modified' Book.findByTitle('Humor - funny book') != null @@ -67,7 +70,7 @@ class PubSubSpec extends Specification { void 'Test synchronous event listener'() { when: 'when a event listener cancels an insert' - bookService.saveBook 'UK Politics' + bookService.saveBook('UK Politics') // due to https://hibernate.atlassian.net/browse/HHH-11721 // an exception must be thrown diff --git a/examples/pubsub-demo/src/integration-test/groovy/pubsub/demo/TaskControllerSpec.groovy b/examples/pubsub-demo/src/integration-test/groovy/pubsub/demo/TaskControllerSpec.groovy index c89eda00..2e8a6ee8 100644 --- a/examples/pubsub-demo/src/integration-test/groovy/pubsub/demo/TaskControllerSpec.groovy +++ b/examples/pubsub-demo/src/integration-test/groovy/pubsub/demo/TaskControllerSpec.groovy @@ -21,14 +21,14 @@ class TaskControllerSpec extends Specification { HttpClient client void setup() { - client = HttpClient.create "http://localhost:$serverPort".toURL() + client = HttpClient.create("http://localhost:$serverPort".toURL()) } void 'test async error handling'() { when: 'we invoke an endpoint that throws an exception' - def request = HttpRequest.GET '/task/error' - client.toBlocking().exchange request, Argument.of(String), Argument.of(String) + def request = HttpRequest.GET('/task/error') + client.toBlocking().exchange(request, Argument.of(String), Argument.of(String)) then: 'the response is as expected' def e = thrown(HttpClientResponseException) diff --git a/examples/pubsub-demo/src/main/groovy/pubsub/demo/BookChecks.groovy b/examples/pubsub-demo/src/main/groovy/pubsub/demo/BookChecks.groovy index ff9e7e55..a364de8b 100644 --- a/examples/pubsub-demo/src/main/groovy/pubsub/demo/BookChecks.groovy +++ b/examples/pubsub-demo/src/main/groovy/pubsub/demo/BookChecks.groovy @@ -12,8 +12,8 @@ class BookChecks { @Listener(Book) @SuppressWarnings('unused') static void checkBook(PreInsertEvent event) { - String title = event.entityAccess.getPropertyValue'title' - if (title?.contains 'Politics') { + String title = event.entityAccess.getPropertyValue('title') + if (title?.contains('Politics')) { throw new IllegalArgumentException('Books about politics not allowed') } } @@ -21,9 +21,9 @@ class BookChecks { @Listener(Book) @SuppressWarnings('unused') static void tagFunnyBooks(PreInsertEvent event) { - String title = event.entityAccess.getPropertyValue'title' - if(title?.contains 'funny') { - event.entityAccess.setProperty 'title', "Humor - ${title}".toString() + String title = event.entityAccess.getPropertyValue('title') + if(title?.contains('funny')) { + event.entityAccess.setProperty('title', "Humor - ${title}".toString()) } } diff --git a/examples/pubsub-demo/src/main/groovy/pubsub/demo/BookSubscriber.groovy b/examples/pubsub-demo/src/main/groovy/pubsub/demo/BookSubscriber.groovy index 940c2831..9be5dd8d 100644 --- a/examples/pubsub-demo/src/main/groovy/pubsub/demo/BookSubscriber.groovy +++ b/examples/pubsub-demo/src/main/groovy/pubsub/demo/BookSubscriber.groovy @@ -16,7 +16,7 @@ class BookSubscriber { @Subscriber('newBook') @SuppressWarnings('unused') void withBook(Book book) { - newBooks << book.title + newBooks.add(book.title) } // tag::gorm[] @@ -25,7 +25,7 @@ class BookSubscriber { @Subscriber @SuppressWarnings('unused') void beforeInsert(PreInsertEvent event) { - insertEvents << event + insertEvents.add(event) } // end::gorm[] } diff --git a/grails-async-core/src/main/groovy/grails/async/PromiseFactory.groovy b/grails-async-core/src/main/groovy/grails/async/PromiseFactory.groovy index 35f373ad..8a88d1f5 100644 --- a/grails-async-core/src/main/groovy/grails/async/PromiseFactory.groovy +++ b/grails-async-core/src/main/groovy/grails/async/PromiseFactory.groovy @@ -102,10 +102,10 @@ interface PromiseFactory { /** * Creates a promise from one or many closures * - * @param c One or many closures + * @param closure A closure * @return A promise */ - Promise createPromise(Closure c, List decorators) + Promise createPromise(Closure closure, List decorators) /** * Creates a promise from one or many closures diff --git a/grails-async-core/src/main/groovy/org/grails/async/factory/BoundPromise.groovy b/grails-async-core/src/main/groovy/org/grails/async/factory/BoundPromise.groovy index f88a7657..ffae5ccf 100644 --- a/grails-async-core/src/main/groovy/org/grails/async/factory/BoundPromise.groovy +++ b/grails-async-core/src/main/groovy/org/grails/async/factory/BoundPromise.groovy @@ -95,6 +95,6 @@ class BoundPromise implements Promise { } Promise leftShift(Closure callable) { - then callable + then(callable) } } diff --git a/grails-async-core/src/main/groovy/org/grails/async/factory/PromiseFactoryBuilder.groovy b/grails-async-core/src/main/groovy/org/grails/async/factory/PromiseFactoryBuilder.groovy index 16443ed8..ebd8fa35 100644 --- a/grails-async-core/src/main/groovy/org/grails/async/factory/PromiseFactoryBuilder.groovy +++ b/grails-async-core/src/main/groovy/org/grails/async/factory/PromiseFactoryBuilder.groovy @@ -24,7 +24,7 @@ class PromiseFactoryBuilder { List promiseFactories = ServiceLoader.load(PromiseFactory).toList() PromiseFactory promiseFactory - if(promiseFactories.empty) { + if(promiseFactories.isEmpty()) { log.debug 'No PromiseFactory implementation found. Using default ExecutorService promise factory.' promiseFactory = new CachedThreadPoolPromiseFactory() } diff --git a/grails-async-core/src/main/groovy/org/grails/async/factory/SynchronousPromise.groovy b/grails-async-core/src/main/groovy/org/grails/async/factory/SynchronousPromise.groovy index 569559fd..6c3d38b0 100644 --- a/grails-async-core/src/main/groovy/org/grails/async/factory/SynchronousPromise.groovy +++ b/grails-async-core/src/main/groovy/org/grails/async/factory/SynchronousPromise.groovy @@ -74,24 +74,24 @@ class SynchronousPromise implements Promise { return this } - Promise onComplete(Closure callable) { + Promise onComplete(Closure callable) { try { callable.call(get()) } catch (Throwable ignored) {} return this } - Promise onError(Closure callable) { + Promise onError(Closure callable) { try { get() } catch (Throwable e) { callable.call(e) } return this } - Promise then(Closure callable) { + Promise then(Closure callable) { final value = get() return new SynchronousPromise(callable.curry(value)) } - Promise leftShift(Closure callable) { - then callable + Promise leftShift(Closure callable) { + then(callable) } } diff --git a/grails-async-core/src/main/groovy/org/grails/async/factory/SynchronousPromiseFactory.groovy b/grails-async-core/src/main/groovy/org/grails/async/factory/SynchronousPromiseFactory.groovy index 9c126c96..2ac5a11e 100644 --- a/grails-async-core/src/main/groovy/org/grails/async/factory/SynchronousPromiseFactory.groovy +++ b/grails-async-core/src/main/groovy/org/grails/async/factory/SynchronousPromiseFactory.groovy @@ -40,7 +40,7 @@ class SynchronousPromiseFactory extends AbstractPromiseFactory { } @Override - Promise createPromise() { + Promise createPromise() { throw new UnsupportedOperationException('synchronous factory does not support unfulfilled promises') } diff --git a/grails-async-core/src/main/groovy/org/grails/async/factory/future/CachedThreadPoolPromiseFactory.groovy b/grails-async-core/src/main/groovy/org/grails/async/factory/future/CachedThreadPoolPromiseFactory.groovy index cc431599..fba0dc0a 100644 --- a/grails-async-core/src/main/groovy/org/grails/async/factory/future/CachedThreadPoolPromiseFactory.groovy +++ b/grails-async-core/src/main/groovy/org/grails/async/factory/future/CachedThreadPoolPromiseFactory.groovy @@ -27,11 +27,11 @@ class CachedThreadPoolPromiseFactory extends AbstractPromiseFactory implements C this.executorService = new ThreadPoolExecutor(0, maxPoolSize, timeout, unit, new SynchronousQueue()) { @Override protected RunnableFuture newTaskFor(Callable callable) { - return new FutureTaskPromise(pf,callable) + return new FutureTaskPromise(pf, callable) } @Override protected RunnableFuture newTaskFor(Runnable runnable, T value) { - return new FutureTaskPromise(pf,runnable, value) + return new FutureTaskPromise(pf, runnable, value) } } } diff --git a/grails-async-core/src/main/groovy/org/grails/async/factory/future/ExecutorPromiseFactory.groovy b/grails-async-core/src/main/groovy/org/grails/async/factory/future/ExecutorPromiseFactory.groovy index 1217abcc..9430dec6 100644 --- a/grails-async-core/src/main/groovy/org/grails/async/factory/future/ExecutorPromiseFactory.groovy +++ b/grails-async-core/src/main/groovy/org/grails/async/factory/future/ExecutorPromiseFactory.groovy @@ -1,7 +1,6 @@ package org.grails.async.factory.future import grails.async.PromiseFactory -import groovy.transform.CompileStatic import java.util.concurrent.ExecutorService diff --git a/grails-async-core/src/main/groovy/org/grails/async/factory/future/FutureTaskChildPromise.groovy b/grails-async-core/src/main/groovy/org/grails/async/factory/future/FutureTaskChildPromise.groovy index 811ef281..3356229c 100644 --- a/grails-async-core/src/main/groovy/org/grails/async/factory/future/FutureTaskChildPromise.groovy +++ b/grails-async-core/src/main/groovy/org/grails/async/factory/future/FutureTaskChildPromise.groovy @@ -96,9 +96,9 @@ class FutureTaskChildPromise implements Promise { return bound.get() } else { - if(parent instanceof FutureTaskPromise) { + if (parent instanceof FutureTaskPromise) { def value = parent.get() - if(bound == null) { + if (bound == null) { def v = callable.call(value) bound = new BoundPromise<>(v) } diff --git a/grails-async-core/src/main/groovy/org/grails/async/factory/future/FutureTaskPromise.groovy b/grails-async-core/src/main/groovy/org/grails/async/factory/future/FutureTaskPromise.groovy index a3691a00..32e9d129 100644 --- a/grails-async-core/src/main/groovy/org/grails/async/factory/future/FutureTaskPromise.groovy +++ b/grails-async-core/src/main/groovy/org/grails/async/factory/future/FutureTaskPromise.groovy @@ -84,7 +84,7 @@ class FutureTaskPromise extends FutureTask implements Promise { @Override Promise onComplete(Closure callable) { synchronized (successCallbacks) { - if (done) { + if (isDone()) { try { T value = get() return new BoundPromise(value).onComplete(callable) @@ -103,7 +103,7 @@ class FutureTaskPromise extends FutureTask implements Promise { @Override Promise onError(Closure callable) { synchronized (failureCallbacks) { - if (done) { + if (isDone()) { try { get() return this diff --git a/grails-async-core/src/test/groovy/grails/async/PromiseListSpec.groovy b/grails-async-core/src/test/groovy/grails/async/PromiseListSpec.groovy index e28d0f33..3f0fad96 100644 --- a/grails-async-core/src/test/groovy/grails/async/PromiseListSpec.groovy +++ b/grails-async-core/src/test/groovy/grails/async/PromiseListSpec.groovy @@ -1,14 +1,14 @@ /* * Copyright 2013 SpringSource * - * Licensed under the Apache License, Version 2.0 (the 'License'); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an 'AS IS' BASIS, + * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -34,7 +34,7 @@ class PromiseListSpec extends Specification { list << { 2 } list << { 3 } def result = null - list.onComplete { result = it }.then{} + list.onComplete { result = it } then: 'then the result from onComplete is correct' new PollingConditions().eventually { diff --git a/grails-async-core/src/test/groovy/grails/async/PromiseMapSpec.groovy b/grails-async-core/src/test/groovy/grails/async/PromiseMapSpec.groovy index 5037ab8a..7af91361 100644 --- a/grails-async-core/src/test/groovy/grails/async/PromiseMapSpec.groovy +++ b/grails-async-core/src/test/groovy/grails/async/PromiseMapSpec.groovy @@ -29,12 +29,12 @@ class PromiseMapSpec extends Specification { when: 'a promise map is used with an onComplete handler' def map = new PromiseMap(one: { 1 }, four: 4, eight: { 4 * 2 }) - def result + def result = null map.onComplete { result = it } then: 'an appropriately populated map is returned to the onComplete event' new PollingConditions().eventually { - result != null + result result['one'] == 1 result['four'] == 4 result['eight'] == 8 @@ -51,7 +51,7 @@ class PromiseMapSpec extends Specification { def result = null map.onComplete { result = it } - then: 'An appropriately populated map is returned to the onComplete event' + then: 'an appropriately populated map is returned to the onComplete event' new PollingConditions().eventually { result result['one'] == 1 @@ -62,7 +62,7 @@ class PromiseMapSpec extends Specification { void 'Test that a PromiseMap populates values from promises onComplete'() { - when:"A promise map is used with an onComplete handler" + when: 'a promise map is used with an onComplete handler' def map = new PromiseMap() map['one'] = { 1 } map['four'] = { 2 + 2 } @@ -71,7 +71,7 @@ class PromiseMapSpec extends Specification { map.onComplete { result = it } sleep 300 - then:'An appropriately populated map is returned to the onComplete event' + then: 'an appropriately populated map is returned to the onComplete event' result result['one'] == 1 result['four'] == 4