|
| 1 | +package com.signalpath.spcron |
| 2 | + |
| 3 | +import cats.effect.{IO, Timer} |
| 4 | +import eu.timepit.fs2cron.awakeEveryCron |
| 5 | +import org.scalamock.scalatest.MockFactory |
| 6 | +import org.scalatest.funspec.AnyFunSpec |
| 7 | +import org.scalatest.matchers.should.Matchers |
| 8 | + |
| 9 | +import scala.concurrent.ExecutionContext |
| 10 | +import scala.concurrent.duration.Duration |
| 11 | + |
| 12 | +class RunnerTest extends AnyFunSpec with MockFactory with Matchers { |
| 13 | + |
| 14 | + describe("getLock") { |
| 15 | + describe("when the key does not exist") { |
| 16 | + it("should set a ttl on key and return true") { |
| 17 | + val mockLock = mock[LockingWorker] |
| 18 | + (mockLock.setNx _).expects("test", *).returning(IO(true)).once() |
| 19 | + (mockLock.expire _).expects("test", 42).returning(IO(true)).once() |
| 20 | + val cron = new Runner(mockLock) |
| 21 | + val actual = cron.getLock("test", 42).unsafeRunSync() |
| 22 | + actual should equal(true) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + describe("when the key already exists") { |
| 27 | + it("should return true when key is owned by runner") { |
| 28 | + val mockLock = mock[LockingWorker] |
| 29 | + val cron = new Runner(mockLock) |
| 30 | + (mockLock.setNx _).expects("test", *).returning(IO(false)).once() |
| 31 | + (mockLock.get _).expects("test").returning(IO(cron.runnerId)).once() |
| 32 | + (mockLock.eval _).expects(*, "test", 42).returning(IO(true)).once() |
| 33 | + val actual = cron.getLock("test", 42).unsafeRunSync() |
| 34 | + actual should equal(true) |
| 35 | + } |
| 36 | + |
| 37 | + it("should return false when key is owned by a different runner") { |
| 38 | + val mockLock = mock[LockingWorker] |
| 39 | + val cron = new Runner(mockLock) |
| 40 | + (mockLock.setNx _).expects("test", *).returning(IO(false)).once() |
| 41 | + (mockLock.get _).expects("test").returning(IO("nope")).once() |
| 42 | + (mockLock.eval _).expects(*, "test", 42).returning(IO(true)).once() |
| 43 | + val actual = cron.getLock("test", 42).unsafeRunSync() |
| 44 | + actual should equal(false) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + describe("worker") { |
| 50 | + implicit val timer: Timer[IO] = IO.timer(ExecutionContext.global) |
| 51 | + describe("when it secures the lock") { |
| 52 | + it("should run the job") { |
| 53 | + var wasRun = false |
| 54 | + val j = IO({ |
| 55 | + wasRun = true |
| 56 | + () |
| 57 | + }) |
| 58 | + val job = JobDefinition("* * * ? * *", "test", j, 1) |
| 59 | + val mockLock = mock[LockingWorker] |
| 60 | + val cron = new Runner(mockLock) |
| 61 | + (mockLock.setNx _).expects(job.name, *).returning(IO(true)).atLeastOnce() |
| 62 | + (mockLock.expire _).expects(job.name, job.ttl).returning(IO(true)).atLeastOnce() |
| 63 | + val runner = new Runner(mockLock) |
| 64 | + val worker = runner.worker(job.job, job.name, job.ttl) |
| 65 | + (awakeEveryCron[IO](job.cron) >> worker).compile.drain.unsafeRunTimed(Duration(1, "second")) |
| 66 | + wasRun should equal(true) |
| 67 | + } |
| 68 | + } |
| 69 | + describe("when it does not secure the lock") { |
| 70 | + it("should not run the job") { |
| 71 | + var wasRun = false |
| 72 | + val j = IO({ |
| 73 | + wasRun = true |
| 74 | + () |
| 75 | + }) |
| 76 | + val job = JobDefinition("* * * ? * *", "test", j, 1) |
| 77 | + val mockLock = mock[LockingWorker] |
| 78 | + val cron = new Runner(mockLock) |
| 79 | + (mockLock.setNx _).expects(job.name, *).returning(IO(false)).atLeastOnce() |
| 80 | + (mockLock.get _).expects(job.name).returning(IO("nope")).atLeastOnce() |
| 81 | + (mockLock.eval _).expects(*, job.name, job.ttl).returning(IO(true)).atLeastOnce() |
| 82 | + val runner = new Runner(mockLock) |
| 83 | + val worker = runner.worker(job.job, job.name, job.ttl) |
| 84 | + (awakeEveryCron[IO](job.cron) >> worker).compile.drain.unsafeRunTimed(Duration(1, "second")) |
| 85 | + wasRun should equal(false) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments