Skip to content

Commit f43e702

Browse files
authored
Merge pull request #227 from armanbilge/issue/226
Close the output writer in JVM lambda runtime
2 parents 3c0582a + 52faa48 commit f43e702

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

lambda/jvm/src/main/scala/feral/lambda/IOLambdaPlatform.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private[lambda] abstract class IOLambdaPlatform[Event, Result]
5353
val json = result.asJson
5454
val writer = new OutputStreamWriter(output)
5555
Printer.noSpaces.unsafePrintToAppendable(json, writer)
56+
writer.close()
5657
}
5758
}
5859
} yield ()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2021 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package feral.lambda
18+
19+
import cats.effect.IO
20+
import cats.effect.kernel.Resource
21+
import com.amazonaws.services.lambda.runtime
22+
import io.circe.Json
23+
import io.circe.jawn
24+
import io.circe.literal._
25+
import munit.FunSuite
26+
27+
import java.io.ByteArrayInputStream
28+
import java.io.ByteArrayOutputStream
29+
30+
class IOLambdaJvmSuite extends FunSuite {
31+
32+
test("reads input and writes output") {
33+
34+
val input = json"""{ "foo": "bar" }"""
35+
val output = json"""{ "woozle": "heffalump" }"""
36+
37+
val lambda = new IOLambda[Json, Json] {
38+
def handler = Resource.pure(_ => IO(Some(output)))
39+
}
40+
41+
val os = new ByteArrayOutputStream
42+
43+
lambda.handleRequest(
44+
new ByteArrayInputStream(input.toString.getBytes()),
45+
os,
46+
DummyContext
47+
)
48+
49+
assertEquals(
50+
jawn.parseByteArray(os.toByteArray()),
51+
Right(output),
52+
new String(os.toByteArray())
53+
)
54+
}
55+
56+
object DummyContext extends runtime.Context {
57+
override def getAwsRequestId(): String = ""
58+
override def getLogGroupName(): String = ""
59+
override def getLogStreamName(): String = ""
60+
override def getFunctionName(): String = ""
61+
override def getFunctionVersion(): String = ""
62+
override def getInvokedFunctionArn(): String = ""
63+
override def getIdentity(): runtime.CognitoIdentity = null
64+
override def getClientContext(): runtime.ClientContext = null
65+
override def getRemainingTimeInMillis(): Int = 0
66+
override def getMemoryLimitInMB(): Int = 0
67+
override def getLogger(): runtime.LambdaLogger = null
68+
}
69+
70+
}

0 commit comments

Comments
 (0)