Skip to content

Commit

Permalink
Some more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
matrei committed Nov 21, 2023
1 parent f5e36db commit 956b0c6
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class SumService {
@Publisher
@Transactional
int sum(int a, int b) {
a + b
return a + b
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ 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')
}
}

@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())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BookSubscriber {
@Subscriber('newBook')
@SuppressWarnings('unused')
void withBook(Book book) {
newBooks << book.title
newBooks.add(book.title)
}

// tag::gorm[]
Expand All @@ -25,7 +25,7 @@ class BookSubscriber {
@Subscriber
@SuppressWarnings('unused')
void beforeInsert(PreInsertEvent event) {
insertEvents << event
insertEvents.add(event)
}
// end::gorm[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
<T> Promise<T> createPromise(Closure<T> c, List<PromiseDecorator> decorators)
<T> Promise<T> createPromise(Closure<T> closure, List<PromiseDecorator> decorators)

/**
* Creates a promise from one or many closures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ class BoundPromise<T> implements Promise<T> {
}

Promise<?> leftShift(Closure<?> callable) {
then callable
then(callable)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PromiseFactoryBuilder {
List<PromiseFactory> 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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,24 @@ class SynchronousPromise<T> implements Promise<T> {
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SynchronousPromiseFactory extends AbstractPromiseFactory {
}

@Override
Promise<Object> createPromise() {
Promise<Void> createPromise() {
throw new UnsupportedOperationException('synchronous factory does not support unfulfilled promises')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class CachedThreadPoolPromiseFactory extends AbstractPromiseFactory implements C
this.executorService = new ThreadPoolExecutor(0, maxPoolSize, timeout, unit, new SynchronousQueue<Runnable>()) {
@Override
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new FutureTaskPromise<T>(pf,callable)
return new FutureTaskPromise<T>(pf, callable)
}
@Override
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
return new FutureTaskPromise<T>(pf,runnable, value)
return new FutureTaskPromise<T>(pf, runnable, value)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.grails.async.factory.future

import grails.async.PromiseFactory
import groovy.transform.CompileStatic

import java.util.concurrent.ExecutorService

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ class FutureTaskChildPromise<T> implements Promise<T> {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class FutureTaskPromise<T> extends FutureTask<T> implements Promise<T> {
@Override
Promise<?> onComplete(Closure<?> callable) {
synchronized (successCallbacks) {
if (done) {
if (isDone()) {
try {
T value = get()
return new BoundPromise<T>(value).onComplete(callable)
Expand All @@ -103,7 +103,7 @@ class FutureTaskPromise<T> extends FutureTask<T> implements Promise<T> {
@Override
Promise<?> onError(Closure<?> callable) {
synchronized (failureCallbacks) {
if (done) {
if (isDone()) {
try {
get()
return this
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 }
Expand All @@ -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
Expand Down

0 comments on commit 956b0c6

Please sign in to comment.