Ensure that logging propagates since it is asynchronous. See #3069
This commit is contained in:
parent
ba957c8356
commit
70d2f2a58f
1 changed files with 40 additions and 6 deletions
|
|
@ -3,11 +3,13 @@
|
|||
*/
|
||||
package akka.event
|
||||
|
||||
import akka.testkit.AkkaSpec
|
||||
import akka.testkit._
|
||||
import scala.concurrent.duration._
|
||||
import com.typesafe.config.{ Config, ConfigFactory }
|
||||
import akka.actor.{ ActorRef, Actor, ActorSystem }
|
||||
import org.scalatest.WordSpec
|
||||
import akka.actor.ActorSystem
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import akka.event.Logging.{ LogEvent, LoggerInitialized, InitializeLogger }
|
||||
|
||||
object LoggerSpec {
|
||||
|
||||
|
|
@ -15,6 +17,7 @@ object LoggerSpec {
|
|||
akka {
|
||||
stdout-loglevel = "WARNING"
|
||||
loglevel = "DEBUG"
|
||||
event-handlers = ["akka.event.LoggerSpec$TestLogger"]
|
||||
}
|
||||
""").withFallback(AkkaSpec.testConf)
|
||||
|
||||
|
|
@ -22,8 +25,26 @@ object LoggerSpec {
|
|||
akka {
|
||||
stdout-loglevel = "OFF"
|
||||
loglevel = "OFF"
|
||||
event-handlers = ["akka.event.LoggerSpec$TestLogger"]
|
||||
}
|
||||
""").withFallback(AkkaSpec.testConf)
|
||||
|
||||
case class SetTarget(ref: ActorRef)
|
||||
|
||||
class TestLogger extends Actor with Logging.StdOutLogger {
|
||||
var target: Option[ActorRef] = None
|
||||
override def receive: Receive = {
|
||||
case InitializeLogger(bus) ⇒
|
||||
bus.subscribe(context.self, classOf[SetTarget])
|
||||
sender ! LoggerInitialized
|
||||
case SetTarget(ref) ⇒
|
||||
target = Some(ref)
|
||||
ref ! ("OK")
|
||||
case event: LogEvent ⇒
|
||||
print(event)
|
||||
target foreach { _ ! event.message }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
|
||||
|
|
@ -31,14 +52,27 @@ class LoggerSpec extends WordSpec with MustMatchers {
|
|||
|
||||
import LoggerSpec._
|
||||
|
||||
private def createSystemAndLogToBuffer(name: String, config: Config) = {
|
||||
private def createSystemAndLogToBuffer(name: String, config: Config, shouldLog: Boolean) = {
|
||||
val out = new java.io.ByteArrayOutputStream()
|
||||
Console.withOut(out) {
|
||||
val system = ActorSystem(name, config)
|
||||
implicit val system = ActorSystem(name, config)
|
||||
try {
|
||||
val probe = TestProbe()
|
||||
system.eventStream.publish(SetTarget(probe.ref))
|
||||
probe.expectMsg("OK")
|
||||
system.log.error("Danger! Danger!")
|
||||
// since logging is asynchronous ensure that it propagates
|
||||
if (shouldLog) {
|
||||
probe.fishForMessage(0.5.seconds.dilated) {
|
||||
case "Danger! Danger!" ⇒ true
|
||||
case _ ⇒ false
|
||||
}
|
||||
} else {
|
||||
probe.expectNoMsg(0.5.seconds.dilated)
|
||||
}
|
||||
} finally {
|
||||
system.shutdown()
|
||||
system.awaitTermination(5.seconds.dilated)
|
||||
}
|
||||
}
|
||||
out
|
||||
|
|
@ -47,7 +81,7 @@ class LoggerSpec extends WordSpec with MustMatchers {
|
|||
"A normally configured actor system" must {
|
||||
|
||||
"log messages to standard output" in {
|
||||
val out = createSystemAndLogToBuffer("defaultLogger", defaultConfig)
|
||||
val out = createSystemAndLogToBuffer("defaultLogger", defaultConfig, true)
|
||||
out.size must be > (0)
|
||||
}
|
||||
}
|
||||
|
|
@ -55,7 +89,7 @@ class LoggerSpec extends WordSpec with MustMatchers {
|
|||
"An actor system configured with the logging turned off" must {
|
||||
|
||||
"not log messages to standard output" in {
|
||||
val out = createSystemAndLogToBuffer("noLogging", noLoggingConfig)
|
||||
val out = createSystemAndLogToBuffer("noLogging", noLoggingConfig, false)
|
||||
out.size must be(0)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue