Skip to content

Commit

Permalink
A bunch of little fixes + reorg listeners code
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Aug 5, 2024
1 parent 22baeda commit 4340e8f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ExecutionResult(
val classesFailures =
this.classesResults.toList().filter { it.second is Aborted || it.second is Failed }
val testsFailures =
this.classesResults.toList().filter { it.second is Aborted || it.second is Failed }
this.testResults.toList().filter { it.second is Aborted || it.second is Failed }

fun printFailures(failureList: List<Pair<TestIdentifier, TestResult>>) {
for (failure in failureList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
//
// This file is part of the Restate SDK Test suite tool,
// which is released under the MIT license.
//
// You can find a copy of the license in file LICENSE in the root
// directory of this repository or package, or at
// https://github.com/restatedev/sdk-test-suite/blob/main/LICENSE
package dev.restate.sdktesting.junit

import kotlin.jvm.optionals.getOrNull
import org.apache.logging.log4j.ThreadContext
import org.junit.platform.engine.TestExecutionResult
import org.junit.platform.engine.support.descriptor.ClassSource
import org.junit.platform.launcher.TestExecutionListener
import org.junit.platform.launcher.TestIdentifier
import org.junit.platform.launcher.TestPlan

class InjectLog4jContextListener(val suiteName: String) : TestExecutionListener {

companion object {
const val TEST_NAME = "test"
}

@Volatile var testPlan: TestPlan? = null

override fun testPlanExecutionStarted(testPlan: TestPlan) {
this.testPlan = testPlan
}

override fun executionStarted(testIdentifier: TestIdentifier) {
if (testIdentifier.isTest || testIdentifier.source.getOrNull() is ClassSource) {
val displayName = describeTestIdentifier(suiteName, testPlan!!, testIdentifier)
ThreadContext.put(TEST_NAME, displayName)
}
}

override fun executionFinished(
testIdentifier: TestIdentifier,
testExecutionResult: TestExecutionResult
) {
ThreadContext.remove(TEST_NAME)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
//
// This file is part of the Restate SDK Test suite tool,
// which is released under the MIT license.
//
// You can find a copy of the license in file LICENSE in the root
// directory of this repository or package, or at
// https://github.com/restatedev/sdk-test-suite/blob/main/LICENSE
package dev.restate.sdktesting.junit

import com.github.ajalt.mordant.terminal.Terminal
import kotlin.jvm.optionals.getOrNull
import org.junit.platform.engine.TestExecutionResult
import org.junit.platform.engine.support.descriptor.ClassSource
import org.junit.platform.launcher.TestExecutionListener
import org.junit.platform.launcher.TestIdentifier
import org.junit.platform.launcher.TestPlan

class LogTestEventsToTerminalListener(val suiteName: String, val terminal: Terminal) :
TestExecutionListener {
@Volatile var testPlan: TestPlan? = null

override fun testPlanExecutionStarted(testPlan: TestPlan) {
this.testPlan = testPlan
}

override fun executionFinished(
testIdentifier: TestIdentifier,
testExecutionResult: TestExecutionResult
) {
if (testIdentifier.isTest) {
val name = describeTestIdentifier(suiteName, testPlan!!, testIdentifier)
when (testExecutionResult.status!!) {
TestExecutionResult.Status.SUCCESSFUL -> terminal.println("$name")
TestExecutionResult.Status.ABORTED -> terminal.println("$name")
TestExecutionResult.Status.FAILED -> {
terminal.println("$name")
}
}
}
if (testIdentifier.source.getOrNull() is ClassSource) {
val name = describeTestIdentifier(suiteName, testPlan!!, testIdentifier)
when (testExecutionResult.status!!) {
TestExecutionResult.Status.ABORTED -> terminal.println("$name init")
TestExecutionResult.Status.FAILED -> {
terminal.println("$name init")
}
else -> {}
}
}
}
}
64 changes: 2 additions & 62 deletions src/main/kotlin/dev/restate/sdktesting/junit/TestSuite.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@ import dev.restate.sdktesting.infra.getGlobalConfig
import dev.restate.sdktesting.infra.registerGlobalConfig
import java.io.PrintWriter
import java.nio.file.Path
import kotlin.jvm.optionals.getOrNull
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.ThreadContext
import org.apache.logging.log4j.core.config.Configurator
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration
import org.junit.platform.engine.Filter
import org.junit.platform.engine.TestExecutionResult
import org.junit.platform.engine.discovery.DiscoverySelectors
import org.junit.platform.engine.support.descriptor.ClassSource
import org.junit.platform.engine.support.descriptor.MethodSource
import org.junit.platform.launcher.*
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder
import org.junit.platform.launcher.core.LauncherFactory
Expand Down Expand Up @@ -80,63 +75,8 @@ class TestSuite(
reportDir.resolve("testrunner.stdout"),
reportDir.resolve("testrunner.stderr"),
errWriter)
val logTestEventsListener =
object : TestExecutionListener {
@Volatile var testPlan: TestPlan? = null

override fun testPlanExecutionStarted(testPlan: TestPlan) {
this.testPlan = testPlan
}

override fun executionFinished(
testIdentifier: TestIdentifier,
testExecutionResult: TestExecutionResult
) {
if (testIdentifier.isTest) {
val name = describeTestIdentifier(name, testPlan!!, testIdentifier)
when (testExecutionResult.status!!) {
TestExecutionResult.Status.SUCCESSFUL -> terminal.println("$name")
TestExecutionResult.Status.ABORTED -> terminal.println("$name")
TestExecutionResult.Status.FAILED -> {
terminal.println("$name")
}
}
}
if (testIdentifier.source.getOrNull() is ClassSource) {
val name = describeTestIdentifier(name, testPlan!!, testIdentifier)
when (testExecutionResult.status!!) {
TestExecutionResult.Status.ABORTED -> terminal.println("$name init")
TestExecutionResult.Status.FAILED -> {
terminal.println("$name init")
}
else -> {}
}
}
}
}
val injectLoggingContextListener =
object : TestExecutionListener {
val TEST_NAME = "test"

override fun executionStarted(testIdentifier: TestIdentifier) {
val displayName =
when (val source = testIdentifier.source.getOrNull()) {
is ClassSource -> source.className
is MethodSource -> "${source.className}#${source.methodName}"
else -> null
}
if (displayName != null) {
ThreadContext.put(TEST_NAME, displayName)
}
}

override fun executionFinished(
testIdentifier: TestIdentifier,
testExecutionResult: TestExecutionResult
) {
ThreadContext.remove(TEST_NAME)
}
}
val logTestEventsListener = LogTestEventsToTerminalListener(name, terminal)
val injectLoggingContextListener = InjectLog4jContextListener(name)

// Launch
LauncherFactory.openSession().use { session ->
Expand Down

0 comments on commit 4340e8f

Please sign in to comment.