From 1d4b213c6007019290737d40d51b9a1f6c3881f9 Mon Sep 17 00:00:00 2001 From: Colin <70862826+ColinKYuen@users.noreply.github.com> Date: Tue, 5 Apr 2022 14:27:30 -0700 Subject: [PATCH] test: Add Integration Test 3_4 (#124) * Added test_writerFailWithNoTransaction Co-authored-by: Colin --- integration/failover_integration_test.cc | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/integration/failover_integration_test.cc b/integration/failover_integration_test.cc index b71e11870..ebce1e9e5 100644 --- a/integration/failover_integration_test.cc +++ b/integration/failover_integration_test.cc @@ -570,6 +570,68 @@ TEST_F(FailoverIntegrationTest, test_writerFailWithinTransaction_startTransactio EXPECT_EQ(SQL_SUCCESS, SQLDisconnect(dbc)); } +/* Writer fails within NO transaction. */ +TEST_F(FailoverIntegrationTest, test_writerFailWithNoTransaction) { + auto initial_writer = retrieve_writer_endpoint(rds_client, cluster_id, DB_CONN_STR_SUFFIX); + auto initial_writer_id = initial_writer.first; + auto initial_writer_endpoint = initial_writer.second; + + build_connection_string(conn_in, dsn, user, pwd, initial_writer_endpoint, MYSQL_PORT, db); + SQLCHAR conn_out[4096], sqlstate[6], message[SQL_MAX_MESSAGE_LENGTH]; + SQLINTEGER native_error; + SQLSMALLINT len, length; + + EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, conn_in, SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT)); + + // Set-up tests + SQLHSTMT handle; + SQLSMALLINT stmt_length; + SQLCHAR stmt_sqlstate[6]; + EXPECT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_STMT, dbc, &handle)); + const auto drop_table_query = (SQLCHAR*)"DROP TABLE IF EXISTS test3_4"; // Setting up tables + const auto setup_table_query = (SQLCHAR*)"CREATE TABLE test3_4 (id int not null primary key, test3_2_field varchar(255) not null)"; + + // Execute setup query + EXPECT_EQ(SQL_SUCCESS, SQLExecDirect(handle, drop_table_query, SQL_NTS)); + EXPECT_EQ(SQL_SUCCESS, SQLExecDirect(handle, setup_table_query, SQL_NTS)); + + // Have something inserted into table + EXPECT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_STMT, dbc, &handle)); + const auto insert_query_A = (SQLCHAR*)"INSERT INTO test3_4 VALUES (1, 'test field string 1')"; + EXPECT_EQ(SQL_SUCCESS, SQLExecDirect(handle, insert_query_A, SQL_NTS)); + + failover_cluster_and_wait_until_writer_changed(rds_client, cluster_id, initial_writer_id); + + // Query expected to fail and rollback things in transaction + const auto insert_query_B = (SQLCHAR*)"INSERT INTO test3_4 VALUES (2, 'test field string 2')"; + + // Execute query expecting failure & rollback insert 2 + EXPECT_EQ(SQL_ERROR, SQLExecDirect(handle, insert_query_B, SQL_NTS)); // Triggers failover + + // Check state + EXPECT_EQ(SQL_SUCCESS, SQLError(env, dbc, handle, stmt_sqlstate, &native_error, message, SQL_MAX_MESSAGE_LENGTH - 1, &stmt_length)); + const std::string state = (char*)stmt_sqlstate; + const std::string expected = "08S02"; + EXPECT_EQ(expected, state); + + // Query new ID after failover + std::string current_connection_id = query_instance_id(dbc); + + // Check if current connection is a new writer + EXPECT_TRUE(is_DB_instance_writer(rds_client, cluster_id, current_connection_id)); + EXPECT_NE(current_connection_id, initial_writer_id); + + // ID 1 should have 1 row + EXPECT_EQ(1, count_table_rows(handle, "test3_4 WHERE id = 1")); + + // ID 2 should have NO rows + EXPECT_EQ(0, count_table_rows(handle, "test3_4 WHERE id = 2")); + + // Clean up test + EXPECT_EQ(SQL_SUCCESS, SQLExecDirect(handle, drop_table_query, SQL_NTS)); + EXPECT_EQ(SQL_SUCCESS, SQLDisconnect(dbc)); +} + /* Pooled connection tests. */ /* Writer connection failover within the connection pool. */