alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (RemoteInitErrorSpec.scala)

This example Akka source code file (RemoteInitErrorSpec.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to akka.io; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

actorsystem, akka, collection, concurrent, flatspec, ip, matchers, nonfatal, remote, remoteactorrefprovider, remoteiniterrorspec, remoting, test, testing, testkit, thread, time

The RemoteInitErrorSpec.scala Akka example source code

/**
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */
package akka.remote

import akka.actor.ActorSystem
import akka.testkit.AkkaSpec
import akka.testkit.DefaultTimeout
import com.typesafe.config.ConfigFactory
import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalatest.WordSpecLike
import org.scalatest.concurrent.Eventually._
import scala.annotation.tailrec
import scala.collection.JavaConversions._
import scala.collection.mutable.Set
import scala.concurrent.duration._
import scala.language.postfixOps
import scala.util.control.NonFatal

/**
 * The 192.0.2.1 is a Documentation IP-address and should not be used at all
 * by any network node. Therefore we assume here that the initialization of
 * the ActorSystem with the use of remoting will intentionally fail.
 */
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
class RemoteInitErrorSpec extends FlatSpec with Matchers {
  val conf = ConfigFactory.parseString(
    """
      akka {
        actor {
          provider = "akka.remote.RemoteActorRefProvider"
        }
        remote {
          enabled-transports = ["akka.remote.netty.tcp"]
            netty.tcp {
              hostname = "192.0.2.1"
              port = 12344
            }
        }
      }
    """).resolve()

  def currentThreadIds() = {
    val threads = Thread.getAllStackTraces().keySet()
    threads.collect({ case t: Thread if (!t.isDaemon()) ⇒ t.getId() })
  }

  "Remoting" must "shut down properly on RemoteActorRefProvider initialization failure" in {
    val start = currentThreadIds()
    try {
      ActorSystem("duplicate", ConfigFactory.parseString("akka.loglevel=OFF").withFallback(conf))
      fail("initialization should fail due to invalid IP address")
    } catch {
      case NonFatal(e) ⇒ {
        eventually(timeout(30 seconds), interval(800 milliseconds)) {
          val current = currentThreadIds()
          // no new threads should remain compared to the start state
          (current -- start) should be(empty)
        }
      }
    }
  }
}

Other Akka source code examples

Here is a short list of links related to this Akka RemoteInitErrorSpec.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.