Writing tests for publishing events

145 views
Skip to first unread message

Henrik Larsson

unread,
Oct 16, 2017, 12:25:04 PM10/16/17
to Lagom Framework Users
Im trying to get a simple test working from the description on the Lagom docs. However a the code is not compiling. How should I read this error and what do i need to fix it?

object DataCollectorService {
val TOPIC_NAME = "tweets"
}

trait DataCollectorService extends Service {

override def descriptor: Descriptor = {
import Service._
named("tweet").withCalls(
call(getTweet)
).withTopics(
topic(DataCollectorService.TOPIC_NAME, tweetsTopic)
).withAutoAcl(true)
}

// REST handlers
def getTweet: ServiceCall[String, String]

// Topic handler
def tweetsTopic(): Topic[TweetMessage]
}

case class TweetMessage(message: String)

object TweetMessage {
implicit val format: Format[TweetMessage] = Json.format[TweetMessage]
}



"The PublishService" should {
"publish events on the topic" in ServiceTest.withServer(ServiceTest.defaultSetup) { ctx =>
new DataCollectorApplication(ctx) with LocalServiceLocator
with TestTopicComponents
} { server =>

implicit val system = server.actorSystem
implicit val mat = server.materializer

val client: DataCollectorService = server.serviceClient.implement[DataCollectorService]
val source = client.tweetsTopic().subscribe.atMostOnceSource
source.runWith(TestSink.probe[TweetMessage])
.request(1)
.expectNext should ===(TweetMessage("msg 1"))

}
}


Error:(16, 11) <$anon: org.mlpipeline.impl.DataCollectorApplication with com.lightbend.lagom.scaladsl.server.LocalServiceLocator with com.lightbend.lagom.scaladsl.testkit.TestTopicComponents> inherits conflicting members:
  lazy value topicFactory in trait LagomKafkaClientComponents of type com.lightbend.lagom.internal.scaladsl.api.broker.TopicFactory  and
  lazy value topicFactory in trait TestTopicComponents of type com.lightbend.lagom.internal.scaladsl.api.broker.TopicFactory
(Note: this can be resolved by declaring an override in <$anon: org.mlpipeline.impl.DataCollectorApplication with com.lightbend.lagom.scaladsl.server.LocalServiceLocator with com.lightbend.lagom.scaladsl.testkit.TestTopicComponents>.)
      new DataCollectorApplication(ctx) with LocalServiceLocator


Joo Lee

unread,
Oct 31, 2017, 6:38:27 AM10/31/17
to Lagom Framework Users
Without testing the codes, I am guessing that you are having "the diamond problem" which means that you are "mixing" the multiple mixins with same method signature or variable. Which means you have to explicitly tell the compiler whose "topicFactory" you intend to use.

I have a feeling that you might want to use the TestTopicComponent's "topic factory", and I think you can do something like this in your test code.

override val topicFactory = super[TestTopicComponents].topicFactory

Tim Moore

unread,
Nov 2, 2017, 4:52:32 AM11/2/17
to Henrik Larsson, Lagom Framework Users
This happens because you are using DataCollectorApplication to set up the test, which already mixes in LagomKafkaClientComponents, and then also telling it to mix in TestTopicComponents, which has a conflicting definition of topicFactory.

Other than Joo's suggestion, which is one possible solution, you can look at how this is done in Online Auction Scala, which avoids using the full "Application" definition and assembles the right pieces together right in the test:

  private val server = ServiceTest.startServer(ServiceTest.defaultSetup.withCassandra(true)) { ctx =>
    new LagomApplication(ctx) with ItemComponents with LocalServiceLocator with AhcWSComponents with TestTopicComponents {
      override def additionalConfiguration: AdditionalConfiguration =
        super.additionalConfiguration ++ Configuration.from(Map(
          "cassandra-query-journal.eventual-consistency-delay" -> "0"
        ))
    }
  }



ItemComponents contains all of the definitions used by the service itself, making it easy to mix in to the test:


Hope this helps.

Best,
Tim

--
You received this message because you are subscribed to the Google Groups "Lagom Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/80907611-961b-41a9-912b-e76b6053d385%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Tim Moore
Senior Engineer, Lagom, Lightbend, Inc.

Reply all
Reply to author
Forward
0 new messages