|  | 
|  | 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