Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added startFuture and stopFuture methods to ScalaVerticle and improve… #24

Merged
merged 5 commits into from
Feb 17, 2017

Conversation

codepitbull
Copy link
Contributor

…d docs

Copy link
Contributor

@Narigo Narigo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handling of startFuture and stopFuture could be optimized, see comments.

@@ -51,14 +52,27 @@ abstract class ScalaVerticle {
}

/**
* Start the verticle.
*/
def start(): Unit = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be noted that if the user overrides both this method and the startFuture method, this here will only be called if the overridden method startFuture calls start. Is this a really good idea?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the same behavior as in AbstractVerticle in the Vertx-core.

case Failure(t) => promise.failure(t)
}

promise.future
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is fSeq not returned directly? You shouldn't need to wrap a promise around it?

This should be enough:

override def startFuture(): Future[Void] = Future.sequence(Seq(
  vertx.eventBus().consumer[String]("asd").handler(a => println(a)).completionFuture(),
  vertx.eventBus().consumer[String]("asd2").handler(a => println(a)).completionFuture()
))

OTOH, why Future[Void] as return value of startFuture? It's Future[Unit] if I read the other changes correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. The Promise is a left over from what was there befor.
I removed it and also changed the signature.

@@ -16,10 +16,10 @@ class ScalaVerticleTest extends FlatSpec with Matchers {
val cl = new CountDownLatch(1)
val vertx = Vertx.vertx
implicit val exec = VertxExecutionContext(vertx.getOrCreateContext())
vertx.deployVerticleFuture(s"scala:${classOf[TestVerticle].getName}").foreach(r => cl.countDown())
vertx.deployVerticleFuture(nameForVerticle[TestVerticle]).onComplete(t => cl.countDown())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nameForVerticle[XXX] method could actually have its own test :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a Test :)

@@ -10,7 +10,7 @@ import scala.util.{Failure, Success}
*/
class TestVerticle extends ScalaVerticle{

override def start(): Future[Unit] = {
override def startFuture(): Future[Unit] = {
Future.successful(())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm... This line does nothing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the whole test, including the comments below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the whole thing.

* Stop the verticle.<p>
* This is called by Vert.x when the verticle instance is un-deployed. Don't call it yourself.<p>
* If your verticle does things in it's shut-down which take some time then you can override this method
* and complete the futrue some time later when clean-up is complete.
*
* @return a future which should be completed when verticle clean-up is complete.
*/
def stop(): concurrent.Future[Unit] = {
def stopFuture(): concurrent.Future[Unit] = {
stop()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If stop() throws, this won't return you a failed Future, but a thrown exception. It would be nicer to do this:

def stopFuture(): concurrent.Future[Unit] = Future.fromTry(util.Try(stop()))

If you want to let the ExecutionContext decide when to run it, you could do this:

def stopFuture(): concurrent.Future[Unit] = Future(stop())

This should also capture a thrown exception into a failed Future. Same applies to startFuture().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhh, good catch! I picked the 2nd option.

@@ -24,7 +24,7 @@ class VertxExecutionContextTest extends FlatSpec with Matchers {

class PromiseTestVerticle extends ScalaVerticle {

override def start(): Future[Unit] = {
override def startFuture(): Future[Unit] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to wrap a promise around the future. Is that what you want to test or just the execution context? Then it should suffice to have res.map(_ => ()) at the end and remove all lines with promise in this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@Narigo Narigo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is a test for overriding start (and stop) with a success and failure case, just to check if the usual startFuture and stopFuture end in a success or failure respectively. Do you have a test for stopFuture? If not, might make sense to add something like you did for startFuture as well.

@Narigo
Copy link
Contributor

Narigo commented Feb 17, 2017

Looks good to me! 👍

@Narigo Narigo merged commit ba43f79 into master Feb 17, 2017
@Narigo Narigo removed the to review label Feb 17, 2017
@codepitbull codepitbull deleted the SCALA_VERTICLE_IMRPOVEMENT branch February 20, 2017 12:37
willisjtc added a commit to willisjtc/vertx-lang-scala that referenced this pull request Jun 19, 2021
Added startFuture and stopFuture methods to ScalaVerticle and improve…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants