From 5035caa4a584eaca18435b18ee8acf1962c27676 Mon Sep 17 00:00:00 2001 From: joyc-bq Date: Mon, 21 Oct 2024 14:43:43 -0400 Subject: [PATCH] enable tests --- .../reports/AURORA-MYSQL_instance-1.html | 260 ------- .../reports/AURORA-MYSQL_instance-2.html | 260 ------- .../tests/read_write_splitting.test.ts | 730 ++++++++++-------- 3 files changed, 396 insertions(+), 854 deletions(-) delete mode 100644 tests/integration/container/reports/AURORA-MYSQL_instance-1.html delete mode 100644 tests/integration/container/reports/AURORA-MYSQL_instance-2.html diff --git a/tests/integration/container/reports/AURORA-MYSQL_instance-1.html b/tests/integration/container/reports/AURORA-MYSQL_instance-1.html deleted file mode 100644 index d96a4a76..00000000 --- a/tests/integration/container/reports/AURORA-MYSQL_instance-1.html +++ /dev/null @@ -1,260 +0,0 @@ -Test Report

Test Report

Started: 2024-10-21 18:11:49
Suites (1)
0 passed
0 failed
1 pending
Tests (12)
0 passed
0 failed
12 pending
aurora read write splitting
test connect to writer switch set read only
pending
0s
aurora read write splitting
test set read only false in read only transaction
pending
0s
aurora read write splitting
test set read only true in transaction
pending
0s
aurora read write splitting
test set read only all instances down
pending
0s
aurora read write splitting
test set read only all readers down
pending
0s
aurora read write splitting
test failover to new writer set read only true false
pending
0s
aurora read write splitting
test failover to new reader set read only false true
pending
0s
aurora read write splitting
test failover reader to writer set read only true false
pending
0s
aurora read write splitting
test pooled connection failover
pending
0s
aurora read write splitting
test set read only reuse cached connection
pending
0s
aurora read write splitting
test pooled connection failover failed
pending
0s
aurora read write splitting
test pooled connection failover in transaction
pending
0s
\ No newline at end of file diff --git a/tests/integration/container/reports/AURORA-MYSQL_instance-2.html b/tests/integration/container/reports/AURORA-MYSQL_instance-2.html deleted file mode 100644 index 297c1e67..00000000 --- a/tests/integration/container/reports/AURORA-MYSQL_instance-2.html +++ /dev/null @@ -1,260 +0,0 @@ -Test Report

Test Report

