Skip to content

Commit

Permalink
test: Add Integration Test 3_4 (#124)
Browse files Browse the repository at this point in the history
* Added test_writerFailWithNoTransaction

Co-authored-by: Colin <ColinKYuen@users.noreply.github.com>
  • Loading branch information
2 people authored and yanw-bq committed May 1, 2023
1 parent 1c14e60 commit 1d4b213
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions integration/failover_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit 1d4b213

Please sign in to comment.