-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(JS) Expose handler function type to allow manual exporting of Lambda…
… handler Related to #246
- Loading branch information
1 parent
8edd293
commit ea4f2af
Showing
2 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
lambda/js/src/test/scala/feral/lambda/ExportedLambdaSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package feral.lambda | ||
|
||
import cats.effect.IO | ||
import cats.effect.Ref | ||
import cats.effect.kernel.Resource | ||
import cats.syntax.all._ | ||
import io.circe.scalajs._ | ||
import munit.CatsEffectSuite | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.| | ||
|
||
class ExportedLambdaSuite extends CatsEffectSuite { | ||
test("exported lambda") { | ||
val context = DummyContext.asInstanceOf[facade.Context] | ||
|
||
for { | ||
allocationCounter <- IO.ref(0) | ||
invokeCounter <- IO.ref(0) | ||
lambda = new CountingIOLambda(allocationCounter, invokeCounter) | ||
|
||
_ <- ('0' to 'z') | ||
.map(_.toString) | ||
.toList | ||
.traverse(x => | ||
IO.fromPromise(IO(lambda.impl(x, context))) | ||
.assertEquals(x.asJsAny.asInstanceOf[js.Any | Unit])) | ||
|
||
_ <- allocationCounter.get.assertEquals(1) | ||
_ <- invokeCounter.get.assertEquals(75) | ||
} yield () | ||
|
||
} | ||
} | ||
|
||
class CountingIOLambda(allocationCounter: Ref[IO, Int], invokeCounter: Ref[IO, Int]) | ||
extends IOLambda[String, String] { | ||
|
||
override def handler: Resource[IO, LambdaEnv[IO, String] => IO[Option[String]]] = | ||
Resource | ||
.eval(allocationCounter.getAndUpdate(_ + 1)) | ||
.as(_.event.map(Some(_)) <* invokeCounter.getAndUpdate(_ + 1)) | ||
|
||
// This would be exported with `@JSExportTopLevel("handler")` | ||
def impl: HandlerFn = handlerFn | ||
} | ||
|
||
object DummyContext extends js.Object { | ||
def callbackWaitsForEmptyEventLoop: Boolean = true | ||
def functionName: String = "" | ||
def functionVersion: String = "" | ||
def invokedFunctionArn: String = "" | ||
def memoryLimitInMB: String = "512" | ||
def awsRequestId: String = "" | ||
def logGroupName: String = "" | ||
def logStreamName: String = "" | ||
def identity: js.UndefOr[CognitoIdentity] = js.undefined | ||
def clientContext: js.UndefOr[ClientContext] = js.undefined | ||
def getRemainingTimeInMillis(): Double = 0 | ||
} |