Skip to content

Commit 09cf197

Browse files
authored
ZIO Test: Do Not Share Execution Event Sink (zio#8227)
* do not share execution event sink * fix compilation errors * cleanup
1 parent cf63bf8 commit 09cf197

File tree

6 files changed

+48
-23
lines changed

6 files changed

+48
-23
lines changed

test-sbt/js/src/main/scala/zio/test/sbt/ZTestRunnerJS.scala

+9-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,15 @@ sealed class ZTestTask(
9090
sendSummary: SendSummary,
9191
testArgs: TestArgs,
9292
spec: ZIOSpecAbstract
93-
) extends BaseTestTask(taskDef, testClassLoader, sendSummary, testArgs, spec, Runtime.default) {
93+
) extends BaseTestTask(
94+
taskDef,
95+
testClassLoader,
96+
sendSummary,
97+
testArgs,
98+
spec,
99+
Runtime.default,
100+
zio.Console.ConsoleLive
101+
) {
94102

95103
def execute(eventHandler: EventHandler, loggers: Array[Logger], continuation: Array[Task] => Unit): Unit = {
96104
val fiber = Runtime.default.unsafe.fork {

test-sbt/jvm/src/main/scala/zio/test/sbt/ZTestRunnerJVM.scala

+10-12
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,23 @@ final class ZTestRunnerJVM(val args: Array[String], val remoteArgs: Array[String
8686
private[sbt] def tasksZ(
8787
defs: Array[TaskDef],
8888
console: zio.Console
89-
)(implicit trace: Trace): Array[ZTestTask[ExecutionEventSink]] = {
89+
)(implicit trace: Trace): Array[ZTestTask[Any]] = {
9090
val testArgs = TestArgs.parse(args)
9191

9292
renderer = testArgs.testRenderer // Ensures summary is pretty in same style as rest of the test output
93-
val sharedSinkLayer = ExecutionEventSink.live(console, testArgs.testEventRenderer)
9493

9594
val specTasks: Array[ZIOSpecAbstract] = defs.map(disectTask(_, testClassLoader))
9695
val sharedLayerFromSpecs: ZLayer[Any, Any, Any] =
9796
(Scope.default ++ ZIOAppArgs.empty) >>> specTasks
9897
.map(_.bootstrap)
9998
.foldLeft(ZLayer.empty: ZLayer[ZIOAppArgs, Any, Any])(_ +!+ _)
10099

101-
val sharedLayer: ZLayer[Any, Any, ExecutionEventSink] =
102-
sharedLayerFromSpecs +!+ sharedSinkLayer
103-
104-
val runtime: Runtime.Scoped[ExecutionEventSink] =
105-
zio.Runtime.unsafe.fromLayer(sharedLayer)(Trace.empty, Unsafe.unsafe)
100+
val runtime =
101+
zio.Runtime.unsafe.fromLayer(sharedLayerFromSpecs)(Trace.empty, Unsafe.unsafe)
106102

107103
shutdownHook = Some(() => runtime.unsafe.shutdown()(Unsafe.unsafe))
108104

109-
defs.map(ZTestTask(_, testClassLoader, sendSummary, testArgs, runtime))
105+
defs.map(ZTestTask(_, testClassLoader, sendSummary, testArgs, runtime, console))
110106
}
111107

112108
private def disectTask(taskDef: TaskDef, testClassLoader: ClassLoader): ZIOSpecAbstract = {
@@ -127,19 +123,21 @@ final class ZTestTask[T](
127123
sendSummary: SendSummary,
128124
testArgs: TestArgs,
129125
spec: ZIOSpecAbstract,
130-
runtime: zio.Runtime[T]
131-
) extends BaseTestTask(taskDef, testClassLoader, sendSummary, testArgs, spec, runtime)
126+
runtime: zio.Runtime[T],
127+
console: zio.Console
128+
) extends BaseTestTask(taskDef, testClassLoader, sendSummary, testArgs, spec, runtime, console)
132129

133130
object ZTestTask {
134131
def apply[T](
135132
taskDef: TaskDef,
136133
testClassLoader: ClassLoader,
137134
sendSummary: SendSummary,
138135
args: TestArgs,
139-
runtime: zio.Runtime[T]
136+
runtime: zio.Runtime[T],
137+
console: zio.Console
140138
): ZTestTask[T] = {
141139
val zioSpec = disectTask(taskDef, testClassLoader)
142-
new ZTestTask(taskDef, testClassLoader, sendSummary, args, zioSpec, runtime)
140+
new ZTestTask(taskDef, testClassLoader, sendSummary, args, zioSpec, runtime, console)
143141
}
144142

145143
private def disectTask(taskDef: TaskDef, testClassLoader: ClassLoader): ZIOSpecAbstract = {

test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSbtSpec.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ object ZTestFrameworkSbtSpec {
162162
zTestTask.sendSummary.provideEnvironment(ZEnvironment(Summary(1, 0, 0, "foo"))),
163163
TestArgs.empty,
164164
zTestTask.spec,
165-
zio.Runtime.default
165+
zio.Runtime.default,
166+
zio.Console.ConsoleLive
166167
)
167168
}
168169
.head
@@ -186,7 +187,8 @@ object ZTestFrameworkSbtSpec {
186187
zTestTask.sendSummary.provideEnvironment(ZEnvironment(Summary(0, 0, 0, "foo"))),
187188
TestArgs.empty,
188189
zTestTask.spec,
189-
zio.Runtime.default
190+
zio.Runtime.default,
191+
zio.Console.ConsoleLive
190192
)
191193
}
192194
.head

test-sbt/native/src/main/scala/zio/test/sbt/ZTestRunnerNative.scala

+9-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,15 @@ sealed class ZTestTask(
9090
sendSummary: SendSummary,
9191
testArgs: TestArgs,
9292
spec: ZIOSpecAbstract
93-
) extends BaseTestTask(taskDef, testClassLoader, sendSummary, testArgs, spec, zio.Runtime.default) {
93+
) extends BaseTestTask(
94+
taskDef,
95+
testClassLoader,
96+
sendSummary,
97+
testArgs,
98+
spec,
99+
zio.Runtime.default,
100+
zio.Console.ConsoleLive
101+
) {
94102

95103
override def execute(eventHandler: EventHandler, loggers: Array[Logger]): Array[sbt.testing.Task] = {
96104
val fiber = Runtime.default.unsafe.fork {

test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package zio.test.sbt
22

33
import sbt.testing.{EventHandler, Logger, Task, TaskDef}
4-
import zio.{CancelableFuture, Scope, Trace, Unsafe, ZEnvironment, ZIO, ZIOAppArgs, ZLayer}
4+
import zio.{CancelableFuture, Console, Scope, Trace, Unsafe, ZEnvironment, ZIO, ZIOAppArgs, ZLayer}
55
import zio.test._
66

77
import scala.concurrent.Await
@@ -14,7 +14,8 @@ abstract class BaseTestTask[T](
1414
val sendSummary: SendSummary,
1515
val args: TestArgs,
1616
val spec: ZIOSpecAbstract,
17-
val runtime: zio.Runtime[T]
17+
val runtime: zio.Runtime[T],
18+
val console: Console
1819
) extends Task {
1920

2021
final override def taskDef(): TaskDef = taskDef0
@@ -29,7 +30,14 @@ abstract class BaseTestTask[T](
2930
)(implicit trace: Trace): ZIO[Any, Throwable, Unit] =
3031
(for {
3132
summary <-
32-
spec.runSpecWithSharedRuntimeLayer(taskDef0.fullyQualifiedName(), spec.spec, args, runtime, eventHandlerZ)
33+
spec.runSpecWithSharedRuntimeLayer(
34+
taskDef0.fullyQualifiedName(),
35+
spec.spec,
36+
args,
37+
runtime,
38+
eventHandlerZ,
39+
console
40+
)
3341
_ <- sendSummary.provideEnvironment(ZEnvironment(summary))
3442
} yield ())
3543
.provideLayer(sharedFilledTestLayer)

test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala

+5-4
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,22 @@ abstract class ZIOSpecAbstract extends ZIOApp with ZIOSpecAbstractVersionSpecifi
130130
spec: Spec[Environment with TestEnvironment with Scope, Any],
131131
testArgs: TestArgs,
132132
runtime: Runtime[_],
133-
testEventHandler: ZTestEventHandler
133+
testEventHandler: ZTestEventHandler,
134+
console: Console
134135
)(implicit
135136
trace: Trace
136137
): UIO[Summary] = {
137138
val filteredSpec = FilteredSpec(spec, testArgs)
138139

139-
val castedRuntime: Runtime[Environment with ExecutionEventSink] =
140-
runtime.asInstanceOf[Runtime[Environment with ExecutionEventSink]]
140+
val castedRuntime: Runtime[Environment] =
141+
runtime.asInstanceOf[Runtime[Environment]]
141142

142143
TestRunner(
143144
TestExecutor
144145
.default[Environment, Any](
145146
ZLayer.succeedEnvironment(castedRuntime.environment),
146147
testEnvironment ++ Scope.default,
147-
ZLayer.succeedEnvironment(castedRuntime.environment),
148+
ExecutionEventSink.live(console, testArgs.testEventRenderer),
148149
testEventHandler
149150
)
150151
).run(fullyQualifiedName, aspects.foldLeft(filteredSpec)(_ @@ _) @@ TestAspect.fibers)

0 commit comments

Comments
 (0)