Skip to content

Commit ef3070e

Browse files
committed
Package migration into com.batchofcode.runtimelocal
Added tests Added pluggable logger to make testing print output easier
1 parent e3284ad commit ef3070e

File tree

19 files changed

+394
-62
lines changed

19 files changed

+394
-62
lines changed

build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ dependencies {
1919
compile(group = "org.http4k", name = "http4k-core", version = http4kVersion)
2020
compile(group = "org.http4k", name = "http4k-server-apache", version = http4kVersion)
2121
compile(group = "org.http4k", name = "http4k-format-jackson", version = http4kVersion)
22+
23+
testImplementation(kotlin("test"))
24+
testImplementation(kotlin("test-junit"))
25+
testImplementation("io.mockk:mockk:1.9.3")
26+
testCompile(group = "org.http4k", name = "http4k-testing-hamkrest", version = http4kVersion)
2227
}
2328

2429
tasks.withType<KotlinCompile> {

src/main/kotlin/handler/EventHandler.kt renamed to src/main/kotlin/com/batchofcode/runtimelocal/handler/EventHandler.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
package handler
1+
package com.batchofcode.runtimelocal.handler
22

3+
import com.batchofcode.runtimelocal.logging.Logger
4+
import com.batchofcode.runtimelocal.logging.PrintLogger
5+
import com.batchofcode.runtimelocal.queue.InvocationQueue
36
import org.http4k.core.Body
47
import org.http4k.core.HttpHandler
58
import org.http4k.core.Method
@@ -9,16 +12,18 @@ import org.http4k.format.Jackson.json
912
import org.http4k.routing.RoutingHttpHandler
1013
import org.http4k.routing.bind
1114
import org.http4k.routing.routes
12-
import queue.InvocationQueue
1315

1416
object EventHandler {
15-
operator fun invoke(): RoutingHttpHandler {
17+
private lateinit var logger: Logger
18+
19+
operator fun invoke(logger: Logger = PrintLogger): RoutingHttpHandler {
20+
this.logger = logger
1621
return routes(
1722
"/next" bind Method.POST to addNextEvent()
1823
)
1924
}
2025

21-
private fun addNextEvent(): HttpHandler = handler@{
26+
fun addNextEvent(): HttpHandler = handler@{
2227
val requestbody = Body.json().toLens()(it)
2328
InvocationQueue.enqueueNewInvocation(requestbody)
2429
Response(OK)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.batchofcode.runtimelocal.handler
2+
3+
import com.batchofcode.runtimelocal.logging.Logger
4+
import com.batchofcode.runtimelocal.logging.PrintLogger
5+
import org.http4k.core.HttpHandler
6+
import org.http4k.core.Method
7+
import org.http4k.core.Response
8+
import org.http4k.core.Status.Companion.OK
9+
import org.http4k.routing.RoutingHttpHandler
10+
import org.http4k.routing.bind
11+
import org.http4k.routing.routes
12+
13+
object InitHandler {
14+
private lateinit var logger: Logger
15+
16+
operator fun invoke(logger: Logger = PrintLogger): RoutingHttpHandler {
17+
this.logger = logger
18+
return routes(
19+
"/error" bind Method.POST to error()
20+
)
21+
}
22+
23+
fun error(): HttpHandler = handler@{
24+
logger.log("RUNTIME REPORTED INIT ERROR:")
25+
logger.log(it.bodyString())
26+
Response(OK)
27+
}
28+
}

src/main/kotlin/handler/InvocationHandler.kt renamed to src/main/kotlin/com/batchofcode/runtimelocal/handler/InvocationHandler.kt

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
package handler
1+
package com.batchofcode.runtimelocal.handler
22

3+
import com.batchofcode.runtimelocal.logging.Logger
4+
import com.batchofcode.runtimelocal.logging.PrintLogger
5+
import com.batchofcode.runtimelocal.queue.InvocationQueue
6+
import com.batchofcode.runtimelocal.queue.PendingRequestQueue
37
import org.http4k.core.*
48
import org.http4k.core.Status.Companion.BAD_REQUEST
59
import org.http4k.core.Status.Companion.OK
@@ -10,20 +14,21 @@ import org.http4k.routing.RoutingHttpHandler
1014
import org.http4k.routing.bind
1115
import org.http4k.routing.path
1216
import org.http4k.routing.routes
13-
import queue.InvocationQueue
14-
import queue.PendingRequestQueue
1517
import java.util.*
1618

1719
object InvocationHandler {
18-
operator fun invoke(): RoutingHttpHandler {
20+
private lateinit var logger: Logger
21+
22+
operator fun invoke(logger: Logger = PrintLogger): RoutingHttpHandler {
23+
this.logger = logger
1924
return routes(
2025
"/next" bind Method.GET to next(),
2126
"/{requestId}/response" bind Method.POST to response(),
2227
"/{requestId}/error" bind Method.POST to error()
2328
)
2429
}
2530

26-
private fun next(): HttpHandler = handler@{
31+
fun next(): HttpHandler = handler@{
2732
val nextInvocation = InvocationQueue.getInvocation() ?: return@handler Response(OK)
2833
val requestId = UUID.randomUUID().toString()
2934
PendingRequestQueue.enqueueNewRequest(requestId)
@@ -38,51 +43,51 @@ object InvocationHandler {
3843
)
3944
}
4045

41-
private fun response(): HttpHandler = handler@{
46+
fun response(): HttpHandler = handler@{
4247
val requestBody = Body.json().toLens()(it)
4348
val traceIdHeader = Header.optional("_X_AMZN_TRACE_ID")(it)
4449
val requestId = it.path("requestId") ?: return@handler Response(BAD_REQUEST).with(
4550
Body.string(ContentType.TEXT_PLAIN).toLens() of "Missing Request ID"
4651
)
4752
val startTime = PendingRequestQueue.completeRequest(requestId)
4853
if (startTime == null) {
49-
println("--------------------------------------")
50-
println("Runtime responded to $requestId but Lambda did not have an active request with that ID")
51-
println("--------------------------------------")
54+
logger.log("--------------------------------------")
55+
logger.log("Runtime responded to $requestId but Lambda did not have an active request with that ID")
56+
logger.log("--------------------------------------")
5257
Response(OK)
5358
}
5459
else {
5560
val totalTime = System.currentTimeMillis() - startTime
56-
println("--------------------------------------")
57-
println("Request $requestId completed in $totalTime ms")
58-
println("Body - $requestBody")
59-
println("Trace ID Header - $traceIdHeader")
60-
println("--------------------------------------")
61+
logger.log("--------------------------------------")
62+
logger.log("Request $requestId completed in $totalTime ms")
63+
logger.log("Body - $requestBody")
64+
logger.log("Trace ID Header - $traceIdHeader")
65+
logger.log("--------------------------------------")
6166
Response(OK)
6267
}
6368
}
6469

65-
private fun error(): HttpHandler = handler@{
70+
fun error(): HttpHandler = handler@{
6671
val requestBody = Body.json().toLens()(it)
6772
val traceIdHeader = Header.optional("_X_AMZN_TRACE_ID")(it)
6873
val requestId = it.path("requestId") ?: return@handler Response(BAD_REQUEST).with(
6974
Body.string(ContentType.TEXT_PLAIN).toLens() of "Missing Request ID"
7075
)
7176
val startTime = PendingRequestQueue.completeRequest(requestId)
7277
if (startTime == null) {
73-
println("--------------------------------------")
74-
println("Runtime responded to $requestId but Lambda did not have an active request with that ID")
75-
println("--------------------------------------")
78+
logger.log("--------------------------------------")
79+
logger.log("Runtime responded to $requestId but Lambda did not have an active request with that ID")
80+
logger.log("--------------------------------------")
7681
Response(OK)
7782
}
7883
else {
7984
val totalTime = System.currentTimeMillis() - startTime
80-
println("--------------------------------------")
81-
println("REQUEST RETURNED AN ERROR!")
82-
println("Request $requestId completed in $totalTime ms")
83-
println("Error Body - $requestBody")
84-
println("Trace ID Header - $traceIdHeader")
85-
println("--------------------------------------")
85+
logger.log("--------------------------------------")
86+
logger.log("REQUEST RETURNED AN ERROR!")
87+
logger.log("Request $requestId completed in $totalTime ms")
88+
logger.log("Error Body - $requestBody")
89+
logger.log("Trace ID Header - $traceIdHeader")
90+
logger.log("--------------------------------------")
8691
Response(OK)
8792
}
8893

src/main/kotlin/handler/RootHandler.kt renamed to src/main/kotlin/com/batchofcode/runtimelocal/handler/RootHandler.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
package handler
1+
package com.batchofcode.runtimelocal.handler
22

3-
import org.http4k.routing.bind
4-
import org.http4k.routing.routes
3+
import org.http4k.core.then
4+
import org.http4k.filter.ServerFilters
55
import org.http4k.server.ApacheServer
66
import org.http4k.server.asServer
77

88
fun main() {
99
val port = 9000
10-
val routes = routes(
11-
"/invocation" bind InvocationHandler(),
12-
"/events" bind EventHandler(),
13-
"/init" bind InitHandler()
14-
)
1510

16-
val server = routes("/2018-06-01/runtime" bind routes).asServer(ApacheServer(port)).start()
11+
ServerFilters.CatchAll()
12+
.then(Routes())
13+
.asServer(ApacheServer(port)).start()
14+
1715
println("Started Lambda Runtime Local on Port $port")
1816
println("GET invocations - http://localhost:$port/2018-06-01/runtime/invocation/next")
1917
println("POST events - http://localhost:$port/2018-06-01/runtime/events/next")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.batchofcode.runtimelocal.handler
2+
3+
import org.http4k.routing.bind
4+
import org.http4k.routing.routes
5+
6+
object Routes {
7+
operator fun invoke() = routes("/2018-06-01/runtime" bind
8+
routes(
9+
"/invocation" bind InvocationHandler(),
10+
"/init" bind InitHandler()
11+
),
12+
"" bind routes("/event" bind EventHandler())
13+
)
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.batchofcode.runtimelocal.logging
2+
3+
interface Logger {
4+
fun log(message: String)
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.batchofcode.runtimelocal.logging
2+
3+
object PrintLogger: Logger {
4+
override fun log(message: String) = println(message)
5+
}

src/main/kotlin/queue/InvocationQueue.kt renamed to src/main/kotlin/com/batchofcode/runtimelocal/queue/InvocationQueue.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
package queue
1+
package com.batchofcode.runtimelocal.queue
22

33
import com.fasterxml.jackson.databind.JsonNode
44

55
object InvocationQueue {
66
private val invocationRequests = mutableListOf<JsonNode>()
77

8+
fun invocationCount(): Int = invocationRequests.size
9+
10+
fun clear() = invocationRequests.clear()
11+
812
fun enqueueNewInvocation(payload: JsonNode) {
913
invocationRequests.add(payload)
1014
println("[ ${invocationRequests.size} PENDING EVENTS ] - Queued event for invocation: $payload ")

src/main/kotlin/queue/PendingRequestQueue.kt renamed to src/main/kotlin/com/batchofcode/runtimelocal/queue/PendingRequestQueue.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
package queue
1+
package com.batchofcode.runtimelocal.queue
22

33
object PendingRequestQueue {
44
private val pendingRequests = mutableMapOf<String, Long>()
55

6+
fun pendingRequestCount(): Int = pendingRequests.size
7+
8+
fun clear() = pendingRequests.clear()
9+
610
fun enqueueNewRequest(requestId: String) {
711
pendingRequests[requestId] = System.currentTimeMillis()
812
}

0 commit comments

Comments
 (0)