Skip to content

Commit

Permalink
fix: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sophia-bq committed Jan 29, 2025
1 parent ee01e15 commit 94cee75
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { MonitoringRdsHostListProvider } from "./monitoring_host_list_provider";
import { Messages } from "../../utils/messages";

export interface ClusterTopologyMonitor {
forceRefresh(client: any, timeoutMs: number): Promise<HostInfo[]>;
forceRefresh(client: ClientWrapper, timeoutMs: number): Promise<HostInfo[]>;

close(): Promise<void>;

Expand Down
12 changes: 6 additions & 6 deletions common/lib/plugins/failover2/failover2_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ export class Failover2Plugin extends AbstractConnectionPlugin implements CanRele
logger.error(Messages.get("Failover2.unableToFetchTopology"));
throw new FailoverFailedError(Messages.get("Failover.unableToConnectToReader"));
}

try {
const result: ReaderFailoverResult = await this.getReaderFailoverConnection(failoverEndTimeMs);
logger.info(Messages.get("Failover.establishedConnection", result.newHost.host));
Expand Down Expand Up @@ -300,7 +299,6 @@ export class Failover2Plugin extends AbstractConnectionPlugin implements CanRele
//new Map(hosts.filter((x) => x.role === HostRole.READER).map((x) => [x.hostId, x]));
const originalWriter: HostInfo = hosts.find((x) => x.role === HostRole.WRITER);
let isOriginalWriterStillWriter: boolean = false;

// Signal to connect that this is an internal call and does not require additional processing.
const copyProps = new Map<string, any>(this._properties);
copyProps.set(Failover2Plugin.INTERNAL_CONNECT_PROPERTY_NAME, true);
Expand All @@ -322,10 +320,12 @@ export class Failover2Plugin extends AbstractConnectionPlugin implements CanRele
try {
const candidateClient: ClientWrapper = await this.pluginService.connect(readerCandidate, copyProps);
const role: HostRole = await this.pluginService.getHostRole(candidateClient);
if (role === HostRole.READER || this.failoverMode != FailoverMode.STRICT_READER) {
// Update HostInfo to reflect correct HostRole.
const updatedHostInfo: HostInfo = this.pluginService.getHostInfoBuilder().copyFrom(readerCandidate).withRole(role).build();
return new ReaderFailoverResult(candidateClient, updatedHostInfo, true);
if (role === HostRole.READER || this.failoverMode !== FailoverMode.STRICT_READER) {
if (role !== readerCandidate.role) {
// Update HostInfo to reflect correct HostRole.
readerCandidate = this.pluginService.getHostInfoBuilder().copyFrom(readerCandidate).withRole(role).build();
}
return new ReaderFailoverResult(candidateClient, readerCandidate, true);
}

// Unable to fail over to readerCandidate, remove from remaining readers to try.
Expand Down
30 changes: 15 additions & 15 deletions tests/unit/failover2_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ import { RdsUrlType } from "../../common/lib/utils/rds_url_type";
import { RdsUtils } from "../../common/lib/utils/rds_utils";
import { WrapperProperties } from "../../common/lib/wrapper_property";
import { AwsMySQLClient } from "../../mysql/lib";
import { anything, instance, mock, reset, resetCalls, spy, verify, when } from "ts-mockito";
import { anything, instance, mock, reset, spy, verify, when } from "ts-mockito";
import { Messages } from "../../common/lib/utils/messages";
import { HostChangeOptions } from "../../common/lib/host_change_options";
import { NullTelemetryFactory } from "../../common/lib/utils/telemetry/null_telemetry_factory";
import { MySQLClientWrapper } from "../../common/lib/mysql_client_wrapper";
import { MySQL2DriverDialect } from "../../mysql/lib/dialect/mysql2_driver_dialect";
Expand Down Expand Up @@ -115,7 +114,7 @@ describe("reader failover handler", () => {
when(spyPlugin.failoverWriter()).thenResolve();
plugin.failoverMode = FailoverMode.STRICT_WRITER;

await expect(plugin.failover(instance(mockHostInfo))).rejects.toThrow(
await expect(plugin.failover()).rejects.toThrow(
new TransactionResolutionUnknownError(Messages.get("Failover.transactionResolutionUnknownError"))
);

Expand All @@ -126,26 +125,27 @@ describe("reader failover handler", () => {
when(mockPluginService.isInTransaction()).thenReturn(false);
initializePlugin(instance(mockPluginService));

const mockHostInfoInstance: HostInfo = instance(mockHostInfo);
const spyPlugin: Failover2Plugin = spy(plugin);
when(spyPlugin.failoverReader()).thenResolve();
plugin.failoverMode = FailoverMode.READER_OR_WRITER;

await expect(plugin.failover(mockHostInfoInstance)).rejects.toThrow(new FailoverSuccessError(Messages.get("Failover.connectionChangedError")));
await expect(plugin.failover()).rejects.toThrow(new FailoverSuccessError(Messages.get("Failover.connectionChangedError")));

verify(spyPlugin.failoverReader()).once();
});

it("test failover reader success", async () => {
const hostInfo = builder.withHost("hostA").build();
const hostInfo = builder.withHost("hostA").withRole(HostRole.READER).build();
const hosts = [hostInfo];

when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "alias2"]));
when(mockHostInfo.getRawAvailability()).thenReturn(HostAvailability.AVAILABLE);
when(mockPluginService.getHosts()).thenReturn(hosts);
when(mockPluginService.initiateTopologyUpdate(false, anything())).thenResolve(true);
when(mockPluginService.connect(hostInfo, anything())).thenResolve(mockClientWrapper);
when(mockPluginService.getHostInfoByStrategy(HostRole.READER, anything(), anything())).thenReturn(mockHostInfo);
when(mockPluginService.forceMonitoringRefresh(false, anything())).thenResolve(true);
when(mockPluginService.connect(anything(), anything())).thenResolve(mockClientWrapper);
when(mockPluginService.getHostRole(mockClientWrapper)).thenResolve(HostRole.READER);
when(mockPluginService.getHostInfoBuilder()).thenReturn(builder);

const mockPluginServiceInstance = instance(mockPluginService);
const mockHostInfoInstance = instance(mockHostInfo);
Expand All @@ -158,7 +158,7 @@ describe("reader failover handler", () => {

await plugin.failoverReader();

verify(mockPluginService.setCurrentClient(mockClientWrapper, hostInfo)).once();
verify(mockPluginService.setCurrentClient(mockClientWrapper, anything())).once();
});

it("test failover reader - connection throws error", async () => {
Expand All @@ -169,7 +169,7 @@ describe("reader failover handler", () => {
when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "alias2"]));
when(mockHostInfo.getRawAvailability()).thenReturn(HostAvailability.AVAILABLE);
when(mockPluginService.getHosts()).thenReturn(hosts);
when(mockPluginService.initiateTopologyUpdate(true, anything())).thenResolve(true);
when(mockPluginService.forceMonitoringRefresh(true, anything())).thenResolve(true);
when(mockPluginService.connect(mockHostInfo, anything())).thenReject(test);

const mockHostInfoInstance = instance(mockHostInfo);
Expand All @@ -194,7 +194,7 @@ describe("reader failover handler", () => {

when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "alias2"]));
when(mockPluginService.getHosts()).thenReturn(hosts);
when(mockPluginService.initiateTopologyUpdate(true, anything())).thenResolve(false);
when(mockPluginService.forceMonitoringRefresh(true, anything())).thenResolve(false);

const mockHostInfoInstance = instance(mockHostInfo);
const mockPluginServiceInstance = instance(mockPluginService);
Expand All @@ -218,7 +218,7 @@ describe("reader failover handler", () => {

when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "alias2"]));
when(mockPluginService.getHosts()).thenReturn(hosts);
when(mockPluginService.initiateTopologyUpdate(true, anything())).thenResolve(true);
when(mockPluginService.forceMonitoringRefresh(true, anything())).thenResolve(true);
when(mockPluginService.connect(mockHostInfo, anything())).thenResolve(null);

const mockHostInfoInstance = instance(mockHostInfo);
Expand All @@ -236,7 +236,7 @@ describe("reader failover handler", () => {
}
}

verify(mockPluginService.initiateTopologyUpdate(true, anything())).once();
verify(mockPluginService.forceMonitoringRefresh(true, anything())).once();
verify(mockPluginService.setCurrentClient(anything(), anything())).never();
});

Expand All @@ -246,7 +246,7 @@ describe("reader failover handler", () => {

when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "alias2"]));
when(mockPluginService.getHosts()).thenReturn(hosts);
when(mockPluginService.initiateTopologyUpdate(true, anything())).thenResolve(true);
when(mockPluginService.forceMonitoringRefresh(true, anything())).thenResolve(true);
when(mockPluginService.connect(hostInfo, anything())).thenResolve(mockClientWrapper);
when(mockPluginService.getHostRole(mockClientWrapper)).thenResolve(HostRole.WRITER);

Expand All @@ -258,7 +258,7 @@ describe("reader failover handler", () => {

await plugin.failoverWriter();

verify(mockPluginService.initiateTopologyUpdate(true, anything())).once();
verify(mockPluginService.forceMonitoringRefresh(true, anything())).once();
verify(mockPluginService.setCurrentClient(mockClientWrapper, hostInfo)).once();
});

Expand Down

0 comments on commit 94cee75

Please sign in to comment.