From 776cf05bcc5a2b54e84eb59c2d57ccefd0d04393 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Thu, 2 Mar 2023 19:33:29 +0000 Subject: [PATCH] Fix `IORuntimeBuilder` `failureReporter` config on JS --- .../unsafe/IORuntimeBuilderPlatform.scala | 49 +++++++++++++++++++ .../unsafe/IORuntimeBuilderPlatform.scala | 0 .../effect/unsafe/IORuntimeBuilderSpec.scala | 33 +++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 core/js/src/main/scala/cats/effect/unsafe/IORuntimeBuilderPlatform.scala rename core/{js-native => native}/src/main/scala/cats/effect/unsafe/IORuntimeBuilderPlatform.scala (100%) create mode 100644 tests/shared/src/test/scala/cats/effect/unsafe/IORuntimeBuilderSpec.scala diff --git a/core/js/src/main/scala/cats/effect/unsafe/IORuntimeBuilderPlatform.scala b/core/js/src/main/scala/cats/effect/unsafe/IORuntimeBuilderPlatform.scala new file mode 100644 index 0000000000..7583f65e9c --- /dev/null +++ b/core/js/src/main/scala/cats/effect/unsafe/IORuntimeBuilderPlatform.scala @@ -0,0 +1,49 @@ +/* + * Copyright 2020-2023 Typelevel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cats.effect.unsafe + +private[unsafe] abstract class IORuntimeBuilderPlatform { self: IORuntimeBuilder => + + protected def platformSpecificBuild: IORuntime = { + val defaultShutdown: () => Unit = () => () + val (compute, computeShutdown) = customCompute.getOrElse( + ( + IORuntime.createBatchingMacrotaskExecutor(reportFailure = failureReporter), + defaultShutdown + ) + ) + val (blocking, blockingShutdown) = customBlocking.getOrElse((compute, defaultShutdown)) + val (scheduler, schedulerShutdown) = + customScheduler.getOrElse((IORuntime.defaultScheduler, defaultShutdown)) + val shutdown = () => { + computeShutdown() + blockingShutdown() + schedulerShutdown() + extraShutdownHooks.reverse.foreach(_()) + } + val runtimeConfig = customConfig.getOrElse(IORuntimeConfig()) + + IORuntime.apply( + computeTransform(compute), + blockingTransform(blocking), + scheduler, + shutdown, + runtimeConfig + ) + } + +} diff --git a/core/js-native/src/main/scala/cats/effect/unsafe/IORuntimeBuilderPlatform.scala b/core/native/src/main/scala/cats/effect/unsafe/IORuntimeBuilderPlatform.scala similarity index 100% rename from core/js-native/src/main/scala/cats/effect/unsafe/IORuntimeBuilderPlatform.scala rename to core/native/src/main/scala/cats/effect/unsafe/IORuntimeBuilderPlatform.scala diff --git a/tests/shared/src/test/scala/cats/effect/unsafe/IORuntimeBuilderSpec.scala b/tests/shared/src/test/scala/cats/effect/unsafe/IORuntimeBuilderSpec.scala new file mode 100644 index 0000000000..c72c06fd79 --- /dev/null +++ b/tests/shared/src/test/scala/cats/effect/unsafe/IORuntimeBuilderSpec.scala @@ -0,0 +1,33 @@ +/* + * Copyright 2020-2023 Typelevel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cats.effect +package unsafe + +class IORuntimeBuilderSpec extends BaseSpec with DetectPlatform { + + "IORuntimeBuilder" should { + if (isNative) "configure the failure reporter" in pending + else + "configure the failure reporter" in { + var invoked = false + val rt = IORuntime.builder().setFailureReporter(_ => invoked = true).build() + rt.compute.reportFailure(new Exception) + invoked must beTrue + } + } + +}