Skip to content

Commit dead33f

Browse files
authored
Fix flaky test ShouldRollbackTransactionWhenCheckFails2 (#14047)
1 parent 9657f16 commit dead33f

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

ydb/core/fq/libs/ydb/ut/ydb_ut.cpp

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <ydb/core/fq/libs/ydb/ydb.h>
2+
#include <ydb/core/fq/libs/ydb/util.h>
23

34
#include <ydb/library/security/ydb_credentials_provider_factory.h>
45

@@ -19,31 +20,45 @@ namespace {
1920

2021
////////////////////////////////////////////////////////////////////////////////
2122

22-
TYdbConnectionPtr MakeConnection(const char* tablePrefix) {
23-
NConfig::TYdbStorageConfig config;
23+
class TFixture : public NUnitTest::TBaseFixture {
24+
public:
25+
TYdbConnectionPtr MakeConnection(const char* tablePrefix) {
26+
NConfig::TYdbStorageConfig config;
2427

25-
config.SetEndpoint(GetEnv("YDB_ENDPOINT"));
26-
config.SetDatabase(GetEnv("YDB_DATABASE"));
27-
config.SetToken("");
28-
config.SetTablePrefix(tablePrefix);
28+
config.SetEndpoint(GetEnv("YDB_ENDPOINT"));
29+
config.SetDatabase(GetEnv("YDB_DATABASE"));
30+
config.SetToken("");
31+
config.SetTablePrefix(tablePrefix);
2932

30-
NYdb::TDriver driver({});
31-
auto connection = NewYdbConnection(config, NKikimr::CreateYdbCredentialsProviderFactory, driver);
33+
NYdb::TDriver driver({});
34+
Connection = NewYdbConnection(config, NKikimr::CreateYdbCredentialsProviderFactory, driver);
3235

33-
auto status = connection->SchemeClient.MakeDirectory(connection->TablePathPrefix).GetValueSync();
34-
UNIT_ASSERT_C(status.IsSuccess(), status.GetIssues().ToString());
36+
auto status = Connection->SchemeClient.MakeDirectory(Connection->TablePathPrefix).GetValueSync();
37+
UNIT_ASSERT_C(status.IsSuccess(), status.GetIssues().ToString());
3538

36-
auto desc = TTableBuilder()
37-
.AddNullableColumn("id", EPrimitiveType::String)
38-
.AddNullableColumn("generation", EPrimitiveType::Uint64)
39-
.SetPrimaryKeyColumn("id")
40-
.Build();
39+
auto desc = TTableBuilder()
40+
.AddNullableColumn("id", EPrimitiveType::String)
41+
.AddNullableColumn("generation", EPrimitiveType::Uint64)
42+
.SetPrimaryKeyColumn("id")
43+
.Build();
4144

42-
status = CreateTable(connection, "test", std::move(desc)).GetValueSync();
43-
UNIT_ASSERT_C(status.IsSuccess(), status.GetIssues().ToString());
45+
status = CreateTable(Connection, "test", std::move(desc)).GetValueSync();
46+
UNIT_ASSERT_C(status.IsSuccess(), status.GetIssues().ToString());
47+
return Connection;
48+
}
4449

45-
return connection;
46-
}
50+
void TearDown(NUnitTest::TTestContext& /*ctx*/) override {
51+
if (Connection) {
52+
auto tablePath = JoinPath(Connection->TablePathPrefix, "test");
53+
Connection->TableClient.RetryOperation(
54+
[tablePath = std::move(tablePath)] (TSession session) mutable {
55+
return session.DropTable(tablePath);
56+
}).GetValueSync();;
57+
}
58+
}
59+
60+
TYdbConnectionPtr Connection;
61+
};
4762

4863
TFuture<TStatus> CheckTransactionClosed(const TFuture<TStatus>& future, const TGenerationContextPtr& context) {
4964
return future.Apply(
@@ -90,7 +105,7 @@ TFuture<TStatus> UpsertDummyInTransaction(const TFuture<TStatus>& future, const
90105
////////////////////////////////////////////////////////////////////////////////
91106

92107
Y_UNIT_TEST_SUITE(TRegisterCheckTest) {
93-
Y_UNIT_TEST(ShouldRegisterCheckNewGeneration)
108+
Y_UNIT_TEST_F(ShouldRegisterCheckNewGeneration, TFixture)
94109
{
95110
auto connection = MakeConnection("ShouldRegisterCheckNewGeneration");
96111

@@ -114,7 +129,7 @@ Y_UNIT_TEST_SUITE(TRegisterCheckTest) {
114129
UNIT_ASSERT_C(status.IsSuccess(), status.GetIssues().ToString());
115130
}
116131

117-
Y_UNIT_TEST(ShouldRegisterCheckSameGeneration)
132+
Y_UNIT_TEST_F(ShouldRegisterCheckSameGeneration, TFixture)
118133
{
119134
auto connection = MakeConnection("ShouldRegisterCheckSameGeneration");
120135

@@ -156,7 +171,7 @@ Y_UNIT_TEST_SUITE(TRegisterCheckTest) {
156171
UNIT_ASSERT_C(status.IsSuccess(), status.GetIssues().ToString());
157172
}
158173

159-
Y_UNIT_TEST(ShouldRegisterCheckNextGeneration)
174+
Y_UNIT_TEST_F(ShouldRegisterCheckNextGeneration, TFixture)
160175
{
161176
auto connection = MakeConnection("ShouldRegisterCheckNextGeneration");
162177

@@ -197,7 +212,7 @@ Y_UNIT_TEST_SUITE(TRegisterCheckTest) {
197212
UNIT_ASSERT_C(status.IsSuccess(), status.GetIssues().ToString());
198213
}
199214

200-
Y_UNIT_TEST(ShouldNotRegisterCheckPrevGeneration)
215+
Y_UNIT_TEST_F(ShouldNotRegisterCheckPrevGeneration, TFixture)
201216
{
202217
auto connection = MakeConnection("ShouldNotRegisterCheckPrevGeneration");
203218

@@ -239,7 +254,7 @@ Y_UNIT_TEST_SUITE(TRegisterCheckTest) {
239254
UNIT_ASSERT(!status.IsSuccess());
240255
}
241256

242-
Y_UNIT_TEST(ShouldNotRegisterCheckPrevGeneration2)
257+
Y_UNIT_TEST_F(ShouldNotRegisterCheckPrevGeneration2, TFixture)
243258
{
244259
auto connection = MakeConnection("ShouldNotRegisterCheckPrevGeneration2");
245260

@@ -281,7 +296,7 @@ Y_UNIT_TEST_SUITE(TRegisterCheckTest) {
281296
UNIT_ASSERT(!status.IsSuccess());
282297
}
283298

284-
Y_UNIT_TEST(ShouldRegisterCheckNewGenerationAndTransact)
299+
Y_UNIT_TEST_F(ShouldRegisterCheckNewGenerationAndTransact, TFixture)
285300
{
286301
auto connection = MakeConnection("ShouldRegisterCheckNewGenerationAndTransact");
287302

@@ -313,7 +328,7 @@ Y_UNIT_TEST_SUITE(TRegisterCheckTest) {
313328
UNIT_ASSERT_C(status.IsSuccess(), status.GetIssues().ToString());
314329
}
315330

316-
Y_UNIT_TEST(ShouldRegisterCheckSameGenerationAndTransact)
331+
Y_UNIT_TEST_F(ShouldRegisterCheckSameGenerationAndTransact, TFixture)
317332
{
318333
auto connection = MakeConnection("ShouldRegisterCheckNewGenerationAndTransact");
319334

@@ -368,7 +383,7 @@ Y_UNIT_TEST_SUITE(TRegisterCheckTest) {
368383

369384
// most of logic is tested inside libs/storage, thus we don't test here again
370385
Y_UNIT_TEST_SUITE(TCheckGenerationTest) {
371-
Y_UNIT_TEST(ShouldRollbackTransactionWhenCheckFails)
386+
Y_UNIT_TEST_F(ShouldRollbackTransactionWhenCheckFails, TFixture)
372387
{
373388
auto connection = MakeConnection("ShouldRollbackTransactionWhenCheckFails");
374389

@@ -410,9 +425,9 @@ Y_UNIT_TEST_SUITE(TCheckGenerationTest) {
410425
UNIT_ASSERT(!status.IsSuccess());
411426
}
412427

413-
Y_UNIT_TEST(ShouldRollbackTransactionWhenCheckFails2)
428+
Y_UNIT_TEST_F(ShouldRollbackTransactionWhenCheckFails2, TFixture)
414429
{
415-
auto connection = MakeConnection("ShouldRollbackTransactionWhenCheckFails");
430+
auto connection = MakeConnection("ShouldRollbackTransactionWhenCheckFails2");
416431

417432
auto future = connection->TableClient.RetryOperation(
418433
[prefix = connection->TablePathPrefix] (TSession session) {

0 commit comments

Comments
 (0)