Search code examples
scalaakkascalatestreactive-streamsscalikejdbc

Test ScalikeJDBC Publisher with Scalatest


Do you have any idea why this code it should "add person s and then find-by-firstname" in { implicit session: DBSession => val person1: Person = Person.create(firstname1, lastname1, ssn1, email1, dob1).get val person3: Person = Person.create(firstname1, lastname3, ssn3, email3, dob3).get

val publisher1: DatabasePublisher[Person] = Person.findByFirstName(firstname1)

val probe1 = TestSink.probe[Person](actorSystem)
val (_, sink) = Source
  .fromPublisher(publisher1)
  .toMat(probe1)(Keep.both)
  .run()

sink.request(2)
sink.expectNext(person1, person3)
sink.expectComplete()

}

would give this error

2019-09-26 16:28:13,846 INFO  s.s.DatabasePublisher@[kka.actor.default-dispatcher-6] - Database stream requested by subscriber: akka.stream.impl.fusing.ActorGraphInterpreter$BatchingActorInputBoundary$$anon$1@17b79df0 is ready
2019-09-26 16:28:13,855 INFO  s.s.DatabaseSubscription@[la-execution-context-global-24] - All data for subscriber: akka.stream.impl.fusing.ActorGraphInterpreter$BatchingActorInputBoundary$$anon$1@17b79df0 has been sent
2019-09-26 16:28:13,857 INFO  s.s.DatabaseSubscription@[la-execution-context-global-24] - Finished cleaning up database resources occupied for subscriber: akka.stream.impl.fusing.ActorGraphInterpreter$BatchingActorInputBoundary$$anon$1@17b79df0
[ERROR] Tests run: 11, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.683 s <<< FAILURE! - in com.bah.devops.common.entities.PersonTest
[ERROR] Person should add person s and then find-by-firstname(com.bah.devops.common.entities.PersonTest)  Time elapsed: 0.144 s  <<< FAILURE!
    java.lang.AssertionError: assertion failed: expected OnNext(Person(14,FN1,LN1,000-00-0001,[email protected],2019-01-02)), found OnComplete
        at com.bah.devops.common.entities.PersonTest.$anonfun$new$22(PersonTest.scala:418)
        at com.bah.devops.common.entities.PersonTest.$anonfun$new$22$adapted(PersonTest.scala:284)
        at com.bah.devops.common.entities.PersonTest.using(PersonTest.scala:27)
        at com.bah.devops.common.entities.PersonTest.withFixture(PersonTest.scala:27)

? I don’t know why the two onNext calls seem to be missed.


Solution

  • This will do it.

    val person1: Person = Person.create(firstname1, lastname1, ssn1, email1, dob1).get val person3: Person = Person.create(firstname1, lastname3, ssn3, email3, dob3).get

    val publisher1: DatabasePublisher[Person] = Person.findByFirstName(firstname1)
    
    val results1Future: Future[Seq[Person]] =
      Source.fromPublisher(publisher1).take(2).runWith(Sink.seq)
    
    val results1: Seq[Person] = Await.result(results1Future, Duration(5, TimeUnit.SECONDS))
    
    results1 should contain(person1)
    results1 should contain(person3)