|
| 1 | +#include <util/system/env.h> |
| 2 | +#include <library/cpp/testing/unittest/registar.h> |
| 3 | + |
| 4 | +#include <ydb/public/sdk/cpp/client/ydb_driver/driver.h> |
| 5 | +#include <ydb/public/sdk/cpp/client/ydb_table/table.h> |
| 6 | +#include <ydb/public/sdk/cpp/client/ydb_proto/accessor.h> |
| 7 | +#include <ydb/public/sdk/cpp/client/draft/ydb_scripting.h> |
| 8 | + |
| 9 | +#include <library/cpp/threading/local_executor/local_executor.h> |
| 10 | + |
| 11 | +using namespace NYdb; |
| 12 | +using namespace NYdb::NTable; |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +std::pair<ui64, Ydb::ResultSet> DoRead(TSession& s, const TString& table) { |
| 17 | + auto res = s.ExecuteDataQuery( |
| 18 | + Sprintf("SELECT * FROM `/local/%s`; SELECT COUNT(*) AS __count FROM `/local/%s`;", |
| 19 | + table.data(), table.data()), TTxControl::BeginTx().CommitTx()).GetValueSync(); |
| 20 | + UNIT_ASSERT_C(res.IsSuccess(), res.GetIssues().ToString()); |
| 21 | + auto rs = NYdb::TResultSetParser(res.GetResultSet(1)); |
| 22 | + UNIT_ASSERT(rs.TryNextRow()); |
| 23 | + auto count = rs.ColumnParser("__count").GetUint64(); |
| 24 | + |
| 25 | + const auto proto = NYdb::TProtoAccessor::GetProto(res.GetResultSet(0)); |
| 26 | + return {count, proto}; |
| 27 | +} |
| 28 | + |
| 29 | +} // namespace |
| 30 | + |
| 31 | +Y_UNIT_TEST_SUITE(Replication) |
| 32 | +{ |
| 33 | + Y_UNIT_TEST(Types) |
| 34 | + { |
| 35 | + TString connectionString = GetEnv("YDB_ENDPOINT") + "/?database=" + GetEnv("YDB_DATABASE"); |
| 36 | + auto config = TDriverConfig(connectionString); |
| 37 | + auto driver = TDriver(config); |
| 38 | + auto tableClient = TTableClient(driver); |
| 39 | + auto session = tableClient.GetSession().GetValueSync().GetSession(); |
| 40 | + |
| 41 | + { |
| 42 | + auto res = session.ExecuteSchemeQuery(R"( |
| 43 | + CREATE TABLE `/local/ProducerUuidValue` ( |
| 44 | + Key Uint32, |
| 45 | + v01 Uuid, |
| 46 | + v02 Uuid NOT NULL, |
| 47 | + v03 Double, |
| 48 | + PRIMARY KEY (Key) |
| 49 | + ); |
| 50 | + )").GetValueSync(); |
| 51 | + UNIT_ASSERT_C(res.IsSuccess(), res.GetIssues().ToString()); |
| 52 | + } |
| 53 | + |
| 54 | + { |
| 55 | + auto sessionResult = tableClient.GetSession().GetValueSync(); |
| 56 | + UNIT_ASSERT_C(sessionResult.IsSuccess(), sessionResult.GetIssues().ToString()); |
| 57 | + auto s = sessionResult.GetSession(); |
| 58 | + |
| 59 | + { |
| 60 | + const TString query = "UPSERT INTO ProducerUuidValue (Key,v01,v02,v03) VALUES" |
| 61 | + "(1, " |
| 62 | + "CAST(\"5b99a330-04ef-4f1a-9b64-ba6d5f44ea01\" as Uuid), " |
| 63 | + "UNWRAP(CAST(\"5b99a330-04ef-4f1a-9b64-ba6d5f44ea02\" as Uuid)), " |
| 64 | + "CAST(\"311111111113.222222223\" as Double) " |
| 65 | + ");"; |
| 66 | + auto res = s.ExecuteDataQuery(query, TTxControl::BeginTx().CommitTx()).GetValueSync(); |
| 67 | + UNIT_ASSERT_C(res.IsSuccess(), res.GetIssues().ToString()); |
| 68 | + } |
| 69 | + |
| 70 | + { |
| 71 | + const TString query = Sprintf("CREATE ASYNC REPLICATION `replication` FOR" |
| 72 | + "`ProducerUuidValue` AS `ConsumerUuidValue`" |
| 73 | + "WITH (" |
| 74 | + "CONNECTION_STRING = 'grpc://%s'," |
| 75 | + "TOKEN = 'root@builtin'" |
| 76 | + ");", connectionString.data()); |
| 77 | + auto res = s.ExecuteSchemeQuery(query).GetValueSync(); |
| 78 | + UNIT_ASSERT_C(res.IsSuccess(), res.GetIssues().ToString()); |
| 79 | + } |
| 80 | + // TODO: Make CREATE ASYNC REPLICATION to be a sync call |
| 81 | + Sleep(TDuration::Seconds(10)); |
| 82 | + } |
| 83 | + |
| 84 | + NYdb::NTable::TExecDataQuerySettings execSettings; |
| 85 | + execSettings.KeepInQueryCache(true); |
| 86 | + |
| 87 | + auto sessionResult = tableClient.GetSession().GetValueSync(); |
| 88 | + UNIT_ASSERT_C(sessionResult.IsSuccess(), sessionResult.GetIssues().ToString()); |
| 89 | + |
| 90 | + auto s = sessionResult.GetSession(); |
| 91 | + TUuidValue expectedV1("5b99a330-04ef-4f1a-9b64-ba6d5f44ea01"); |
| 92 | + TUuidValue expectedV2("5b99a330-04ef-4f1a-9b64-ba6d5f44ea02"); |
| 93 | + double expectedV3 = 311111111113.222222223; |
| 94 | + ui32 attempt = 10; |
| 95 | + while (--attempt) { |
| 96 | + auto res = DoRead(s, "ConsumerUuidValue"); |
| 97 | + if (res.first == 1) { |
| 98 | + const Ydb::ResultSet& proto = res.second; |
| 99 | + UNIT_ASSERT_VALUES_EQUAL(proto.rows(0).items(0).uint32_value(), 1); |
| 100 | + UNIT_ASSERT_VALUES_EQUAL(proto.rows(0).items(1).low_128(), expectedV1.Buf_.Halfs[0]); |
| 101 | + UNIT_ASSERT_VALUES_EQUAL(proto.rows(0).items(1).high_128(), expectedV1.Buf_.Halfs[1]); |
| 102 | + UNIT_ASSERT_VALUES_EQUAL(proto.rows(0).items(2).low_128(), expectedV2.Buf_.Halfs[0]); |
| 103 | + UNIT_ASSERT_VALUES_EQUAL(proto.rows(0).items(2).high_128(), expectedV2.Buf_.Halfs[1]); |
| 104 | + UNIT_ASSERT_DOUBLES_EQUAL(proto.rows(0).items(3).double_value(), expectedV3, 0.0001); |
| 105 | + break; |
| 106 | + } |
| 107 | + Sleep(TDuration::Seconds(1)); |
| 108 | + } |
| 109 | + |
| 110 | + UNIT_ASSERT_C(attempt, "Unable to wait replication result"); |
| 111 | + } |
| 112 | +} |
| 113 | + |
0 commit comments