Skip to content

Commit 084f7ee

Browse files
committed
Use Global EC for CatsResource
1 parent be95599 commit 084f7ee

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

scalatest/shared/src/main/scala/cats/effect/testing/scalatest/CatsResource.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,18 @@ trait CatsResource[F[_], A] extends BeforeAndAfterAll { this: FixtureAsyncTestSu
7373
}
7474

7575
override def afterAll(): Unit = {
76-
UnsafeRun[F].unsafeToFuture(shutdown, finiteResourceTimeout)
77-
78-
gate = None
79-
value = None
80-
shutdown = ().pure[F]
76+
UnsafeRun[F].unsafeToFuture(
77+
for {
78+
_ <- shutdown
79+
_ <- Sync[F] delay {
80+
gate = None
81+
value = None
82+
shutdown = ().pure[F]
83+
}
84+
} yield (),
85+
finiteResourceTimeout
86+
)
87+
()
8188
}
8289

8390
override type FixtureParam = A

scalatest/shared/src/main/scala/cats/effect/testing/scalatest/CatsResourceIO.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ import cats.effect.unsafe.IORuntime
2222

2323
import org.scalatest.FixtureAsyncTestSuite
2424

25-
import scala.concurrent.Future
25+
import scala.concurrent.{ExecutionContext, Future}
2626
import scala.concurrent.duration._
2727

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

30+
override implicit def executionContext: ExecutionContext = ExecutionContext.global
31+
3032
final def ResourceAsync = Async[IO]
3133

3234
final def ResourceUnsafeRun = _ResourceUnsafeRun
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2020 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cats.effect.testing.scalatest
18+
19+
import cats.effect.{IO, Resource}
20+
import org.scalatest.concurrent.Eventually
21+
import org.scalatest.events.Event
22+
import org.scalatest.{Args, Reporter}
23+
import org.scalatest.matchers.must.Matchers._
24+
import org.scalatest.wordspec.{AsyncWordSpec, FixtureAsyncWordSpec}
25+
26+
import scala.concurrent.duration._
27+
28+
class CatsResourceAllocationSpecs extends AsyncWordSpec with Eventually {
29+
30+
override implicit def patienceConfig: PatienceConfig =
31+
super.patienceConfig.copy(timeout = 1.second)
32+
33+
@volatile
34+
var beforeCalled: Int = 0
35+
36+
@volatile
37+
var afterCalled: Int = 0
38+
39+
class ResourceSpec
40+
extends FixtureAsyncWordSpec
41+
with AsyncIOSpec
42+
with CatsResourceIO[Unit] {
43+
44+
override val resource: Resource[IO, Unit] =
45+
Resource.make { IO.delay { beforeCalled += 1 } } { _ =>
46+
IO.delay { afterCalled += 1 }
47+
}
48+
49+
"test" should {
50+
"doFoo" in { _ => true mustBe true }
51+
}
52+
}
53+
54+
val reporter: Reporter = (_: Event) => ()
55+
56+
"cats resource allocation" should {
57+
"release the resource" in {
58+
59+
val outerResourceSpec = new ResourceSpec
60+
61+
outerResourceSpec.run(None, Args(reporter))
62+
63+
eventually {
64+
beforeCalled mustBe 1
65+
afterCalled mustBe 1
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)