如何在Akka流中模拟Sink?

时间:2018-12-03 10:24:31

标签: scala akka akka-stream reactive-streams scala-streams

我有一个使用akka-stream-alpakka multipartUpload的简单“保存”功能,它看起来像这样:

  def save(fileName: String): Future[AWSLocation] = {

    val uuid: String = s"${UUID.randomUUID()}"

    val s3Sink: Sink[ByteString, Future[MultipartUploadResult]] = s3Client.multipartUpload(s"$bucketName", s"$uuid/$fileName")

    val file = Paths.get(s"/tmp/$fileName")

    FileIO.fromPath(file).runWith(s3Sink).map(res => {
      AWSLocation(uuid, fileName, res.key)
    }).recover {
      case ex: S3Exception =>
        logger.error("Upload to S3 failed with s3 exception", ex)
        throw ex
      case ex: Throwable =>
        logger.error("Upload to S3 failed with an unknown exception", ex)
        throw ex
    }
  }

我要测试此功能,有2种情况:

  1. multipartUpload成功,我得到了AWSLocation(我的案例类)。
  2. multipartUpload失败,我得到S3Exception

所以我想监视multipartUpload并返回自己的接收器,如下所示:

  val mockAmazonS3ProxyService: S3ClientProxy = mock[S3ClientProxy]

  val s3serviceMock: S3Service = mock[S3Service]

  override val fakeApplication: Application = GuiceApplicationBuilder()
    .overrides(bind[S3ClientProxy].toInstance(mockAmazonS3ProxyService))
    .router(Router.empty).build()

  "test" in {
    when(mockAmazonS3ProxyService.multipartUpload(anyString(), anyString())) thenReturn Sink(ByteString.empty, Future.successful(MultipartUploadResult(Uri(""),"","myKey123","",Some(""))))

    val res = s3serviceMock.save("someFileName").futureValue

    res.key shouldBe "myKey123"

  }

问题是我得到Error:(47, 93) akka.stream.scaladsl.Sink.type does not take parameters,我知道我无法创建这样的接收器,但是我该怎么办?   还是有什么更好的方法对此进行测试?

1 个答案:

答案 0 :(得分:1)

请考虑重新设计方法save,使其变得更具可测试性,并且可能注入特定的接收器,以针对不同的测试产生不同的结果(如Bennie Krijger所述)。

  def save(fileName: String): Future[AWSLocation] = {
    val uuid: String = s"${UUID.randomUUID()}"
    save(fileName)(() => s3Client.multipartUpload(s"$bucketName", s"$uuid/$fileName"))
  }

  def save(
    fileName: String
  )(createS3UploadSink: () => Sink[ByteString, Future[MultipartUploadResult]]): Future[AWSLocation] = {

    val s3Sink: Sink[ByteString, Future[MultipartUploadResult]] = createS3UploadSink()

    val file = Paths.get(s"/tmp/$fileName")

    FileIO
      .fromPath(file)
      .runWith(s3Sink)
      .map(res => {
        AWSLocation(uuid, fileName, res.key)
      })
      .recover {
        case ex: S3Exception =>
          logger.error("Upload to S3 failed with s3 exception", ex)
          throw ex
        case ex: Throwable =>
          logger.error("Upload to S3 failed with an unknown exception", ex)
          throw ex
      }
  }

测试看起来像

class MultipartUploadSpec extends TestKit(ActorSystem("multipartUpload")) with FunSpecLike {

  implicit val mat: Materializer = ActorMaterializer()

  describe("multipartUpload") {
    it("should pass failure") {
      val result = save(() => Sink.ignore.mapMaterializedValue(_ => Future.failed(new RuntimeException)))
      // assert result
    }

    it("should pass successfully") {
      val result = save(() => Sink.ignore.mapMaterializedValue(_ => Future.successful(new MultipartUploadResult(???))))
      // assert result
    }
  }