From a3a64c5c61f010d1d27bdf248c83a5f8e72b0e88 Mon Sep 17 00:00:00 2001 From: Michael Bryzek Date: Sat, 8 Jun 2024 13:36:47 -0400 Subject: [PATCH] Add test case ensuring all processors are accounted for --- api/app/processor/TaskActorCompanion.scala | 38 +++++++++---------- api/app/processor/TaskProcessor.scala | 2 +- .../ProductionTaskProcessorsSpec.scala | 28 ++++++++++++++ 3 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 api/test/processor/ProductionTaskProcessorsSpec.scala diff --git a/api/app/processor/TaskActorCompanion.scala b/api/app/processor/TaskActorCompanion.scala index b35a18e4f..cf6f798d0 100644 --- a/api/app/processor/TaskActorCompanion.scala +++ b/api/app/processor/TaskActorCompanion.scala @@ -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() } } diff --git a/api/app/processor/TaskProcessor.scala b/api/app/processor/TaskProcessor.scala index d866c7bf8..673ebb35b 100644 --- a/api/app/processor/TaskProcessor.scala +++ b/api/app/processor/TaskProcessor.scala @@ -78,7 +78,7 @@ abstract class TaskProcessorWithData[T]( abstract class BaseTaskProcessor( args: TaskProcessorArgs, - typ: TaskType + val typ: TaskType ) { private[this] val Limit = 100 diff --git a/api/test/processor/ProductionTaskProcessorsSpec.scala b/api/test/processor/ProductionTaskProcessorsSpec.scala new file mode 100644 index 000000000..95faa249a --- /dev/null +++ b/api/test/processor/ProductionTaskProcessorsSpec.scala @@ -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(", ")) + } + } +}