summaryrefslogtreecommitdiffstats
path: root/container-di/src/test
diff options
context:
space:
mode:
authorHarald Musum <musum@yahoo-inc.com>2017-05-22 09:11:21 +0200
committerGitHub <noreply@github.com>2017-05-22 09:11:21 +0200
commit88c64ad827d23a3c2cab321712d21e7479892bb8 (patch)
treec52d2f280644e53baec00d8f6058bb71e5e29a0d /container-di/src/test
parentebbe253e649f38c5fe4dd7cfa8f891f6c0c5fdf4 (diff)
Revert "Gjoranv/component construction"
Diffstat (limited to 'container-di/src/test')
-rw-r--r--container-di/src/test/scala/com/yahoo/container/di/ContainerTest.scala116
1 files changed, 19 insertions, 97 deletions
diff --git a/container-di/src/test/scala/com/yahoo/container/di/ContainerTest.scala b/container-di/src/test/scala/com/yahoo/container/di/ContainerTest.scala
index b7c3f94410b..b64de80e39f 100644
--- a/container-di/src/test/scala/com/yahoo/container/di/ContainerTest.scala
+++ b/container-di/src/test/scala/com/yahoo/container/di/ContainerTest.scala
@@ -1,35 +1,30 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.di
-import com.yahoo.container.di.componentgraph.core.ComponentGraphTest.{SimpleComponent, SimpleComponent2}
+import com.yahoo.container.di.componentgraph.core.ComponentGraphTest.{SimpleComponent2, SimpleComponent}
import com.yahoo.container.di.componentgraph.Provider
-import com.yahoo.container.di.componentgraph.core.{ComponentGraph, ComponentNode, Node}
-import org.junit.{After, Before, Ignore, Test}
+import com.yahoo.container.di.componentgraph.core.{Node, ComponentGraph}
+import org.junit.{Test, Before, After}
import org.junit.Assert._
import org.hamcrest.CoreMatchers._
import com.yahoo.config.test.TestConfig
import com.yahoo.component.AbstractComponent
import ContainerTest._
-
import scala.collection.JavaConversions
import com.yahoo.config.di.IntConfig
-
-import scala.concurrent.{Await, Future, future}
+import scala.concurrent.{future, Await}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Try
import com.yahoo.container.di.config.RestApiContext
import com.yahoo.container.bundle.MockBundle
-import com.yahoo.container.di.componentgraph.core.ComponentNode.ComponentConstructorException
-
-import scala.language.postfixOps
/**
* @author tonytv
* @author gjoranv
*/
class ContainerTest {
- var dirConfigSource: DirConfigSource = _
+ var dirConfigSource: DirConfigSource = null
@Before def setup() {
dirConfigSource = new DirConfigSource("ContainerTest-")
@@ -93,8 +88,8 @@ class ContainerTest {
container.reloadConfig(2)
val newGraph = container.runOnce(graph)
- assertThat(ComponentGraph.getNode(newGraph, "id1"), notNullValue(classOf[Node]))
- assertThat(ComponentGraph.getNode(newGraph, "id2"), notNullValue(classOf[Node]))
+ assertThat(ComponentGraph.getNode(newGraph, "id1"), notNullValue(classOf[Node]));
+ assertThat(ComponentGraph.getNode(newGraph, "id2"), notNullValue(classOf[Node]));
container.shutdownConfigurer()
}
@@ -118,22 +113,8 @@ class ContainerTest {
assertTrue(componentToDestruct.deconstructed)
}
- @Ignore // because logAndDie is impossible(?) to verify programmatically
@Test
- def manually_verify_what_happens_when_first_graph_contains_component_that_throws_exception_in_ctor() {
- writeBootstrapConfigs("thrower", classOf[ComponentThrowingExceptionInConstructor])
- val container = newContainer(dirConfigSource)
- var currentGraph: ComponentGraph = null
- try {
- currentGraph = container.runOnce()
- fail("Expected to log and die.")
- } catch {
- case _: Throwable => fail("Expected to log and die")
- }
- }
-
- @Test
- def previous_graph_is_retained_when_new_graph_contains_component_that_throws_exception_in_ctor() {
+ def previous_graph_is_retained_when_new_graph_throws_exception() {
val simpleComponentEntry = ComponentEntry("simpleComponent", classOf[SimpleComponent])
writeBootstrapConfigs(Array(simpleComponentEntry))
@@ -142,76 +123,26 @@ class ContainerTest {
val simpleComponent = currentGraph.getInstance(classOf[SimpleComponent])
- writeBootstrapConfigs("thrower", classOf[ComponentThrowingExceptionInConstructor])
+ writeBootstrapConfigs("thrower", classOf[ComponentThrowingException])
+ dirConfigSource.writeConfig("test", """stringVal "myString" """)
container.reloadConfig(2)
try {
currentGraph = container.runOnce(currentGraph)
fail("Expected exception")
} catch {
- case _: ComponentConstructorException => // Expected, do nothing
- case _: Throwable => fail("Expected ComponentConstructorException")
+ case e: Exception => e.printStackTrace()
}
- assertEquals(1, currentGraph.generation)
- // Also verify that next reconfig is successful
val componentTakingConfigEntry = ComponentEntry("componentTakingConfig", classOf[ComponentTakingConfig])
- dirConfigSource.writeConfig("test", """stringVal "myString" """)
writeBootstrapConfigs(Array(simpleComponentEntry, componentTakingConfigEntry))
container.reloadConfig(3)
currentGraph = container.runOnce(currentGraph)
- assertEquals(3, currentGraph.generation)
assertSame(simpleComponent, currentGraph.getInstance(classOf[SimpleComponent]))
assertNotNull(currentGraph.getInstance(classOf[ComponentTakingConfig]))
}
@Test
- def previous_graph_is_retained_when_new_graph_contains_component_that_times_out_in_ctor() {
- ComponentNode.ComponentConstructTimeout = 1 second
- val simpleComponentEntry = ComponentEntry("simpleComponent", classOf[SimpleComponent])
-
- writeBootstrapConfigs(Array(simpleComponentEntry))
- val container = newContainer(dirConfigSource)
- var currentGraph = container.runOnce()
-
- val simpleComponent = currentGraph.getInstance(classOf[SimpleComponent])
-
- writeBootstrapConfigs("sleeper", classOf[ComponentThatSleepsInConstructor])
- container.reloadConfig(2)
- try {
- currentGraph = container.runOnce(currentGraph)
- fail("Expected exception")
- } catch {
- case e: ComponentConstructorException => assertThat(e.getMessage, containsString("Timed out"))
- case _: Throwable => fail("Expected ComponentConstructorException")
- }
- assertEquals(1, currentGraph.generation)
- }
-
- @Test
- def previous_graph_is_retained_when_new_graph_throws_exception_for_missing_config() {
- val simpleComponentEntry = ComponentEntry("simpleComponent", classOf[SimpleComponent])
-
- writeBootstrapConfigs(Array(simpleComponentEntry))
- val container = newContainer(dirConfigSource)
- var currentGraph = container.runOnce()
-
- val simpleComponent = currentGraph.getInstance(classOf[SimpleComponent])
-
- writeBootstrapConfigs("thrower", classOf[ComponentThrowingExceptionForMissingConfig])
- dirConfigSource.writeConfig("test", """stringVal "myString" """)
- container.reloadConfig(2)
- try {
- currentGraph = container.runOnce(currentGraph)
- fail("Expected exception")
- } catch {
- case _: IllegalArgumentException => // Expected, do nothing
- case _: Throwable => fail("Expected IllegalArgumentException")
- }
- assertEquals(1, currentGraph.generation)
- }
-
- @Test
def runOnce_hangs_waiting_for_valid_config_after_invalid_config() {
dirConfigSource.writeConfig("test", """stringVal "original" """)
writeBootstrapConfigs("myId", classOf[ComponentTakingConfig])
@@ -219,7 +150,7 @@ class ContainerTest {
val container = newContainer(dirConfigSource)
var currentGraph = container.runOnce()
- writeBootstrapConfigs("thrower", classOf[ComponentThrowingExceptionForMissingConfig])
+ writeBootstrapConfigs("thrower", classOf[ComponentThrowingException])
container.reloadConfig(2)
try {
@@ -229,7 +160,7 @@ class ContainerTest {
case e: Exception =>
}
- val newGraph = Future {
+ val newGraph = future {
currentGraph = container.runOnce(currentGraph)
currentGraph
}
@@ -285,7 +216,7 @@ class ContainerTest {
val anotherComponentClass = classOf[SimpleComponent2]
val anotherComponentId = "anotherComponent"
- val componentsConfig: String =
+ val componentsConfig =
ComponentEntry(injectedComponentId, injectedClass).asConfig(0) + "\n" +
ComponentEntry(anotherComponentId, anotherComponentClass).asConfig(1) + "\n" +
ComponentEntry("restApiContext", restApiClass).asConfig(2) + "\n" +
@@ -309,7 +240,7 @@ class ContainerTest {
}
case class ComponentEntry(componentId: String, classId: Class[_]) {
- def asConfig(position: Int): String = {
+ def asConfig(position: Int) = {
<config>
|components[{position}].id "{componentId}"
|components[{position}].classId "{classId.getName}"
@@ -376,7 +307,7 @@ object ContainerTest {
def get() = instance
def deconstruct() {
- require(! instance.deconstructed)
+ require(instance.deconstructed == false)
instance.deconstructed = true
}
}
@@ -385,17 +316,8 @@ object ContainerTest {
require(config != null)
}
- class ComponentThrowingExceptionInConstructor() {
- throw new RuntimeException("This component fails upon construction.")
- }
-
- class ComponentThrowingExceptionForMissingConfig(intConfig: IntConfig) extends AbstractComponent {
- fail("This component should never be created. Only used for tests where 'int' config is missing.")
- }
-
- class ComponentThatSleepsInConstructor() {
- Thread.sleep(3 * ComponentNode.ComponentConstructTimeout.toMillis)
- fail("The timeout mechanism for constructing components is broken.")
+ class ComponentThrowingException(config:IntConfig) extends AbstractComponent {
+ throw new RuntimeException("This component can never be created")
}
class DestructableComponent extends AbstractComponent {
@@ -424,5 +346,5 @@ object ContainerTest {
componentGraph.getInstance(classOf[ComponentTakingConfig])
}
- def convertMap[K, V](map: java.util.Map[K, V]): Map[K, V] = JavaConversions.mapAsScalaMap(map).toMap
+ def convertMap[K, V](map: java.util.Map[K, V]) = JavaConversions.mapAsScalaMap(map).toMap
}