isapego commented on code in PR #6352:
URL: https://github.com/apache/ignite-3/pull/6352#discussion_r2254195549


##########
modules/platforms/cpp/ignite/client/transaction/transaction_options.h:
##########
@@ -0,0 +1,59 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+/**
+ * Transaction options.
+ */
+class transaction_options {
+public:
+    /**
+     * Transaction timeout.
+     * @return Transaction timeout in milliseconds.
+     */
+    [[nodiscard]] std::int64_t get_timeout_millis() const { return 
m_timeout_millis; }
+
+    /**
+     * Sets new value for transaction timeout.
+     * @param timeout_millis Transaction timeout in milliseconds.
+     * @return Reference to this for chaining.
+     */
+    transaction_options & set_timeout_millis(std::int64_t timeout_millis) {
+        m_timeout_millis = timeout_millis;
+        return *this;
+    }
+
+    /**
+     * Transaction allow only read operations.
+     * @return True if only read operation are allowed false otherwise.
+     */
+    [[nodiscard]] bool is_read_only() const { return m_read_only; }
+
+    /**
+     * Change transaction to be read-only or read-write.
+     * @param read_only True if new type should read-only, false if read-write.
+     * @return Reference to this for chaining.
+     */
+    transaction_options & set_read_only(bool read_only) {
+        m_read_only = read_only;
+        return *this;
+    }
+private:
+    std::int64_t m_timeout_millis = 0;
+    bool m_read_only = false;
+};

Review Comment:
   ```suggestion
   class transaction_options {
   public:
       /**
        * Transaction timeout.
        *
        * @return Transaction timeout in milliseconds.
        */
       [[nodiscard]] std::int64_t get_timeout_millis() const { return 
m_timeout_millis; }
   
       /**
        * Sets new value for transaction timeout.
        *
        * @param timeout_millis Transaction timeout in milliseconds.
        * @return Reference to this for chaining.
        */
       transaction_options & set_timeout_millis(std::int64_t timeout_millis) {
           m_timeout_millis = timeout_millis;
           return *this;
       }
   
       /**
        * Transaction allow only read operations.
        *
        * @return True if only read operation are allowed false otherwise.
        */
       [[nodiscard]] bool is_read_only() const { return m_read_only; }
   
       /**
        * Change transaction to be read-only or read-write.
        *
        * @param read_only True if new type should read-only, false if 
read-write.
        * @return Reference to this for chaining.
        */
       transaction_options & set_read_only(bool read_only) {
           m_read_only = read_only;
           return *this;
       }
   private:
       /** Timeout. */
       std::int64_t m_timeout_millis = 0;
       
       /** Read-only mode. */
       bool m_read_only = false;
   };
   ```



##########
modules/platforms/cpp/tests/client-test/transactions_test.cpp:
##########
@@ -434,3 +434,55 @@ TEST_F(transactions_test, record_view_remove_all_exact) {
     auto values2 = record_view.remove_all(nullptr, {value0});
     ASSERT_TRUE(values2.empty());
 }
+
+TEST_F(transactions_test, tx_successful_when_timeout_does_not_exceed) {
+    auto record_view = 
m_client.get_tables().get_table(TABLE_1)->get_record_binary_view();
+
+    int64_t key = 42;
+    std::string val = "Lorem ipsum";
+
+    auto tx_opts = 
transaction_options().set_timeout_millis(10'000L).set_read_only(false);
+    auto tx = m_client.get_transactions().begin(tx_opts);
+
+    record_view.insert(&tx, get_tuple(key, val));
+
+    tx.commit();
+
+    auto value = record_view.get(nullptr, get_tuple(42));
+
+    ASSERT_TRUE(value.has_value());
+    EXPECT_EQ(key, value->get<int64_t>(KEY_COLUMN));
+    EXPECT_EQ(val, value->get<std::string>(VAL_COLUMN));
+}
+
+
+TEST_F(transactions_test, tx_failed_when_timeout_exceeds) {
+    auto record_view = 
m_client.get_tables().get_table(TABLE_1)->get_record_binary_view();
+
+    auto timeout_ms = 1'000L;
+
+    int64_t key = 42;
+    std::string val = "Lorem ipsum";
+    auto tx_opts = transaction_options()
+        .set_timeout_millis(timeout_ms)
+        .set_read_only(false);
+
+    auto tx = m_client.get_transactions().begin(tx_opts);
+
+    record_view.insert(&tx, get_tuple(key, val));
+
+    std::this_thread::sleep_for(std::chrono::milliseconds{timeout_ms * 2});
+
+    EXPECT_THROW(
+        {
+            try {
+                // TODO change to check tx.commit when IGNITE-24233 is 
implemented
+                // tx.commit();
+                auto rec = record_view.get(&tx, get_tuple(key));
+            } catch (const ignite_error& e) {
+                EXPECT_EQ(e.get_status_code(), 
error::code::TX_ALREADY_FINISHED_WITH_TIMEOUT);
+                throw;
+            }
+        },
+        ignite_error);
+}

Review Comment:
   ```suggestion
   }
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to