Skip to content

Instantly share code, notes, and snippets.

View ctoestreich's full-sized avatar

Christian Oestreich ctoestreich

View GitHub Profile
@ctoestreich
ctoestreich / split_params.groovy
Created April 29, 2013 20:24
Test for splitting grails params for scripts
assert doSplit('''-compile
-wsdl=some/path''') == ['-compile','-wsdl','some/path']
assert doSplit('-compile -wsdl=some/path') == ['-compile','-wsdl','some/path']
assert doSplit('''-compile -wsdl=some/path
-test ''') == ['-compile','-wsdl','some/path', '-test']
assert doSplit('''-compile
-wsdl=some/path/to/wsdl
package org.grails.demo
import grails.plugins.rest.client.RequestCustomizer
import grails.plugins.rest.client.RestResponse
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.client.RestTemplate
public class MockRestBuilder {
package org.grails.demo
import grails.plugins.rest.client.RestBuilder
import spock.lang.Specification
class BaseRestBuilderSpec extends Specification {
def setup() {
RestBuilder.metaClass.constructor = { ->
return new MockRestBuilder()
package org.grails.demo
import grails.plugins.rest.client.RequestCustomizer
import grails.plugins.rest.client.RestResponse
import grails.test.mixin.TestFor
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
@TestFor(ExternalRestBuilderService)
class extends BaseRestBuilderSpec {
package org.grails.demo
import grails.plugins.rest.client.RestBuilder
import grails.plugins.rest.client.RestResponse
import org.springframework.http.HttpStatus
class ExternalRestBuilderService {
void invokeRestBuilderPostWithUrl(String url) {
package com.bsb.interceptor
import grails.artefact.Interceptor
import grails.util.Holders
import groovy.transform.CompileStatic
import org.hibernate.SessionFactory
import org.hibernate.stat.Statistics
@CompileStatic
class HibernateInterceptor implements Interceptor {
// deletes all keys matching a pattern (see redis "keys" documentation for more)
// OK for low traffic methods, but expensive compared to other redis commands
// perf test before relying on this rather than storing your own set of keys to
// delete
void deleteKeysWithPattern(keyPattern) {
if (log.infoEnabled) log.info("Cleaning all redis keys with pattern [${keyPattern}]")
withRedis { Jedis redis ->
String[] keys = redis.keys(keyPattern)
if(keys) redis.del(keys)
}
@ctoestreich
ctoestreich / controller.groovy
Last active June 17, 2016 15:01
Grails Async Sample
package foo
import grails.async.Promise
// more imports
import static grails.async.Promises.waitAll
import static grails.async.Promises.task
class ProductService {
//Go get promotions async
Promise<Map<String, List<Promotion>> promisePromotions = task { getPromitions(...) }
//Get the map from the promise
Map<String, List<Promotion>> promotionMessages = promisePromotions.get()
//Get the list of promitions from the map
List<Promotion> promotionMessages = promotionMessages.get(mapKey)
@ctoestreich
ctoestreich / PromiseUtil.groovy
Created June 20, 2016 20:42
Promise utility for ensuring timeouts happen in grails 3
import grails.async.Promise
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
@Slf4j
@CompileStatic