|
| 1 | +package lib |
| 2 | + |
| 3 | +import better.files.File |
| 4 | +import cats.data.EitherT |
| 5 | +import com.typesafe.scalalogging.StrictLogging |
| 6 | +import controllers.WsApiController |
| 7 | +import lib.App._ |
| 8 | +import lib.db.Dao |
| 9 | +import lib.server.serverapi.{RemoteFile, UploadResponse} |
| 10 | +import lib.settings.Settings |
| 11 | +import monix.eval.Task |
| 12 | +import monix.execution.Scheduler |
| 13 | +import monix.execution.Scheduler.Implicits.global |
| 14 | +import org.http4s.Uri |
| 15 | +import org.mockito.ArgumentMatchers |
| 16 | +import org.mockito.Mockito._ |
| 17 | +import org.mockito.invocation.InvocationOnMock |
| 18 | +import org.scalatest.concurrent.{Eventually, ScalaFutures} |
| 19 | +import org.scalatest.mockito.MockitoSugar |
| 20 | +import org.scalatest.time.{Seconds, Span} |
| 21 | +import scalikejdbc.{ConnectionPool, DB, _} |
| 22 | +import utils.TestOps.ResultOps |
| 23 | + |
| 24 | +import scala.concurrent.duration._ |
| 25 | + |
| 26 | +class BackupSetExecutorTest extends TestWithDB with MockitoSugar with ScalaFutures with StrictLogging with Eventually { |
| 27 | + |
| 28 | + private implicit val pat: PatienceConfig = PatienceConfig(timeout = Span(5, Seconds)) |
| 29 | + |
| 30 | + private implicit val ss: ServerSession = ServerSession(Uri.unsafeFromString("http://localhost"), "sesssionId", AppVersion(0, 1, 0)) |
| 31 | + private val blockingScheduler = Scheduler.io() |
| 32 | + private val dao = new Dao(blockingScheduler) |
| 33 | + |
| 34 | + test("doesn't execute backup set twice in parallel") { |
| 35 | + val filesHandler: FilesHandler = mock[FilesHandler] |
| 36 | + when(filesHandler.upload(ArgumentMatchers.any())(ArgumentMatchers.any())).thenReturn(EitherT { |
| 37 | + Task { |
| 38 | + Right(List(Some(UploadResponse.Uploaded(mock[RemoteFile])))): Either[AppException, List[Option[UploadResponse]]] |
| 39 | + }.delayResult(1.second) |
| 40 | + }) |
| 41 | + |
| 42 | + val tasksManager: TasksManager = mock[TasksManager] |
| 43 | + when(tasksManager.start(ArgumentMatchers.any())(ArgumentMatchers.any())).thenAnswer((invocation: InvocationOnMock) => { |
| 44 | + invocation.getArgument[Result[Unit]](1) |
| 45 | + }) |
| 46 | + |
| 47 | + val wsApiController: WsApiController = mock[WsApiController] |
| 48 | + when(wsApiController.send(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(pureResult(())) |
| 49 | + |
| 50 | + val bse = new BackupSetsExecutor(dao, filesHandler, tasksManager, wsApiController, blockingScheduler, new Settings(dao)) |
| 51 | + |
| 52 | + assertResult(Right(List.empty))(bse.executeWaitingBackupSets().futureValue) |
| 53 | + |
| 54 | + val bs1 = dao.createBackupSet("ahoj").unwrappedFutureValue |
| 55 | + val bs2 = dao.createBackupSet("ahoj2").unwrappedFutureValue |
| 56 | + |
| 57 | + dao.updateFilesInBackupSet(bs1.id, Seq(File("/tmp/1"), File("/tmp/2"))).unwrappedFutureValue |
| 58 | + dao.updateFilesInBackupSet(bs2.id, Seq(File("/tmp/3"), File("/tmp/4"))).unwrappedFutureValue |
| 59 | + |
| 60 | + dao.markAsProcessing(bs1.id, processing = false).unwrappedFutureValue |
| 61 | + dao.markAsProcessing(bs2.id, processing = false).unwrappedFutureValue |
| 62 | + |
| 63 | + // execute the backup |
| 64 | + val executed = bse.executeWaitingBackupSets() |
| 65 | + |
| 66 | + Thread.sleep(300) // delay which the backup sets need to be started |
| 67 | + |
| 68 | + assertResult(Right(List.empty))(bse.executeWaitingBackupSets().futureValue) |
| 69 | + |
| 70 | + assertResult(Right(List(bs1, bs2).map(_.id)))(executed.futureValue.map(_.map(_.id))) // test this AFTER the previous test |
| 71 | + } |
| 72 | +} |
0 commit comments