Skip to content

Instantly share code, notes, and snippets.

import org.joda.time.DateTime
import org.joda.time.Minutes
val now = DateTime.now()
val past = now.minusMinutes(3)
println(Minutes.minutesBetween(past, now).minutes)
println(Minutes.minutesBetween(now, past).minutes)
@miensol
miensol / withStrictModeThreadPolicy.kt
Last active May 6, 2019 06:27
Override android strict mode policy when in android tests to allow calling network on main thread.Useful when you want to access mock web server address and you don't care about the threading.
import android.os.StrictMode
fun <T> withStrictModeThreadPolicy(threadPolicy: StrictMode.ThreadPolicy, block: () -> T): T {
val previousPolicy = StrictMode.getThreadPolicy()
StrictMode.setThreadPolicy(threadPolicy)
try {
return block()
} finally {
StrictMode.setThreadPolicy(previousPolicy)
}
// Alice.ts
import aws from 'aws-sdk';
export function main(event: any, context: any, callback: Function) {
new aws.Lambda().invoke({
FunctionName: process.env.BobFunction!,
Payload: JSON.stringify({message: "Hi!. I'm Alice."})
}, (error: Error, data: any) => {
const response = JSON.parse(data.Payload);
console.log('FromBob', error || response)
import cloudform, { Lambda, IAM, Fn } from 'cloudform';
const LambdaExecutionRole = 'LambdaExecutionRole';
cloudform({
Resources: {
HelloWorld: new Lambda.Function({
Code: {
ZipFile: "exports.wrtiteToConsole = function (event, context, callback){ console.log('Hello'); callback(null); }"
},
[LambdaExecutionRole]: new IAM.Role({
AssumeRolePolicyDocument: {
Statement: [{
Effect: "Allow",
Principal: { Service: ["lambda.amazonaws.com"] }, Action: ["sts:AssumeRole"]
}]
},
Path: "/",
ManagedPolicyArns: ["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"], Policies: [{
PolicyName: "AllowCallingOtherLambda",
import cloudform, { Lambda, IAM, Fn } from 'cloudform';
import { FunctionProperties } from 'cloudform/types/lambda/function';
import { readFileSync } from 'fs';
const LambdaExecutionRole = 'LambdaExecutionRole', Alice = 'Alice', Bob = 'Bob';
function lambdaFunction(functionCode: string, options?: Partial<FunctionProperties>) {
return new Lambda.Function({
Code: { ZipFile: functionCode },
Handler: "index.main",
Role: Fn.GetAtt(LambdaExecutionRole, "Arn"),
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ResponseStatus
import reactor.core.publisher.Flux
import reactor.core.publisher.FluxSink
import reactor.core.publisher.Mono
import reactor.core.publisher.MonoSink
import reactor.core.scheduler.Scheduler
import java.util.concurrent.Callable
class CommandInvocableHandlerMethod(private val handlerMethod: HandlerMethod,
private val requestCommandFactory: RequestCommandFactory,
private val configuration: RequestCommandConfiguration) : ServletInvocableHandlerMethod(handlerMethod) {
private lateinit var returnValueHandlers: HandlerMethodReturnValueHandlerComposite
@Trace(dispatcher = true)
override fun invokeForRequest(request: NativeWebRequest?, mavContainer: ModelAndViewContainer?, vararg providedArgs: Any?): Any {
// same as super.invokeForRequest(request, mavContainer, *providedArgs)
//but with request passed to do invoke
val args = this.getMethodArgumentValuesCallable.invoke(request, mavContainer, providedArgs)
import org.springframework.core.task.TaskExecutor
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContext
import org.springframework.security.core.context.SecurityContextHolder
class SecurityContextHolderAwareTaskExecutor(private val realTaskExecutor: TaskExecutor) : TaskExecutor {
override fun execute(task: Runnable) {
val context = SecurityContextHolder.getContext()
if (context != null) {
import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers
import org.junit.Test
import org.mockito.Mockito
import javax.inject.Inject
import javax.servlet.http.HttpServletRequest
interface Database {
fun findUser(): User