1
+ package kamon
2
+
3
+ import java .io ._
4
+ import java .util .concurrent .TimeUnit
5
+
6
+ import org .scalatest .{Matchers , WordSpec }
7
+ import org .scalatest .concurrent .Eventually
8
+ import org .scalatest .time .SpanSugar ._
9
+
10
+ import scala .util .{Failure , Success , Try }
11
+
12
+ class InitialConfigLoadingSpec extends WordSpec with Matchers with Eventually {
13
+
14
+ " the initial config loading" should {
15
+ " fallback to using reference configuration only when application.conf files are malformed" in {
16
+ val process = Runtime .getRuntime.exec(createProcessWithConfig(" kamon.KamonWithCustomConfig" , " {This is a bad config}" ))
17
+ val processOutputReader = new BufferedReader (new InputStreamReader (process.getInputStream()))
18
+
19
+ eventually(timeout(10 seconds)) {
20
+ val outputLine = processOutputReader.readLine()
21
+ outputLine shouldBe " All Good"
22
+ }
23
+
24
+ if (process.isAlive) {
25
+ process.destroyForcibly().waitFor(5 , TimeUnit .SECONDS )
26
+ }
27
+ }
28
+ }
29
+
30
+ def createProcessWithConfig (mainClass : String , configContent : String ): String = {
31
+ val tempConfigFile = File .createTempFile(" bad-config" , " .conf" )
32
+ val writer = new BufferedWriter (new FileWriter (tempConfigFile))
33
+ writer.write(configContent)
34
+ writer.flush()
35
+ writer.close()
36
+
37
+ val configOptions = " -Dconfig.trace=loads -Dconfig.file=" + tempConfigFile.getAbsolutePath()
38
+ System .getProperty(" java.home" ) + File .separator + " bin" + File .separator + " java " + configOptions +
39
+ " -cp " + System .getProperty(" java.class.path" ) + " " + mainClass
40
+ }
41
+ }
42
+
43
+ object KamonWithCustomConfig extends App {
44
+ Try {
45
+ Kamon .counter(" test" ).increment()
46
+ } match {
47
+ case Success (_) => println(" All Good" )
48
+ case Failure (_) => println(" All Bad" )
49
+ }
50
+
51
+ }
0 commit comments