Skip to content

Commit

Permalink
Fixed all the weirdness and insanity with ScalaJS
Browse files Browse the repository at this point in the history
  • Loading branch information
djspiewak committed Jul 31, 2021
1 parent ec8f185 commit 7894980
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 18 deletions.
30 changes: 30 additions & 0 deletions core/js/src/main/scala/cats/effect/testing/RuntimePlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2020-2021 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 testing

import scala.concurrent.ExecutionContext

private[testing] trait RuntimePlatform {
private[testing] def createIORuntime(delegate: ExecutionContext): unsafe.IORuntime =
unsafe.IORuntime(
delegate,
unsafe.IORuntime.global.blocking,
unsafe.IORuntime.global.scheduler,
unsafe.IORuntime.global.shutdown,
unsafe.IORuntime.global.config)
}
28 changes: 28 additions & 0 deletions core/jvm/src/main/scala/cats/effect/testing/RuntimePlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2020-2021 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 testing

import scala.concurrent.ExecutionContext

private[testing] trait RuntimePlatform {
private[testing] def createIORuntime(ec: ExecutionContext): unsafe.IORuntime = {
val (blocking, blockingSD) = unsafe.IORuntime.createDefaultBlockingExecutionContext()
val (scheduler, schedulerSD) = unsafe.IORuntime.createDefaultScheduler()
unsafe.IORuntime(ec, blocking, scheduler, { () => blockingSD(); schedulerSD(); }, unsafe.IORuntimeConfig())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import cats.syntax.all._

import org.scalatest.{BeforeAndAfterAll, FixtureAsyncTestSuite, FutureOutcome, Outcome}

import scala.concurrent.Await
import scala.concurrent.duration._

trait CatsResource[F[_], A] extends BeforeAndAfterAll { this: FixtureAsyncTestSuite =>
Expand Down Expand Up @@ -74,7 +73,7 @@ trait CatsResource[F[_], A] extends BeforeAndAfterAll { this: FixtureAsyncTestSu
}

override def afterAll(): Unit = {
Await.result(UnsafeRun[F].unsafeToFuture(shutdown, finiteResourceTimeout), ResourceTimeout)
UnsafeRun[F].unsafeToFuture(shutdown, finiteResourceTimeout)

gate = None
value = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,25 @@ package cats.effect.testing
package scalatest

import cats.effect.{Async, IO}
import cats.effect.unsafe.IORuntime

import org.scalatest.FixtureAsyncTestSuite

trait CatsResourceIO[A] extends CatsResource[IO, A] { this: FixtureAsyncTestSuite =>
import scala.concurrent.Future
import scala.concurrent.duration._

trait CatsResourceIO[A] extends CatsResource[IO, A] with RuntimePlatform { this: FixtureAsyncTestSuite =>

final def ResourceAsync = Async[IO]
final def ResourceUnsafeRun = UnsafeRun[IO]

final val ResourceUnsafeRun =
new UnsafeRun[IO] {
private implicit val runtime: IORuntime = createIORuntime(executionContext)

override def unsafeToFuture[B](ioa: IO[B]): Future[B] =
unsafeToFuture(ioa, None)

override def unsafeToFuture[B](ioa: IO[B], timeout: Option[FiniteDuration]): Future[B] =
timeout.fold(ioa)(ioa.timeout).unsafeToFuture()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2020-2021 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.testing.specs2

trait CatsEffectSpecsPlatform { this: CatsEffectSpecs =>
def platformSpecs = "really execute effects" in skipped
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2020-2021 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.testing.specs2

import cats.effect.IO

trait CatsEffectSpecsPlatform { this: CatsEffectSpecs =>
def platformSpecs = {
"really execute effects" in {
var gate = false

"forcibly attempt to get the deferred value" in {
IO.cede.untilM_(IO(gate)).as(ok)
}

"complete the deferred value inside IO context" in {
(IO { gate = true }).as(ok)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import cats.syntax.all._

import org.specs2.specification.BeforeAfterAll

import scala.concurrent.Await
import scala.concurrent.duration._

abstract class CatsResource[F[_]: Async: UnsafeRun, A] extends BeforeAfterAll with CatsEffect {
Expand Down Expand Up @@ -66,7 +65,7 @@ abstract class CatsResource[F[_]: Async: UnsafeRun, A] extends BeforeAfterAll wi
}

override def afterAll(): Unit = {
Await.result(UnsafeRun[F].unsafeToFuture(shutdown, finiteResourceTimeout), ResourceTimeout)
UnsafeRun[F].unsafeToFuture(shutdown, finiteResourceTimeout)

gate = None
value = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import cats.effect.{IO, Ref, Resource}
import cats.implicits._
import org.specs2.mutable.Specification

class CatsEffectSpecs extends Specification with CatsEffect {
class CatsEffectSpecs extends Specification with CatsEffect with CatsEffectSpecsPlatform {

"cats effect specifications" should {
"run a non-effectful test" in {
Expand All @@ -42,17 +42,7 @@ class CatsEffectSpecs extends Specification with CatsEffect {
}
}

"really execute effects" in {
var gate = false

"forcibly attempt to get the deferred value" in {
IO.cede.untilM_(IO(gate)).as(ok)
}

"complete the deferred value inside IO context" in {
(IO { gate = true }).as(ok)
}
}
platformSpecs

// "timeout a failing test" in (IO.never: IO[Boolean])
}
Expand Down

0 comments on commit 7894980

Please sign in to comment.