-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c7c3ad
commit 2d86feb
Showing
4 changed files
with
72 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
modules/core/src/test/scala/doobie/util/QueryCancellationSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2013-2020 Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package doobie.util | ||
|
||
import cats.effect.{IO, Resource} | ||
import cats.implicits.catsSyntaxApplicativeId | ||
import doobie.Transactor | ||
import doobie.* | ||
import doobie.implicits.* | ||
import doobie.syntax.* | ||
import cats.syntax.all.* | ||
|
||
import scala.concurrent.duration.DurationInt | ||
|
||
class QueryCancellationSuite extends munit.FunSuite { | ||
import cats.effect.unsafe.implicits.global | ||
|
||
val xa = Transactor.fromDriverManager[IO]( | ||
driver = "org.h2.Driver", | ||
url = "jdbc:h2:mem:queryspec;DB_CLOSE_DELAY=-1", | ||
user = "sa", | ||
password = "", | ||
logHandler = None | ||
) | ||
|
||
test("Query cancel") { | ||
val scenario = WeakAsync.liftIO[ConnectionIO].use { elevator => | ||
for { | ||
_ <- sql"CREATE TABLE IF NOT EXISTS example_table ( id INT)".update.run.transact(xa) | ||
_ <- sql"TRUNCATE TABLE example_table".update.run.transact(xa) | ||
_ <- sql"INSERT INTO example_table (id) VALUES (1)".update.run.transact(xa) | ||
_ <- { | ||
sql"select * from example_table for update".query[Int].unique >> elevator.liftIO(IO.never) | ||
}.transact(xa).start | ||
|
||
insertWithLockFiber <- { | ||
for { | ||
_ <- IO.sleep(100.milli) | ||
insertFiber <- sql"UPDATE example_table SET id = 2".update.run.transact(xa).start | ||
_ <- IO.sleep(100.milli) | ||
_ <- insertFiber.cancel | ||
} yield () | ||
}.start | ||
|
||
_ <- IO.race(insertWithLockFiber.join, IO.sleep(5.seconds) >> IO(fail("Cancellation is blocked"))) | ||
result <- sql"SELECT * FROM example_table".query[Int].to[List].transact(xa) | ||
} yield assertEquals(result, List(1)) | ||
} | ||
scenario.unsafeRunSync() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters