Skip to content

Add test case that exhibits portal does not exist problem. #904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 68 additions & 6 deletions modules/tests/shared/src/test/scala/DescribeCacheTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

package tests

import skunk.implicits._
import skunk.codec.numeric.int4
import cats.syntax.all._
import cats.effect.IO
import skunk.exception.PostgresErrorException
import cats.effect.Resource
import skunk.Session
import cats.syntax.all._
import org.typelevel.otel4s.trace.Tracer
import skunk.codec.numeric.int4
import skunk.codec.text
import skunk._
import skunk.exception.PostgresErrorException
import skunk.implicits._

class DescribeCacheTest extends SkunkTest {
class DescribeCacheTest extends SkunkTest(true) {

implicit val tracer: Tracer[IO] = Tracer.noop

Expand Down Expand Up @@ -82,6 +83,67 @@ class DescribeCacheTest extends SkunkTest {
} yield "ok"
}

val runs = 100
// This should not fail
pooledTest("portal1 - concurrent portal with normal flatMap") { pool =>
import scala.concurrent.duration._
def cmd(idx: Int): Command[String *: Int *: EmptyTuple] = sql"INSERT INTO scalars VALUES (${text.varchar}, ${int4}) -- #${idx.toString}".command
1.to(runs).toList.parTraverse{ idx =>
pool.use { s =>
s.transaction.use { _ =>
s.prepare(cmd(idx)).flatMap(_.execute((s"name$idx", idx))).flatMap (_ =>
IO.sleep(1.second) >> s.prepare(cmd(idx)).flatMap(_.execute((s"name$idx", idx))).void
)
}
}
}
}

// This should not fail
pooledTest("portal2 - concurrent portal with map / identity *inside tx*") { pool =>
import scala.concurrent.duration._
def cmd(idx: Int): Command[String *: Int *: EmptyTuple] = sql"INSERT INTO scalars VALUES (${text.varchar}, ${int4}) -- #${idx.toString}".command
1.to(runs).toList.parTraverse{ idx =>
pool.use { s =>
s.transaction.use { _ =>
s.prepare(cmd(idx)).flatMap(_.execute((s"name$idx", idx))).map (_ =>
IO.sleep(1.second) >> s.prepare(cmd(idx)).flatMap(_.execute((s"name$idx", idx))).void
).flatMap(identity)
}
}
}
}

// This will fail if run enough
pooledTest("portal3 - concurrent portal with map / identity *outside tx*") { pool =>
import scala.concurrent.duration._
def cmd(idx: Int): Command[String *: Int *: EmptyTuple] = sql"INSERT INTO scalars VALUES (${text.varchar}, ${int4}) -- #${idx.toString}".command
1.to(runs).toList.parTraverse{ idx =>
pool.use { s =>
s.transaction.use { _ =>
s.prepare(cmd(idx)).flatMap(_.execute((s"name$idx", idx))).map (_ =>
IO.sleep(1.second) >> s.prepare(cmd(idx)).flatMap(_.execute((s"name$idx", idx))).void
)
}
}.flatMap(identity)
Comment on lines +122 to +128
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

}
}

// This will fail if run enough
pooledTest("portal4 - concurrent portal with flatten *outside tx*") { pool =>
import scala.concurrent.duration._
def cmd(idx: Int): Command[String *: Int *: EmptyTuple] = sql"INSERT INTO scalars VALUES (${text.varchar}, ${int4}) -- #${idx.toString}".command
1.to(runs).toList.parTraverse{ idx =>
pool.use { s =>
s.transaction.use { _ =>
s.prepare(cmd(idx)).flatMap(_.execute((s"name$idx", idx))).map (_ =>
IO.sleep(1.second) >> s.prepare(cmd(idx)).flatMap(_.execute((s"name$idx", idx))).void
)
}
}.flatten
Comment on lines +137 to +143
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.use(...).flatten looks wrong. It means you are building an IO in the use(...) when the Resource is available, but you are executing it after the use, when the resource has been released.

}
}

sessionTest("command should not be cached after cache is cleared") { s =>
val cmd = sql"commit".command
for {
Expand Down
5 changes: 5 additions & 0 deletions world/world.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

CREATE TYPE myenum AS ENUM ('foo', 'bar');

CREATE TABLE IF NOT EXISTS scalars(
a_string varchar not null,
a_int integer not null
);

CREATE TABLE IF NOT EXISTS city (
id integer NOT NULL,
name varchar NOT NULL,
Expand Down