Started: 2024-10-21 18:12:43
Suites (1)
1 passed
0 failed
0 pending
Tests (12)
1 passed
0 failed
11 pending
aurora read write splitting
test connect to writer switch set read only
pending
0s
aurora read write splitting
test set read only false in read only transaction
pending
0s
aurora read write splitting
test set read only true in transaction
pending
0s
aurora read write splitting
test set read only all instances down
pending
0s
aurora read write splitting
test set read only all readers down
pending
0s
aurora read write splitting
test failover to new writer set read only true false
pending
0s
aurora read write splitting
test failover to new reader set read only false true
pending
0s
aurora read write splitting
test failover reader to writer set read only true false
pending
0s
aurora read write splitting
test pooled connection failover
pending
0s
aurora read write splitting
test set read only reuse cached connection
passed
1.438s
aurora read write splitting
test pooled connection failover failed
pending
0s
aurora read write splitting
test pooled connection failover in transaction
pending
0s
\ No newline at end of file diff --git a/tests/integration/container/tests/read_write_splitting.test.ts b/tests/integration/container/tests/read_write_splitting.test.ts index d006d491..19fb73f2 100644 --- a/tests/integration/container/tests/read_write_splitting.test.ts +++ b/tests/integration/container/tests/read_write_splitting.test.ts @@ -103,357 +103,406 @@ describe("aurora read write splitting", () => { logger.info(`Test finished: ${expect.getState().currentTestName}`); }, 1320000); - it.skip("test connect to writer switch set read only", async () => { - const config = await initDefaultConfig(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort, false); - client = initClientFunc(config); - - client.on("error", (error: any) => { - logger.debug(`event emitter threw error: ${error.message}`); - logger.debug(error.stack); - }); - - await client.connect(); - const initialWriterId = await auroraTestUtility.queryInstanceId(client); - expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); - - await client.setReadOnly(true); - const readerId = await auroraTestUtility.queryInstanceId(client); - expect(readerId).not.toBe(initialWriterId); - - await client.setReadOnly(true); - const currentId0 = await auroraTestUtility.queryInstanceId(client); - expect(currentId0).toStrictEqual(readerId); - - await client.setReadOnly(false); - const currentId1 = await auroraTestUtility.queryInstanceId(client); - expect(currentId1).toStrictEqual(initialWriterId); - - await client.setReadOnly(false); - const currentId2 = await auroraTestUtility.queryInstanceId(client); - expect(currentId2).toStrictEqual(initialWriterId); - - await client.setReadOnly(true); - const currentId3 = await auroraTestUtility.queryInstanceId(client); - expect(currentId3).toStrictEqual(readerId); - expect(await auroraTestUtility.isDbInstanceWriter(currentId3)).toStrictEqual(false); - }, 1320000); + itIf( + "test connect to writer switch set read only", + async () => { + const config = await initDefaultConfig(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort, false); + client = initClientFunc(config); - it.skip("test set read only false in read only transaction", async () => { - const config = await initDefaultConfig(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort, false); - client = initClientFunc(config); + client.on("error", (error: any) => { + logger.debug(`event emitter threw error: ${error.message}`); + logger.debug(error.stack); + }); - client.on("error", (error: any) => { - logger.debug(`event emitter threw error: ${error.message}`); - logger.debug(error.stack); - }); + await client.connect(); + const initialWriterId = await auroraTestUtility.queryInstanceId(client); + expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); - await client.connect(); - const initialWriterId = await auroraTestUtility.queryInstanceId(client); - expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); + await client.setReadOnly(true); + const readerId = await auroraTestUtility.queryInstanceId(client); + expect(readerId).not.toBe(initialWriterId); - await client.setReadOnly(true); - const initialReaderId = await auroraTestUtility.queryInstanceId(client); - expect(initialReaderId).not.toBe(initialWriterId); + await client.setReadOnly(true); + const currentId0 = await auroraTestUtility.queryInstanceId(client); + expect(currentId0).toStrictEqual(readerId); - await DriverHelper.executeQuery(env.engine, client, "START TRANSACTION READ ONLY"); // start transaction - await DriverHelper.executeQuery(env.engine, client, "SELECT 1"); + await client.setReadOnly(false); + const currentId1 = await auroraTestUtility.queryInstanceId(client); + expect(currentId1).toStrictEqual(initialWriterId); - try { await client.setReadOnly(false); - } catch (error: any) { - logger.debug(error.message); - if (!(error instanceof AwsWrapperError)) { - throw new Error("Resulting error type incorrect"); - } - } - const currentConnectionId0 = await auroraTestUtility.queryInstanceId(client); - expect(currentConnectionId0).toStrictEqual(initialReaderId); + const currentId2 = await auroraTestUtility.queryInstanceId(client); + expect(currentId2).toStrictEqual(initialWriterId); - await DriverHelper.executeQuery(env.engine, client, "COMMIT"); + await client.setReadOnly(true); + const currentId3 = await auroraTestUtility.queryInstanceId(client); + expect(currentId3).toStrictEqual(readerId); + expect(await auroraTestUtility.isDbInstanceWriter(currentId3)).toStrictEqual(false); + }, + 1320000 + ); - await client.setReadOnly(false); - const currentConnectionId1 = await auroraTestUtility.queryInstanceId(client); - expect(currentConnectionId1).toStrictEqual(initialWriterId); - }, 1320000); + itIf( + "test set read only false in read only transaction", + async () => { + const config = await initDefaultConfig(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort, false); + client = initClientFunc(config); + + client.on("error", (error: any) => { + logger.debug(`event emitter threw error: ${error.message}`); + logger.debug(error.stack); + }); - it.skip("test set read only true in transaction", async () => { - const config = await initDefaultConfig(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort, false); - client = initClientFunc(config); + await client.connect(); + const initialWriterId = await auroraTestUtility.queryInstanceId(client); + expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); - client.on("error", (error: any) => { - logger.debug(`event emitter threw error: ${error.message}`); - logger.debug(error.stack); - }); + await client.setReadOnly(true); + const initialReaderId = await auroraTestUtility.queryInstanceId(client); + expect(initialReaderId).not.toBe(initialWriterId); - await client.connect(); - const initialWriterId = await auroraTestUtility.queryInstanceId(client); - expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); + await DriverHelper.executeQuery(env.engine, client, "START TRANSACTION READ ONLY"); // start transaction + await DriverHelper.executeQuery(env.engine, client, "SELECT 1"); - await DriverHelper.executeQuery(env.engine, client, "DROP TABLE IF EXISTS test3_3"); - await DriverHelper.executeQuery(env.engine, client, "CREATE TABLE test3_3 (id int not null primary key, test3_3_field varchar(255) not null)"); + try { + await client.setReadOnly(false); + } catch (error: any) { + logger.debug(error.message); + if (!(error instanceof AwsWrapperError)) { + throw new Error("Resulting error type incorrect"); + } + } + const currentConnectionId0 = await auroraTestUtility.queryInstanceId(client); + expect(currentConnectionId0).toStrictEqual(initialReaderId); - await DriverHelper.executeQuery(env.engine, client, "START TRANSACTION"); // start transaction - await DriverHelper.executeQuery(env.engine, client, "INSERT INTO test3_3 VALUES (1, 'test field string 1')"); + await DriverHelper.executeQuery(env.engine, client, "COMMIT"); - await client.setReadOnly(true); - const currentReaderId = await auroraTestUtility.queryInstanceId(client); - expect(currentReaderId).toStrictEqual(initialWriterId); + await client.setReadOnly(false); + const currentConnectionId1 = await auroraTestUtility.queryInstanceId(client); + expect(currentConnectionId1).toStrictEqual(initialWriterId); + }, + 1320000 + ); - await DriverHelper.executeQuery(env.engine, client, "COMMIT"); + itIf( + "test set read only true in transaction", + async () => { + const config = await initDefaultConfig(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort, false); + client = initClientFunc(config); - // Assert that 1 row has been inserted to the table. - const result = await DriverHelper.executeQuery(env.engine, client, "SELECT count(*) from test3_3"); - if (env.engine === DatabaseEngine.PG) { - expect((result as QueryResult).rows[0]["count"]).toBe("1"); - } else if (env.engine === DatabaseEngine.MYSQL) { - expect(JSON.parse(JSON.stringify(result))[0][0]["count(*)"]).toBe(1); - } - await client.setReadOnly(false); - const currentConnectionId1 = await auroraTestUtility.queryInstanceId(client); - expect(await auroraTestUtility.isDbInstanceWriter(currentConnectionId1)).toStrictEqual(true); - expect(currentConnectionId1).toStrictEqual(initialWriterId); + client.on("error", (error: any) => { + logger.debug(`event emitter threw error: ${error.message}`); + logger.debug(error.stack); + }); - await DriverHelper.executeQuery(env.engine, client, "DROP TABLE IF EXISTS test3_3"); - }, 1320000); + await client.connect(); + const initialWriterId = await auroraTestUtility.queryInstanceId(client); + expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); - it.skip("test set read only all instances down", async () => { - const config = await initDefaultConfig(env.proxyDatabaseInfo.writerInstanceEndpoint, env.proxyDatabaseInfo.instanceEndpointPort, true); - client = initClientFunc(config); + await DriverHelper.executeQuery(env.engine, client, "DROP TABLE IF EXISTS test3_3"); + await DriverHelper.executeQuery(env.engine, client, "CREATE TABLE test3_3 (id int not null primary key, test3_3_field varchar(255) not null)"); - client.on("error", (error: any) => { - logger.debug(`event emitter threw error: ${error.message}`); - logger.debug(error.stack); - }); + await DriverHelper.executeQuery(env.engine, client, "START TRANSACTION"); // start transaction + await DriverHelper.executeQuery(env.engine, client, "INSERT INTO test3_3 VALUES (1, 'test field string 1')"); - await client.connect(); - const initialWriterId = await auroraTestUtility.queryInstanceId(client); - expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); + await client.setReadOnly(true); + const currentReaderId = await auroraTestUtility.queryInstanceId(client); + expect(currentReaderId).toStrictEqual(initialWriterId); - await client.setReadOnly(true); - const currentReaderId0 = await auroraTestUtility.queryInstanceId(client); - expect(currentReaderId0).not.toBe(initialWriterId); + await DriverHelper.executeQuery(env.engine, client, "COMMIT"); - // Kill all instances - await ProxyHelper.disableAllConnectivity(env.engine); - await expect(async () => { + // Assert that 1 row has been inserted to the table. + const result = await DriverHelper.executeQuery(env.engine, client, "SELECT count(*) from test3_3"); + if (env.engine === DatabaseEngine.PG) { + expect((result as QueryResult).rows[0]["count"]).toBe("1"); + } else if (env.engine === DatabaseEngine.MYSQL) { + expect(JSON.parse(JSON.stringify(result))[0][0]["count(*)"]).toBe(1); + } await client.setReadOnly(false); - }).rejects.toThrow(); - }, 1320000); + const currentConnectionId1 = await auroraTestUtility.queryInstanceId(client); + expect(await auroraTestUtility.isDbInstanceWriter(currentConnectionId1)).toStrictEqual(true); + expect(currentConnectionId1).toStrictEqual(initialWriterId); - it.skip("test set read only all readers down", async () => { - const config = await initDefaultConfig(env.proxyDatabaseInfo.writerInstanceEndpoint, env.proxyDatabaseInfo.instanceEndpointPort, true); + await DriverHelper.executeQuery(env.engine, client, "DROP TABLE IF EXISTS test3_3"); + }, + 1320000 + ); - client = initClientFunc(config); + itIf( + "test set read only all instances down", + async () => { + const config = await initDefaultConfig(env.proxyDatabaseInfo.writerInstanceEndpoint, env.proxyDatabaseInfo.instanceEndpointPort, true); + client = initClientFunc(config); - client.on("error", (error: any) => { - logger.debug(`event emitter threw error: ${error.message}`); - logger.debug(error.stack); - }); + client.on("error", (error: any) => { + logger.debug(`event emitter threw error: ${error.message}`); + logger.debug(error.stack); + }); - await client.connect(); - const initialWriterId = await auroraTestUtility.queryInstanceId(client); - expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); + await client.connect(); + const initialWriterId = await auroraTestUtility.queryInstanceId(client); + expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); + + await client.setReadOnly(true); + const currentReaderId0 = await auroraTestUtility.queryInstanceId(client); + expect(currentReaderId0).not.toBe(initialWriterId); + + // Kill all instances + await ProxyHelper.disableAllConnectivity(env.engine); + await expect(async () => { + await client.setReadOnly(false); + }).rejects.toThrow(); + }, + 1320000 + ); + + itIfMinThreeInstance( + "test set read only all readers down", + async () => { + const config = await initDefaultConfig(env.proxyDatabaseInfo.writerInstanceEndpoint, env.proxyDatabaseInfo.instanceEndpointPort, true); + + client = initClientFunc(config); + + client.on("error", (error: any) => { + logger.debug(`event emitter threw error: ${error.message}`); + logger.debug(error.stack); + }); - // Kill all reader instances - for (const host of env.proxyDatabaseInfo.instances) { - if (host.instanceId && host.instanceId !== initialWriterId) { - await ProxyHelper.disableConnectivity(env.engine, host.instanceId); + await client.connect(); + const initialWriterId = await auroraTestUtility.queryInstanceId(client); + expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); + + // Kill all reader instances + for (const host of env.proxyDatabaseInfo.instances) { + if (host.instanceId && host.instanceId !== initialWriterId) { + await ProxyHelper.disableConnectivity(env.engine, host.instanceId); + } } - } - await client.setReadOnly(true); - const currentReaderId0 = await auroraTestUtility.queryInstanceId(client); - expect(currentReaderId0).toStrictEqual(initialWriterId); + await client.setReadOnly(true); + const currentReaderId0 = await auroraTestUtility.queryInstanceId(client); + expect(currentReaderId0).toStrictEqual(initialWriterId); - await client.setReadOnly(false); - const currentReaderId1 = await auroraTestUtility.queryInstanceId(client); - expect(currentReaderId1).toStrictEqual(initialWriterId); + await client.setReadOnly(false); + const currentReaderId1 = await auroraTestUtility.queryInstanceId(client); + expect(currentReaderId1).toStrictEqual(initialWriterId); - await ProxyHelper.enableAllConnectivity(); - await client.setReadOnly(true); - const currentReaderId2 = await auroraTestUtility.queryInstanceId(client); - expect(currentReaderId2).not.toBe(initialWriterId); - }, 1320000); + await ProxyHelper.enableAllConnectivity(); + await client.setReadOnly(true); + const currentReaderId2 = await auroraTestUtility.queryInstanceId(client); + expect(currentReaderId2).not.toBe(initialWriterId); + }, + 1320000 + ); - it.skip("test failover to new writer set read only true false", async () => { - // Connect to writer instance - const writerConfig = await initConfigWithFailover(env.proxyDatabaseInfo.writerInstanceEndpoint, env.proxyDatabaseInfo.instanceEndpointPort, true); - client = initClientFunc(writerConfig); - client.on("error", (error: any) => { - logger.debug(`event emitter threw error: ${error.message}`); - logger.debug(error.stack); - }); - await client.connect(); - - const initialWriterId = await auroraTestUtility.queryInstanceId(client); - expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); - - // Kill all reader instances - for (const host of env.proxyDatabaseInfo.instances) { - if (host.instanceId && host.instanceId !== initialWriterId) { - await ProxyHelper.disableConnectivity(env.engine, host.instanceId); + itIfMinThreeInstance( + "test failover to new writer set read only true false", + async () => { + // Connect to writer instance + const writerConfig = await initConfigWithFailover( + env.proxyDatabaseInfo.writerInstanceEndpoint, + env.proxyDatabaseInfo.instanceEndpointPort, + true + ); + client = initClientFunc(writerConfig); + client.on("error", (error: any) => { + logger.debug(`event emitter threw error: ${error.message}`); + logger.debug(error.stack); + }); + await client.connect(); + + const initialWriterId = await auroraTestUtility.queryInstanceId(client); + expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); + + // Kill all reader instances + for (const host of env.proxyDatabaseInfo.instances) { + if (host.instanceId && host.instanceId !== initialWriterId) { + await ProxyHelper.disableConnectivity(env.engine, host.instanceId); + } } - } - // Force internal reader connection to the writer instance - await client.setReadOnly(true); - const currentId0 = await auroraTestUtility.queryInstanceId(client); + // Force internal reader connection to the writer instance + await client.setReadOnly(true); + const currentId0 = await auroraTestUtility.queryInstanceId(client); - expect(currentId0).toStrictEqual(initialWriterId); + expect(currentId0).toStrictEqual(initialWriterId); - await client.setReadOnly(false); + await client.setReadOnly(false); - await ProxyHelper.enableAllConnectivity(); + await ProxyHelper.enableAllConnectivity(); - // Crash instance 1 and nominate a new writer - await auroraTestUtility.failoverClusterAndWaitUntilWriterChanged(); - await TestEnvironment.verifyClusterStatus(); + // Crash instance 1 and nominate a new writer + await auroraTestUtility.failoverClusterAndWaitUntilWriterChanged(); + await TestEnvironment.verifyClusterStatus(); - await expect(async () => { - await auroraTestUtility.queryInstanceId(client); - }).rejects.toThrow(FailoverSuccessError); - const newWriterId = await auroraTestUtility.queryInstanceId(client); + await expect(async () => { + await auroraTestUtility.queryInstanceId(client); + }).rejects.toThrow(FailoverSuccessError); + const newWriterId = await auroraTestUtility.queryInstanceId(client); - expect(await auroraTestUtility.isDbInstanceWriter(newWriterId)).toStrictEqual(true); - expect(newWriterId).not.toBe(initialWriterId); + expect(await auroraTestUtility.isDbInstanceWriter(newWriterId)).toStrictEqual(true); + expect(newWriterId).not.toBe(initialWriterId); - await client.setReadOnly(true); - const currentReaderId = await auroraTestUtility.queryInstanceId(client); - expect(currentReaderId).not.toBe(newWriterId); + await client.setReadOnly(true); + const currentReaderId = await auroraTestUtility.queryInstanceId(client); + expect(currentReaderId).not.toBe(newWriterId); - await client.setReadOnly(false); - const currentId = await auroraTestUtility.queryInstanceId(client); - expect(currentId).toStrictEqual(newWriterId); - }, 1320000); + await client.setReadOnly(false); + const currentId = await auroraTestUtility.queryInstanceId(client); + expect(currentId).toStrictEqual(newWriterId); + }, + 1320000 + ); - it.skip("test failover to new reader set read only false true", async () => { - // Connect to writer instance - const writerConfig = await initConfigWithFailover(env.proxyDatabaseInfo.writerInstanceEndpoint, env.proxyDatabaseInfo.instanceEndpointPort, true); - writerConfig["failoverMode"] = "reader-or-writer"; - client = initClientFunc(writerConfig); - client.on("error", (error: any) => { - logger.debug(`event emitter threw error: ${error.message}`); - logger.debug(error.stack); - }); - - await client.connect(); - const initialWriterId = await auroraTestUtility.queryInstanceId(client); - expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); - - await client.setReadOnly(true); - - const readerConnectionId = await auroraTestUtility.queryInstanceId(client); - expect(readerConnectionId).not.toBe(initialWriterId); - // Get a reader instance - let otherReaderId; - for (const host of env.proxyDatabaseInfo.instances) { - if (host.instanceId && host.instanceId !== readerConnectionId && host.instanceId !== initialWriterId) { - otherReaderId = host.instanceId; - break; + itIfMinThreeInstance( + "test failover to new reader set read only false true", + async () => { + // Connect to writer instance + const writerConfig = await initConfigWithFailover( + env.proxyDatabaseInfo.writerInstanceEndpoint, + env.proxyDatabaseInfo.instanceEndpointPort, + true + ); + writerConfig["failoverMode"] = "reader-or-writer"; + client = initClientFunc(writerConfig); + client.on("error", (error: any) => { + logger.debug(`event emitter threw error: ${error.message}`); + logger.debug(error.stack); + }); + + await client.connect(); + const initialWriterId = await auroraTestUtility.queryInstanceId(client); + expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); + + await client.setReadOnly(true); + + const readerConnectionId = await auroraTestUtility.queryInstanceId(client); + expect(readerConnectionId).not.toBe(initialWriterId); + // Get a reader instance + let otherReaderId; + for (const host of env.proxyDatabaseInfo.instances) { + if (host.instanceId && host.instanceId !== readerConnectionId && host.instanceId !== initialWriterId) { + otherReaderId = host.instanceId; + break; + } } - } - if (!otherReaderId) { - throw new Error("Could not find a reader instance"); - } - // Kill all instances except one other reader - for (const host of env.proxyDatabaseInfo.instances) { - if (host.instanceId && host.instanceId !== otherReaderId) { - await ProxyHelper.disableConnectivity(env.engine, host.instanceId); + if (!otherReaderId) { + throw new Error("Could not find a reader instance"); } - } - await expect(async () => { - await auroraTestUtility.queryInstanceId(client); - }).rejects.toThrow(FailoverSuccessError); + // Kill all instances except one other reader + for (const host of env.proxyDatabaseInfo.instances) { + if (host.instanceId && host.instanceId !== otherReaderId) { + await ProxyHelper.disableConnectivity(env.engine, host.instanceId); + } + } + await expect(async () => { + await auroraTestUtility.queryInstanceId(client); + }).rejects.toThrow(FailoverSuccessError); - const currentReaderId0 = await auroraTestUtility.queryInstanceId(client); + const currentReaderId0 = await auroraTestUtility.queryInstanceId(client); - expect(currentReaderId0).toStrictEqual(otherReaderId); - expect(currentReaderId0).not.toBe(readerConnectionId); + expect(currentReaderId0).toStrictEqual(otherReaderId); + expect(currentReaderId0).not.toBe(readerConnectionId); - await ProxyHelper.enableAllConnectivity(); - await client.setReadOnly(false); + await ProxyHelper.enableAllConnectivity(); + await client.setReadOnly(false); - const currentId = await auroraTestUtility.queryInstanceId(client); - expect(currentId).toStrictEqual(initialWriterId); + const currentId = await auroraTestUtility.queryInstanceId(client); + expect(currentId).toStrictEqual(initialWriterId); - await client.setReadOnly(true); + await client.setReadOnly(true); - const currentReaderId2 = await auroraTestUtility.queryInstanceId(client); - expect(currentReaderId2).toStrictEqual(otherReaderId); - }, 1320000); + const currentReaderId2 = await auroraTestUtility.queryInstanceId(client); + expect(currentReaderId2).toStrictEqual(otherReaderId); + }, + 1320000 + ); - it.skip("test failover reader to writer set read only true false", async () => { - // Connect to writer instance - const writerConfig = await initConfigWithFailover(env.proxyDatabaseInfo.writerInstanceEndpoint, env.proxyDatabaseInfo.instanceEndpointPort, true); - client = initClientFunc(writerConfig); - client.on("error", (error: any) => { - logger.debug(`event emitter threw error: ${error.message}`); - logger.debug(error.stack); - }); - await client.connect(); - const initialWriterId = await auroraTestUtility.queryInstanceId(client); - expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); - await client.setReadOnly(true); - - const currentReaderId = await auroraTestUtility.queryInstanceId(client); - expect(currentReaderId).not.toBe(initialWriterId); - - // Kill all reader instances - for (const host of env.proxyDatabaseInfo.instances) { - if (host.instanceId && host.instanceId !== initialWriterId) { - await ProxyHelper.disableConnectivity(env.engine, host.instanceId); + itIfMinThreeInstance( + "test failover reader to writer set read only true false", + async () => { + // Connect to writer instance + const writerConfig = await initConfigWithFailover( + env.proxyDatabaseInfo.writerInstanceEndpoint, + env.proxyDatabaseInfo.instanceEndpointPort, + true + ); + client = initClientFunc(writerConfig); + client.on("error", (error: any) => { + logger.debug(`event emitter threw error: ${error.message}`); + logger.debug(error.stack); + }); + await client.connect(); + const initialWriterId = await auroraTestUtility.queryInstanceId(client); + expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); + await client.setReadOnly(true); + + const currentReaderId = await auroraTestUtility.queryInstanceId(client); + expect(currentReaderId).not.toBe(initialWriterId); + + // Kill all reader instances + for (const host of env.proxyDatabaseInfo.instances) { + if (host.instanceId && host.instanceId !== initialWriterId) { + await ProxyHelper.disableConnectivity(env.engine, host.instanceId); + } } - } - await expect(async () => { - await auroraTestUtility.queryInstanceId(client); - }).rejects.toThrow(FailoverSuccessError); + await expect(async () => { + await auroraTestUtility.queryInstanceId(client); + }).rejects.toThrow(FailoverSuccessError); - const currentId0 = await auroraTestUtility.queryInstanceId(client); + const currentId0 = await auroraTestUtility.queryInstanceId(client); - expect(currentId0).toStrictEqual(initialWriterId); - - await ProxyHelper.enableAllConnectivity(); - await client.setReadOnly(true); + expect(currentId0).toStrictEqual(initialWriterId); - const currentId1 = await auroraTestUtility.queryInstanceId(client); - expect(currentId1).not.toBe(initialWriterId); + await ProxyHelper.enableAllConnectivity(); + await client.setReadOnly(true); - await client.setReadOnly(false); + const currentId1 = await auroraTestUtility.queryInstanceId(client); + expect(currentId1).not.toBe(initialWriterId); - const currentId2 = await auroraTestUtility.queryInstanceId(client); - expect(currentId2).toStrictEqual(initialWriterId); - }, 1320000); + await client.setReadOnly(false); - it.skip("test pooled connection failover", async () => { - const config = await initConfigWithFailover(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort, false); - client = initClientFunc(config); + const currentId2 = await auroraTestUtility.queryInstanceId(client); + expect(currentId2).toStrictEqual(initialWriterId); + }, + 1320000 + ); - const provider = new InternalPooledConnectionProvider(); - ConnectionProviderManager.setConnectionProvider(provider); + itIf( + "test pooled connection failover", + async () => { + const config = await initConfigWithFailover(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort, false); + client = initClientFunc(config); + const provider = new InternalPooledConnectionProvider(); + ConnectionProviderManager.setConnectionProvider(provider); - client.on("error", (error: any) => { - logger.debug(`event emitter threw error: ${error.message}`); - logger.debug(error.stack); - }); + client.on("error", (error: any) => { + logger.debug(`event emitter threw error: ${error.message}`); + logger.debug(error.stack); + }); - await client.connect(); - const initialWriterId = await auroraTestUtility.queryInstanceId(client); - provider.logConnections(); + await client.connect(); + const initialWriterId = await auroraTestUtility.queryInstanceId(client); + provider.logConnections(); - await auroraTestUtility.failoverClusterAndWaitUntilWriterChanged(); - await expect(async () => { - await auroraTestUtility.queryInstanceId(client); - }).rejects.toThrow(FailoverSuccessError); + await auroraTestUtility.failoverClusterAndWaitUntilWriterChanged(); + await expect(async () => { + await auroraTestUtility.queryInstanceId(client); + }).rejects.toThrow(FailoverSuccessError); - const newWriterId = await auroraTestUtility.queryInstanceId(client); - expect(newWriterId).not.toBe(initialWriterId); - provider.logConnections(); - await client.connect(); - const oldWriterId = await auroraTestUtility.queryInstanceId(client); - expect(oldWriterId).toBe(initialWriterId); - }, 1000000); + const newWriterId = await auroraTestUtility.queryInstanceId(client); + expect(newWriterId).not.toBe(initialWriterId); + await client.connect(); + provider.logConnections(); + const oldWriterId = await auroraTestUtility.queryInstanceId(client); + // This should be a new connection to the initial writer instance (now a reader). + expect(oldWriterId).toBe(initialWriterId); + provider.logConnections(); + }, + 1000000 + ); itIf( "test set read only reuse cached connection", @@ -483,74 +532,87 @@ describe("aurora read write splitting", () => { await secondaryClient.connect(); expect(client).not.toBe(secondaryClient); provider.logConnections(); + try { + await secondaryClient.end(); + } catch (error) { + // pass + } }, 1000000 ); - it.skip("test pooled connection failover failed", async () => { - const config = await initConfigWithFailover(env.proxyDatabaseInfo.writerInstanceEndpoint, env.proxyDatabaseInfo.instanceEndpointPort, true); - config["failoverTimeoutMs"] = 1000; - - client = initClientFunc(config); + itIf( + "test pooled connection failover failed", + async () => { + const config = await initConfigWithFailover(env.proxyDatabaseInfo.writerInstanceEndpoint, env.proxyDatabaseInfo.instanceEndpointPort, true); + config["failoverTimeoutMs"] = 1000; - client.on("error", (error: any) => { - logger.debug(`event emitter threw error: ${error.message}`); - logger.debug(error.stack); - }); + client = initClientFunc(config); - provider = new InternalPooledConnectionProvider(); - ConnectionProviderManager.setConnectionProvider(provider); + client.on("error", (error: any) => { + logger.debug(`event emitter threw error: ${error.message}`); + logger.debug(error.stack); + }); - await client.connect(); - const initialWriterId = await auroraTestUtility.queryInstanceId(client); + provider = new InternalPooledConnectionProvider(); + ConnectionProviderManager.setConnectionProvider(provider); - // Kill all instances - await ProxyHelper.disableAllConnectivity(env.engine); - await expect(async () => { - await auroraTestUtility.queryInstanceId(client); - }).rejects.toThrow(FailoverFailedError); - await ProxyHelper.enableAllConnectivity(); - await client.connect(); - await TestEnvironment.verifyClusterStatus(); + await client.connect(); + const initialWriterId = await auroraTestUtility.queryInstanceId(client); + + // Kill all instances + await ProxyHelper.disableAllConnectivity(env.engine); + await expect(async () => { + await auroraTestUtility.queryInstanceId(client); + }).rejects.toThrow(FailoverFailedError); + await ProxyHelper.enableAllConnectivity(); + await client.connect(); + await TestEnvironment.verifyClusterStatus(); - const newWriterId = await auroraTestUtility.queryInstanceId(client); - expect(newWriterId).toBe(initialWriterId); - }, 1000000); + const newWriterId = await auroraTestUtility.queryInstanceId(client); + expect(newWriterId).toBe(initialWriterId); + }, + 1000000 + ); - it.skip("test pooled connection failover in transaction", async () => { - const config = await initConfigWithFailover(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort, false); - client = initClientFunc(config); + itIf( + "test pooled connection failover in transaction", + async () => { + const config = await initConfigWithFailover(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort, false); + client = initClientFunc(config); - provider = new InternalPooledConnectionProvider(); - await provider.releaseResources(); // make sure there's no pool's left after prior test + provider = new InternalPooledConnectionProvider(); + await provider.releaseResources(); // make sure there's no pool's left after prior test - ConnectionProviderManager.setConnectionProvider(provider); + ConnectionProviderManager.setConnectionProvider(provider); - client.on("error", (error: any) => { - logger.debug(`event emitter threw error: ${error.message}`); - logger.debug(error.stack); - }); + client.on("error", (error: any) => { + logger.debug(`event emitter threw error: ${error.message}`); + logger.debug(error.stack); + }); - await client.connect(); + await client.connect(); - const initialWriterId = await auroraTestUtility.queryInstanceId(client); - expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); + const initialWriterId = await auroraTestUtility.queryInstanceId(client); + expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); - await DriverHelper.executeQuery(env.engine, client, "DROP TABLE IF EXISTS test3_3"); - await DriverHelper.executeQuery(env.engine, client, "CREATE TABLE test3_3 (id int not null primary key, test3_3_field varchar(255) not null)"); + await DriverHelper.executeQuery(env.engine, client, "DROP TABLE IF EXISTS test3_3"); + await DriverHelper.executeQuery(env.engine, client, "CREATE TABLE test3_3 (id int not null primary key, test3_3_field varchar(255) not null)"); - await DriverHelper.executeQuery(env.engine, client, "START TRANSACTION READ ONLY"); // start transaction - await DriverHelper.executeQuery(env.engine, client, "SELECT 1"); - // Crash instance 1 and nominate a new writer - await auroraTestUtility.failoverClusterAndWaitUntilWriterChanged(); + await DriverHelper.executeQuery(env.engine, client, "START TRANSACTION READ ONLY"); // start transaction + await DriverHelper.executeQuery(env.engine, client, "SELECT 1"); + // Crash instance 1 and nominate a new writer + await auroraTestUtility.failoverClusterAndWaitUntilWriterChanged(); - await expect(async () => { - await DriverHelper.executeQuery(env.engine, client, "INSERT INTO test3_3 VALUES (2, 'test field string 2')"); - }).rejects.toThrow(TransactionResolutionUnknownError); + await expect(async () => { + await DriverHelper.executeQuery(env.engine, client, "INSERT INTO test3_3 VALUES (2, 'test field string 2')"); + }).rejects.toThrow(TransactionResolutionUnknownError); - // Attempt to query the instance id. - const nextWriterId = await auroraTestUtility.queryInstanceId(client); - expect(nextWriterId).not.toBe(initialWriterId); - await DriverHelper.executeQuery(env.engine, client, "COMMIT"); - }, 1000000); + // Attempt to query the instance id. + const nextWriterId = await auroraTestUtility.queryInstanceId(client); + expect(nextWriterId).not.toBe(initialWriterId); + await DriverHelper.executeQuery(env.engine, client, "COMMIT"); + }, + 1000000 + ); });