Skip to content

Commit a3890c9

Browse files
committed
cc postgresql: collect cancel tests in single file
commit_hash:b941055cddb5e94617684e51f5be0f2dab76450f
1 parent 4e51b15 commit a3890c9

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

postgresql/src/storages/postgres/tests/cancel_pgtest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <storages/postgres/tests/util_pgtest.hpp>
22
#include <userver/engine/async.hpp>
3+
#include <userver/engine/single_consumer_event.hpp>
34
#include <userver/engine/sleep.hpp>
45

56
USERVER_NAMESPACE_BEGIN
@@ -54,6 +55,40 @@ UTEST_P(PostgreConnection, Cancel) {
5455
ASSERT_NO_THROW(conn->Execute("select 1", /*query_params*/ {}, cmd_ctrl)) << "connection must be usable";
5556
}
5657

58+
void CleanupConnectionTest(storages::postgres::detail::ConnectionPtr& conn, bool use_cancel) {
59+
EXPECT_EQ(pg::ConnectionState::kIdle, conn->GetState());
60+
61+
const DefaultCommandControlScope scope(pg::CommandControl{utest::kMaxTestWaitTime, utest::kMaxTestWaitTime});
62+
63+
engine::SingleConsumerEvent task_started;
64+
auto task = engine::AsyncNoSpan([&] {
65+
task_started.Send();
66+
UEXPECT_THROW(conn->Execute("select pg_sleep(1)"), pg::ConnectionInterrupted);
67+
});
68+
ASSERT_TRUE(task_started.WaitForEventFor(utest::kMaxTestWaitTime));
69+
task.RequestCancel();
70+
task.WaitFor(utest::kMaxTestWaitTime);
71+
ASSERT_TRUE(task.IsFinished());
72+
73+
EXPECT_EQ(pg::ConnectionState::kTranActive, conn->GetState());
74+
if (use_cancel) {
75+
UEXPECT_NO_THROW(conn->CancelAndCleanup(utest::kMaxTestWaitTime));
76+
} else {
77+
UEXPECT_NO_THROW(conn->Cleanup(std::chrono::seconds{2}));
78+
}
79+
EXPECT_EQ(pg::ConnectionState::kIdle, conn->GetState());
80+
}
81+
82+
UTEST_P(PostgreConnection, QueryTaskCancelAndCleanup) {
83+
CheckConnection(GetConn());
84+
CleanupConnectionTest(GetConn(), /*use cancel=*/true);
85+
}
86+
87+
UTEST_P(PostgreConnection, QueryTaskCleanup) {
88+
CheckConnection(GetConn());
89+
CleanupConnectionTest(GetConn(), /*use cancel=*/false);
90+
}
91+
5792
} // namespace
5893

5994
USERVER_NAMESPACE_END

postgresql/src/storages/postgres/tests/connection_pgtest.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <storages/postgres/tests/util_pgtest.hpp>
22

33
#include <userver/concurrent/background_task_storage.hpp>
4-
#include <userver/engine/single_consumer_event.hpp>
54

65
#include <storages/postgres/detail/connection.hpp>
76
#include <userver/storages/postgres/dsn.hpp>
@@ -363,40 +362,6 @@ UTEST_P(PostgreConnection, StatementTimeout) {
363362
EXPECT_FALSE(GetConn()->IsBroken());
364363
}
365364

366-
void CleanupConnectionTest(storages::postgres::detail::ConnectionPtr& conn, bool use_cancel) {
367-
EXPECT_EQ(pg::ConnectionState::kIdle, conn->GetState());
368-
369-
const DefaultCommandControlScope scope(pg::CommandControl{utest::kMaxTestWaitTime, utest::kMaxTestWaitTime});
370-
371-
engine::SingleConsumerEvent task_started;
372-
auto task = engine::AsyncNoSpan([&] {
373-
task_started.Send();
374-
UEXPECT_THROW(conn->Execute("select pg_sleep(1)"), pg::ConnectionInterrupted);
375-
});
376-
ASSERT_TRUE(task_started.WaitForEventFor(utest::kMaxTestWaitTime));
377-
task.RequestCancel();
378-
task.WaitFor(utest::kMaxTestWaitTime);
379-
ASSERT_TRUE(task.IsFinished());
380-
381-
EXPECT_EQ(pg::ConnectionState::kTranActive, conn->GetState());
382-
if (use_cancel) {
383-
UEXPECT_NO_THROW(conn->CancelAndCleanup(utest::kMaxTestWaitTime));
384-
} else {
385-
UEXPECT_NO_THROW(conn->Cleanup(std::chrono::seconds{2}));
386-
}
387-
EXPECT_EQ(pg::ConnectionState::kIdle, conn->GetState());
388-
}
389-
390-
UTEST_P(PostgreConnection, QueryTaskCancelAndCleanup) {
391-
CheckConnection(GetConn());
392-
CleanupConnectionTest(GetConn(), /*use cancel=*/true);
393-
}
394-
395-
UTEST_P(PostgreConnection, QueryTaskCleanup) {
396-
CheckConnection(GetConn());
397-
CleanupConnectionTest(GetConn(), /*use cancel=*/false);
398-
}
399-
400365
UTEST_P(PostgreConnection, CachedPlanChange) {
401366
// this only works with english messages, better than nothing
402367
GetConn()->Execute("SET lc_messages = 'en_US.UTF-8'");

0 commit comments

Comments
 (0)