Skip to content

Commit

Permalink
add tests for lambdaapp
Browse files Browse the repository at this point in the history
  • Loading branch information
kenoir committed Dec 19, 2024
1 parent 0faf41e commit 14a47a4
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import weco.pipeline.relation_embedder.lib._

import scala.concurrent.Future


object LambdaMain
extends LambdaApp[SQSEvent, String, RelationEmbedderConfig]
with RelationEmbedderConfigurable {
extends LambdaApp[SQSEvent, String, RelationEmbedderConfig]
with RelationEmbedderConfigurable {

import SQSEventOps._
private lazy val batchProcessor = BatchProcessor(config)

def processEvent(event: SQSEvent): Future[String] = {
info(s"running relation_embedder lambda, got event: $event")

Future.sequence(event.extractBatches.map(batchProcessor(_))).map(_ => "Done")
Future
.sequence(event.extractBatches.map(batchProcessor(_)))
.map(_ => "Done")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration.{DurationInt, FiniteDuration}

trait LambdaApp[In, Out, Config <: ApplicationConfig]
extends RequestHandler[In, Out]
extends RequestHandler[In, Out]
with LambdaConfigurable[Config]
with Logging {

// 15 minutes is the maximum time allowed for a lambda to run, as of 2024-12-19
private val maximumExecutionTime: FiniteDuration = 15.minutes
protected val maximumExecutionTime: FiniteDuration = 15.minutes

implicit val actorSystem: ActorSystem =
ActorSystem("main-actor-system")
Expand All @@ -23,9 +23,9 @@ trait LambdaApp[In, Out, Config <: ApplicationConfig]
def processEvent(in: In): Future[Out]

override def handleRequest(
event: In,
context: Context
): Out = Await.result(
event: In,
context: Context
): Out = Await.result(
processEvent(event),
maximumExecutionTime
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
configString=knownConfigValue
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package weco.pipeline.relation_embedder.lib

import com.typesafe.config.Config
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers
import weco.fixtures.RandomGenerators
import weco.pipeline.relation_embedder.helpers.ConfigurationTestHelpers

import scala.concurrent.duration.{DurationInt, FiniteDuration}
import scala.concurrent.Future

class LambdaAppTest
extends AnyFunSpec
with ConfigurationTestHelpers
with RandomGenerators
with Matchers {

// This value is from application.conf in test resources
val configString = "knownConfigValue"

case class TestLambdaAppConfiguration(configString: String)
extends ApplicationConfig
class TestLambdaApp
extends LambdaApp[String, String, TestLambdaAppConfiguration] {
override protected val maximumExecutionTime: FiniteDuration = 200.millis

// Config is available in this scope
lazy val configString: String = config.configString

// Function to process an event is required, and should return a Future
override def processEvent(event: String): Future[String] =
Future.successful(event + configString)

// Function to convert typesafe config to application config is required
override def build(rawConfig: Config): TestLambdaAppConfiguration =
TestLambdaAppConfiguration(
configString = rawConfig.getString("configString")
)
}

it(
"creates a lambda app with a config, and allows execution of a processEvent function"
) {
val lambdaApp = new TestLambdaApp()
val eventString = randomAlphanumeric()

lambdaApp.handleRequest(
eventString,
null
) shouldBe eventString + configString
}

class FailingTestLambdaApp extends TestLambdaApp {
override def processEvent(event: String): Future[String] =
Future.failed(new Throwable("Failed"))
}

it("fails if the processEvent function fails") {
val lambdaApp = new FailingTestLambdaApp()
val eventString = randomAlphanumeric()

a[Throwable] shouldBe thrownBy {
lambdaApp.handleRequest(eventString, null)
}
}

class SleepingTestLambdaApp extends TestLambdaApp {
override def processEvent(event: String): Future[String] = Future {
Thread.sleep(500)
event + configString
}
}

it("fails if the processEvent function takes too long") {
val lambdaApp = new SleepingTestLambdaApp()
val eventString = randomAlphanumeric()

a[Throwable] shouldBe thrownBy {
lambdaApp.handleRequest(eventString, null)
}
}
}

0 comments on commit 14a47a4

Please sign in to comment.