Skip to content

Commit

Permalink
Add test case ensuring all processors are accounted for
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Jun 8, 2024
1 parent b0f3bb0 commit a3a64c5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
38 changes: 19 additions & 19 deletions api/app/processor/TaskActorCompanion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ class TaskActorCompanion @Inject() (
purgeOldDeleted: PurgeOldDeletedProcessor,
checkInvariants: CheckInvariantsProcessor,
) {

def process(typ: TaskType): Unit = {
lookup(typ).process()
private[processor] val all: Map[TaskType, BaseTaskProcessor] = {
import TaskType._
Map(
CheckInvariants -> checkInvariants,
IndexApplication -> indexApplication,
CleanupDeletions -> cleanupDeletions,
DiffVersion -> diffVersion,
MigrateVersion -> migrateVersion,
ScheduleMigrateVersions -> scheduleMigrateVersions,
UserCreated -> userCreated,
ScheduleSyncGeneratorServices -> scheduleSyncGeneratorServices,
SyncGeneratorService -> syncGeneratorService,
Email -> email,
PurgeOldDeleted -> purgeOldDeleted,
)
}

private[this] def lookup(typ: TaskType): BaseTaskProcessor = {
import TaskType._
typ match {
case CheckInvariants => checkInvariants
case IndexApplication => indexApplication
case CleanupDeletions => cleanupDeletions
case DiffVersion => diffVersion
case MigrateVersion => migrateVersion
case ScheduleMigrateVersions => scheduleMigrateVersions
case UserCreated => userCreated
case ScheduleSyncGeneratorServices => scheduleSyncGeneratorServices
case SyncGeneratorService => syncGeneratorService
case Email => email
case PurgeOldDeleted => purgeOldDeleted
case UNDEFINED(_) => sys.error(s"Undefined task type '$typ")
}
def process(typ: TaskType): Unit = {
all.getOrElse(typ, {
sys.error(s"Failed to find processor for task type '$typ'")
}).process()
}
}
2 changes: 1 addition & 1 deletion api/app/processor/TaskProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ abstract class TaskProcessorWithData[T](

abstract class BaseTaskProcessor(
args: TaskProcessorArgs,
typ: TaskType
val typ: TaskType
) {

private[this] val Limit = 100
Expand Down
28 changes: 28 additions & 0 deletions api/test/processor/ProductionTaskProcessorsSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package processor

import io.apibuilder.task.v0.models.TaskType
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.guice.GuiceOneAppPerSuite

final class ProductionTaskProcessorsSpec extends PlaySpec with GuiceOneAppPerSuite {

private[this] def companion = app.injector.instanceOf[TaskActorCompanion]

"each task type is assigned a processor" in {
val missing = TaskType.all.filterNot(companion.all.contains)
if (missing.nonEmpty) {
sys.error(
s"TaskActorCompanion: Missing processor for task type(s): ${missing.map(_.toString).mkString(", ")}",
)
}
}

"each task type is assigned to at most one processor" in {
val dups = companion.all.values.toSeq.groupBy(_.typ).filter { case (_, v) => v.length > 1 }.map { case (t, all) =>
s"Task type $t is assigned to more than 1 processor: " + all.map(_.getClass.getName).mkString(", ")
}
if (dups.nonEmpty) {
sys.error(dups.mkString(", "))
}
}
}

0 comments on commit a3a64c5

Please sign in to comment.