From 5dc71fc35086aad59f1a01d24405670020cbc77f Mon Sep 17 00:00:00 2001 From: josecoll Date: Thu, 26 Apr 2018 13:40:37 +0100 Subject: [PATCH] =?UTF-8?q?Behave:=20use=20existing=20Core=20common=20util?= =?UTF-8?q?ity=20functions=20for=20Logger=20creation=20and=20Ti=E2=80=A6?= =?UTF-8?q?=20(#2990)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use existing Core common utility functions for Logger creation and Time durations. * Updated JUnit tests. --- .../net/corda/core/utilities/KotlinUtils.kt | 1 - .../main/kotlin/net/corda/behave/Utilities.kt | 20 ------------------- .../net/corda/behave/logging/LogUtilities.kt | 9 --------- .../net/corda/behave/monitoring/Watch.kt | 2 +- .../net/corda/behave/network/Network.kt | 6 +++--- .../net/corda/behave/node/Distribution.kt | 5 ++--- .../main/kotlin/net/corda/behave/node/Node.kt | 6 +++--- .../node/configuration/Configuration.kt | 4 ++-- .../net/corda/behave/process/Command.kt | 11 ++++++---- .../net/corda/behave/service/Service.kt | 4 ++-- .../corda/behave/service/ServiceSettings.kt | 11 +++++----- .../kotlin/net/corda/behave/ssh/SSHClient.kt | 4 ++-- .../behave/monitoring/MonitoringTests.kt | 14 ++++++------- .../net/corda/behave/network/NetworkTests.kt | 2 +- 14 files changed, 35 insertions(+), 64 deletions(-) delete mode 100644 experimental/behave/src/main/kotlin/net/corda/behave/logging/LogUtilities.kt diff --git a/core/src/main/kotlin/net/corda/core/utilities/KotlinUtils.kt b/core/src/main/kotlin/net/corda/core/utilities/KotlinUtils.kt index 675745f759..4e6f62b31d 100644 --- a/core/src/main/kotlin/net/corda/core/utilities/KotlinUtils.kt +++ b/core/src/main/kotlin/net/corda/core/utilities/KotlinUtils.kt @@ -5,7 +5,6 @@ import net.corda.core.internal.uncheckedCast import net.corda.core.serialization.CordaSerializable import org.slf4j.Logger import org.slf4j.LoggerFactory -import rx.Observable import java.time.Duration import java.util.concurrent.ExecutionException import java.util.concurrent.Future diff --git a/experimental/behave/src/main/kotlin/net/corda/behave/Utilities.kt b/experimental/behave/src/main/kotlin/net/corda/behave/Utilities.kt index 0b4951e0cc..18b11273e6 100644 --- a/experimental/behave/src/main/kotlin/net/corda/behave/Utilities.kt +++ b/experimental/behave/src/main/kotlin/net/corda/behave/Utilities.kt @@ -3,26 +3,6 @@ package net.corda.behave import java.time.Duration import java.util.concurrent.CountDownLatch -// TODO Most of these are available in corda core - -val Int.millisecond: Duration - get() = Duration.ofMillis(this.toLong()) - -val Int.milliseconds: Duration - get() = Duration.ofMillis(this.toLong()) - -val Int.second: Duration - get() = Duration.ofSeconds(this.toLong()) - -val Int.seconds: Duration - get() = Duration.ofSeconds(this.toLong()) - -val Int.minute: Duration - get() = Duration.ofMinutes(this.toLong()) - -val Int.minutes: Duration - get() = Duration.ofMinutes(this.toLong()) - fun CountDownLatch.await(duration: Duration) = this.await(duration.toMillis(), java.util.concurrent.TimeUnit.MILLISECONDS) diff --git a/experimental/behave/src/main/kotlin/net/corda/behave/logging/LogUtilities.kt b/experimental/behave/src/main/kotlin/net/corda/behave/logging/LogUtilities.kt deleted file mode 100644 index 16491e6c87..0000000000 --- a/experimental/behave/src/main/kotlin/net/corda/behave/logging/LogUtilities.kt +++ /dev/null @@ -1,9 +0,0 @@ -package net.corda.behave.logging - -import org.slf4j.Logger -import org.slf4j.LoggerFactory - -// TODO Already available in corda core - -inline fun getLogger(): Logger = - LoggerFactory.getLogger(T::class.java) diff --git a/experimental/behave/src/main/kotlin/net/corda/behave/monitoring/Watch.kt b/experimental/behave/src/main/kotlin/net/corda/behave/monitoring/Watch.kt index ee835ac425..ae3b14dba4 100644 --- a/experimental/behave/src/main/kotlin/net/corda/behave/monitoring/Watch.kt +++ b/experimental/behave/src/main/kotlin/net/corda/behave/monitoring/Watch.kt @@ -1,7 +1,7 @@ package net.corda.behave.monitoring import net.corda.behave.await -import net.corda.behave.seconds +import net.corda.core.utilities.seconds import rx.Observable import java.time.Duration import java.util.concurrent.CountDownLatch diff --git a/experimental/behave/src/main/kotlin/net/corda/behave/network/Network.kt b/experimental/behave/src/main/kotlin/net/corda/behave/network/Network.kt index 2e52f9fe1c..c62ae493c9 100644 --- a/experimental/behave/src/main/kotlin/net/corda/behave/network/Network.kt +++ b/experimental/behave/src/main/kotlin/net/corda/behave/network/Network.kt @@ -4,14 +4,14 @@ import net.corda.behave.database.DatabaseType import net.corda.behave.file.LogSource import net.corda.behave.file.currentDirectory import net.corda.behave.file.stagingRoot -import net.corda.behave.logging.getLogger -import net.corda.behave.minutes import net.corda.behave.node.Distribution import net.corda.behave.node.Node import net.corda.behave.node.configuration.NotaryType import net.corda.behave.process.JarCommand import net.corda.core.CordaException import net.corda.core.internal.* +import net.corda.core.utilities.contextLogger +import net.corda.core.utilities.minutes import org.apache.commons.io.FileUtils import java.io.Closeable import java.nio.file.Path @@ -285,7 +285,7 @@ class Network private constructor( } companion object { - val log = getLogger() + val log = contextLogger() const val CLEANUP_ON_ERROR = false fun new(timeout: Duration = 2.minutes diff --git a/experimental/behave/src/main/kotlin/net/corda/behave/node/Distribution.kt b/experimental/behave/src/main/kotlin/net/corda/behave/node/Distribution.kt index cbc0d6518f..9035eab03c 100644 --- a/experimental/behave/src/main/kotlin/net/corda/behave/node/Distribution.kt +++ b/experimental/behave/src/main/kotlin/net/corda/behave/node/Distribution.kt @@ -1,12 +1,11 @@ package net.corda.behave.node import net.corda.behave.file.stagingRoot -import net.corda.behave.logging.getLogger -import net.corda.behave.service.Service import net.corda.core.internal.copyTo import net.corda.core.internal.createDirectories import net.corda.core.internal.div import net.corda.core.internal.exists +import net.corda.core.utilities.contextLogger import java.net.URL import java.nio.file.Path @@ -82,7 +81,7 @@ class Distribution private constructor( companion object { - protected val log = getLogger() + private val log = contextLogger() private val distributions = mutableListOf() diff --git a/experimental/behave/src/main/kotlin/net/corda/behave/node/Node.kt b/experimental/behave/src/main/kotlin/net/corda/behave/node/Node.kt index 67eddc6543..6f8351dbca 100644 --- a/experimental/behave/src/main/kotlin/net/corda/behave/node/Node.kt +++ b/experimental/behave/src/main/kotlin/net/corda/behave/node/Node.kt @@ -5,11 +5,9 @@ import net.corda.behave.database.DatabaseType import net.corda.behave.file.LogSource import net.corda.behave.file.currentDirectory import net.corda.behave.file.stagingRoot -import net.corda.behave.logging.getLogger import net.corda.behave.monitoring.PatternWatch import net.corda.behave.node.configuration.* import net.corda.behave.process.JarCommand -import net.corda.behave.seconds import net.corda.behave.service.Service import net.corda.behave.service.ServiceSettings import net.corda.behave.ssh.MonitoringSSHClient @@ -20,6 +18,8 @@ import net.corda.core.internal.div import net.corda.core.internal.exists import net.corda.core.messaging.CordaRPCOps import net.corda.core.utilities.NetworkHostAndPort +import net.corda.core.utilities.loggerFor +import net.corda.core.utilities.seconds import org.apache.commons.io.FileUtils import java.net.InetAddress import java.nio.file.Path @@ -35,7 +35,7 @@ class Node( private val settings: ServiceSettings = ServiceSettings() ) { - private val log = getLogger() + private val log = loggerFor() private val runtimeDirectory = rootDirectory / config.name diff --git a/experimental/behave/src/main/kotlin/net/corda/behave/node/configuration/Configuration.kt b/experimental/behave/src/main/kotlin/net/corda/behave/node/configuration/Configuration.kt index e28d9c5888..6ebe3613f4 100644 --- a/experimental/behave/src/main/kotlin/net/corda/behave/node/configuration/Configuration.kt +++ b/experimental/behave/src/main/kotlin/net/corda/behave/node/configuration/Configuration.kt @@ -1,10 +1,10 @@ package net.corda.behave.node.configuration import net.corda.behave.database.DatabaseType -import net.corda.behave.logging.getLogger import net.corda.behave.node.Distribution import net.corda.core.identity.CordaX500Name import net.corda.core.internal.writeText +import net.corda.core.utilities.contextLogger import java.nio.file.Path class Configuration( @@ -53,7 +53,7 @@ class Configuration( .joinToString("\n") companion object { - private val log = getLogger() + private val log = contextLogger() const val DEFAULT_PASSWORD = "S0meS3cretW0rd" } diff --git a/experimental/behave/src/main/kotlin/net/corda/behave/process/Command.kt b/experimental/behave/src/main/kotlin/net/corda/behave/process/Command.kt index 0860cfdb7b..2b5d41b2b8 100644 --- a/experimental/behave/src/main/kotlin/net/corda/behave/process/Command.kt +++ b/experimental/behave/src/main/kotlin/net/corda/behave/process/Command.kt @@ -1,9 +1,12 @@ package net.corda.behave.process -import net.corda.behave.* +import net.corda.behave.await import net.corda.behave.file.currentDirectory -import net.corda.behave.logging.getLogger import net.corda.behave.process.output.OutputListener +import net.corda.behave.waitFor +import net.corda.core.utilities.loggerFor +import net.corda.core.utilities.minutes +import net.corda.core.utilities.seconds import rx.Observable import rx.Subscriber import java.io.Closeable @@ -18,7 +21,7 @@ open class Command( private val timeout: Duration = 2.minutes ): Closeable { - protected val log = getLogger() + protected val log = loggerFor() private val terminationLatch = CountDownLatch(1) @@ -74,7 +77,7 @@ open class Command( }).start() val streamIsClosed = outputCapturedLatch.await(timeout) val timeout = if (!streamIsClosed || isInterrupted) { - 1.second + 1.seconds } else { timeout } diff --git a/experimental/behave/src/main/kotlin/net/corda/behave/service/Service.kt b/experimental/behave/src/main/kotlin/net/corda/behave/service/Service.kt index 64c7e869f8..cc42a60511 100644 --- a/experimental/behave/src/main/kotlin/net/corda/behave/service/Service.kt +++ b/experimental/behave/src/main/kotlin/net/corda/behave/service/Service.kt @@ -1,6 +1,6 @@ package net.corda.behave.service -import net.corda.behave.logging.getLogger +import net.corda.core.utilities.loggerFor import java.io.Closeable abstract class Service( @@ -11,7 +11,7 @@ abstract class Service( private var isRunning: Boolean = false - protected val log = getLogger() + protected val log = loggerFor() fun start(): Boolean { if (isRunning) { diff --git a/experimental/behave/src/main/kotlin/net/corda/behave/service/ServiceSettings.kt b/experimental/behave/src/main/kotlin/net/corda/behave/service/ServiceSettings.kt index f94523691e..e8a34a790b 100644 --- a/experimental/behave/src/main/kotlin/net/corda/behave/service/ServiceSettings.kt +++ b/experimental/behave/src/main/kotlin/net/corda/behave/service/ServiceSettings.kt @@ -1,13 +1,12 @@ package net.corda.behave.service -import net.corda.behave.minute -import net.corda.behave.second -import net.corda.behave.seconds +import net.corda.core.utilities.minutes +import net.corda.core.utilities.seconds import java.time.Duration data class ServiceSettings( - val timeout: Duration = 1.minute, - val startupDelay: Duration = 1.second, + val timeout: Duration = 1.minutes, + val startupDelay: Duration = 1.seconds, val startupTimeout: Duration = 15.seconds, - val pollInterval: Duration = 1.second + val pollInterval: Duration = 1.seconds ) diff --git a/experimental/behave/src/main/kotlin/net/corda/behave/ssh/SSHClient.kt b/experimental/behave/src/main/kotlin/net/corda/behave/ssh/SSHClient.kt index afa530cf3c..1ff800408a 100644 --- a/experimental/behave/src/main/kotlin/net/corda/behave/ssh/SSHClient.kt +++ b/experimental/behave/src/main/kotlin/net/corda/behave/ssh/SSHClient.kt @@ -1,6 +1,6 @@ package net.corda.behave.ssh -import net.corda.behave.logging.getLogger +import net.corda.core.utilities.contextLogger import org.apache.sshd.client.SshClient import org.apache.sshd.client.channel.ChannelShell import org.apache.sshd.client.session.ClientSession @@ -96,7 +96,7 @@ open class SSHClient private constructor( companion object { - private val log = getLogger() + private val log = contextLogger() fun connect( port: Int, diff --git a/experimental/behave/src/test/kotlin/net/corda/behave/monitoring/MonitoringTests.kt b/experimental/behave/src/test/kotlin/net/corda/behave/monitoring/MonitoringTests.kt index 5bf5861a13..eb0809cccc 100644 --- a/experimental/behave/src/test/kotlin/net/corda/behave/monitoring/MonitoringTests.kt +++ b/experimental/behave/src/test/kotlin/net/corda/behave/monitoring/MonitoringTests.kt @@ -1,6 +1,6 @@ package net.corda.behave.monitoring -import net.corda.behave.second +import net.corda.core.utilities.seconds import org.assertj.core.api.Assertions.assertThat import org.junit.Test import rx.Observable @@ -10,14 +10,14 @@ class MonitoringTests { @Test fun `watch gets triggered when pattern is observed`() { val observable = Observable.just("first", "second", "third") - val result = PatternWatch(observable, "c.n").await(1.second) + val result = PatternWatch(observable, "c.n").await(1.seconds) assertThat(result).isTrue() } @Test fun `watch does not get triggered when pattern is not observed`() { val observable = Observable.just("first", "second", "third") - val result = PatternWatch(observable, "forth").await(1.second) + val result = PatternWatch(observable, "forth").await(1.seconds) assertThat(result).isFalse() } @@ -28,7 +28,7 @@ class MonitoringTests { val watch2 = PatternWatch(observable, "ond") val watch3 = PatternWatch(observable, "ird") val aggregate = watch1 * watch2 * watch3 - assertThat(aggregate.await(1.second)).isTrue() + assertThat(aggregate.await(1.seconds)).isTrue() } @Test @@ -38,7 +38,7 @@ class MonitoringTests { val watch2 = PatternWatch(observable, "ond") val watch3 = PatternWatch(observable, "baz") val aggregate = watch1 * watch2 * watch3 - assertThat(aggregate.await(1.second)).isFalse() + assertThat(aggregate.await(1.seconds)).isFalse() } @Test @@ -48,7 +48,7 @@ class MonitoringTests { val watch2 = PatternWatch(observable, "ond") val watch3 = PatternWatch(observable, "bar") val aggregate = watch1 / watch2 / watch3 - assertThat(aggregate.await(1.second)).isTrue() + assertThat(aggregate.await(1.seconds)).isTrue() } @Test @@ -58,7 +58,7 @@ class MonitoringTests { val watch2 = PatternWatch(observable, "baz") val watch3 = PatternWatch(observable, "bar") val aggregate = watch1 / watch2 / watch3 - assertThat(aggregate.await(1.second)).isFalse() + assertThat(aggregate.await(1.seconds)).isFalse() } } \ No newline at end of file diff --git a/experimental/behave/src/test/kotlin/net/corda/behave/network/NetworkTests.kt b/experimental/behave/src/test/kotlin/net/corda/behave/network/NetworkTests.kt index e901b84560..642ba937c4 100644 --- a/experimental/behave/src/test/kotlin/net/corda/behave/network/NetworkTests.kt +++ b/experimental/behave/src/test/kotlin/net/corda/behave/network/NetworkTests.kt @@ -2,7 +2,7 @@ package net.corda.behave.network import net.corda.behave.database.DatabaseType import net.corda.behave.node.configuration.NotaryType -import net.corda.behave.seconds +import net.corda.core.utilities.seconds import org.junit.Ignore import org.junit.Test