|
4 | 4 |
|
5 | 5 | #include <ydb/public/api/grpc/ydb_table_v1.grpc.pb.h>
|
6 | 6 |
|
| 7 | +#include <ydb/public/lib/ut_helpers/ut_helpers_query.h> |
| 8 | + |
7 | 9 | using namespace NYdb;
|
8 | 10 | using namespace NYdb::NTable;
|
9 | 11 |
|
| 12 | +namespace { |
| 13 | + |
| 14 | +void CreateTestTable(NYdb::TDriver& driver) { |
| 15 | + NYdb::NTable::TTableClient client(driver); |
| 16 | + auto sessionResponse = client.GetSession().ExtractValueSync(); |
| 17 | + UNIT_ASSERT(sessionResponse.IsSuccess()); |
| 18 | + auto session = sessionResponse.GetSession(); |
| 19 | + auto result = session.ExecuteSchemeQuery(R"___( |
| 20 | + CREATE TABLE `Root/Test` ( |
| 21 | + Key Uint32, |
| 22 | + Value String, |
| 23 | + PRIMARY KEY (Key) |
| 24 | + ); |
| 25 | + )___").ExtractValueSync(); |
| 26 | + UNIT_ASSERT(result.IsSuccess()); |
| 27 | + UNIT_ASSERT_VALUES_EQUAL(client.GetActiveSessionCount(), 1); |
| 28 | +} |
| 29 | + |
| 30 | +TString WarmPoolCreateSession(NYdb::NQuery::TQueryClient& client) { |
| 31 | + TString sessionId; |
| 32 | + auto sessionResponse = client.GetSession().ExtractValueSync(); |
| 33 | + UNIT_ASSERT(sessionResponse.IsSuccess()); |
| 34 | + auto session = sessionResponse.GetSession(); |
| 35 | + sessionId = session.GetId(); |
| 36 | + auto res = session.ExecuteQuery("SELECT * FROM `Root/Test`", NYdb::NQuery::TTxControl::BeginTx().CommitTx()).GetValueSync(); |
| 37 | + |
| 38 | + UNIT_ASSERT_VALUES_EQUAL_C(res.GetStatus(), EStatus::SUCCESS, res.GetIssues().ToString()); |
| 39 | + |
| 40 | + TResultSetParser resultSet(res.GetResultSetParser(0)); |
| 41 | + UNIT_ASSERT_VALUES_EQUAL(resultSet.ColumnsCount(), 2); |
| 42 | + |
| 43 | + return sessionId; |
| 44 | +} |
| 45 | + |
| 46 | +void WaitForSessionsInPool(NYdb::NQuery::TQueryClient& client, i64 expected) { |
| 47 | + int attempt = 10; |
| 48 | + while (attempt--) { |
| 49 | + if (client.GetCurrentPoolSize() == expected) |
| 50 | + break; |
| 51 | + Sleep(TDuration::MilliSeconds(100)); |
| 52 | + } |
| 53 | + UNIT_ASSERT_VALUES_EQUAL(client.GetCurrentPoolSize(), expected); |
| 54 | +} |
| 55 | + |
| 56 | +} |
| 57 | + |
10 | 58 | Y_UNIT_TEST_SUITE(YdbSdkSessions) {
|
11 | 59 | Y_UNIT_TEST(TestSessionPool) {
|
12 | 60 | TKikimrWithGrpcAndRootSchema server;
|
@@ -128,6 +176,108 @@ Y_UNIT_TEST_SUITE(YdbSdkSessions) {
|
128 | 176 | driver.Stop(true);
|
129 | 177 | }
|
130 | 178 |
|
| 179 | + Y_UNIT_TEST(TestSdkFreeSessionAfterBadSessionQueryService) { |
| 180 | + TKikimrWithGrpcAndRootSchema server; |
| 181 | + ui16 grpc = server.GetPort(); |
| 182 | + |
| 183 | + TString location = TStringBuilder() << "localhost:" << grpc; |
| 184 | + auto clientConfig = NGRpcProxy::TGRpcClientConfig(location); |
| 185 | + |
| 186 | + auto driver = NYdb::TDriver( |
| 187 | + TDriverConfig() |
| 188 | + .SetEndpoint(location)); |
| 189 | + |
| 190 | + CreateTestTable(driver); |
| 191 | + |
| 192 | + NYdb::NQuery::TQueryClient client(driver); |
| 193 | + TString sessionId = WarmPoolCreateSession(client); |
| 194 | + WaitForSessionsInPool(client, 1); |
| 195 | + |
| 196 | + bool allDoneOk = true; |
| 197 | + NTestHelpers::CheckDelete(clientConfig, sessionId, Ydb::StatusIds::SUCCESS, allDoneOk); |
| 198 | + UNIT_ASSERT(allDoneOk); |
| 199 | + |
| 200 | + { |
| 201 | + auto sessionResponse = client.GetSession().ExtractValueSync(); |
| 202 | + UNIT_ASSERT(sessionResponse.IsSuccess()); |
| 203 | + auto session = sessionResponse.GetSession(); |
| 204 | + UNIT_ASSERT_VALUES_EQUAL(session.GetId(), sessionId); |
| 205 | + |
| 206 | + auto res = session.ExecuteQuery("SELECT * FROM `Root/Test`", NYdb::NQuery::TTxControl::BeginTx().CommitTx()).GetValueSync(); |
| 207 | + |
| 208 | + UNIT_ASSERT_VALUES_EQUAL_C(res.GetStatus(), EStatus::BAD_SESSION, res.GetIssues().ToString()); |
| 209 | + } |
| 210 | + |
| 211 | + WaitForSessionsInPool(client, 0); |
| 212 | + |
| 213 | + { |
| 214 | + auto sessionResponse = client.GetSession().ExtractValueSync(); |
| 215 | + UNIT_ASSERT(sessionResponse.IsSuccess()); |
| 216 | + auto session = sessionResponse.GetSession(); |
| 217 | + UNIT_ASSERT_VALUES_UNEQUAL(session.GetId(), sessionId); |
| 218 | + auto res = session.ExecuteQuery("SELECT * FROM `Root/Test`", NYdb::NQuery::TTxControl::BeginTx().CommitTx()).GetValueSync(); |
| 219 | + |
| 220 | + UNIT_ASSERT_VALUES_EQUAL_C(res.GetStatus(), EStatus::SUCCESS, res.GetIssues().ToString()); |
| 221 | + } |
| 222 | + |
| 223 | + WaitForSessionsInPool(client, 1); |
| 224 | + |
| 225 | + driver.Stop(true); |
| 226 | + } |
| 227 | + |
| 228 | + Y_UNIT_TEST(TestSdkFreeSessionAfterBadSessionQueryServiceStreamCall) { |
| 229 | + TKikimrWithGrpcAndRootSchema server; |
| 230 | + ui16 grpc = server.GetPort(); |
| 231 | + |
| 232 | + TString location = TStringBuilder() << "localhost:" << grpc; |
| 233 | + auto clientConfig = NGRpcProxy::TGRpcClientConfig(location); |
| 234 | + |
| 235 | + auto driver = NYdb::TDriver( |
| 236 | + TDriverConfig() |
| 237 | + .SetEndpoint(location)); |
| 238 | + |
| 239 | + CreateTestTable(driver); |
| 240 | + |
| 241 | + NYdb::NQuery::TQueryClient client(driver); |
| 242 | + TString sessionId = WarmPoolCreateSession(client); |
| 243 | + WaitForSessionsInPool(client, 1); |
| 244 | + |
| 245 | + bool allDoneOk = true; |
| 246 | + NTestHelpers::CheckDelete(clientConfig, sessionId, Ydb::StatusIds::SUCCESS, allDoneOk); |
| 247 | + UNIT_ASSERT(allDoneOk); |
| 248 | + |
| 249 | + { |
| 250 | + auto sessionResponse = client.GetSession().ExtractValueSync(); |
| 251 | + UNIT_ASSERT(sessionResponse.IsSuccess()); |
| 252 | + auto session = sessionResponse.GetSession(); |
| 253 | + UNIT_ASSERT_VALUES_EQUAL(session.GetId(), sessionId); |
| 254 | + |
| 255 | + auto it = session.StreamExecuteQuery("SELECT * FROM `Root/Test`", NYdb::NQuery::TTxControl::BeginTx().CommitTx()).GetValueSync(); |
| 256 | + |
| 257 | + UNIT_ASSERT_VALUES_EQUAL_C(it.GetStatus(), EStatus::SUCCESS, it.GetIssues().ToString()); |
| 258 | + |
| 259 | + auto res = it.ReadNext().GetValueSync(); |
| 260 | + UNIT_ASSERT_VALUES_EQUAL_C(res.GetStatus(), EStatus::BAD_SESSION, res.GetIssues().ToString()); |
| 261 | + } |
| 262 | + |
| 263 | + WaitForSessionsInPool(client, 0); |
| 264 | + |
| 265 | + { |
| 266 | + auto sessionResponse = client.GetSession().ExtractValueSync(); |
| 267 | + UNIT_ASSERT(sessionResponse.IsSuccess()); |
| 268 | + auto session = sessionResponse.GetSession(); |
| 269 | + UNIT_ASSERT_VALUES_UNEQUAL(session.GetId(), sessionId); |
| 270 | + |
| 271 | + auto res = session.ExecuteQuery("SELECT * FROM `Root/Test`", NYdb::NQuery::TTxControl::BeginTx().CommitTx()).GetValueSync(); |
| 272 | + |
| 273 | + UNIT_ASSERT_VALUES_EQUAL_C(res.GetStatus(), EStatus::SUCCESS, res.GetIssues().ToString()); |
| 274 | + } |
| 275 | + |
| 276 | + WaitForSessionsInPool(client, 1); |
| 277 | + |
| 278 | + driver.Stop(true); |
| 279 | + } |
| 280 | + |
131 | 281 | Y_UNIT_TEST(TestActiveSessionCountAfterTransportError) {
|
132 | 282 | TKikimrWithGrpcAndRootSchema server;
|
133 | 283 | ui16 grpc = server.GetPort();
|
|
0 commit comments