Skip to content

Commit 0ccd6ea

Browse files
committed
fix: unit tests
1 parent 0143408 commit 0ccd6ea

4 files changed

+18
-18
lines changed

tests/unit/monitor_impl.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe("monitor impl test", () => {
105105
});
106106

107107
it("run with context", async () => {
108-
const monitorContextInstance = new MonitorConnectionContext(monitor, mockClient, 30000, 5000, 3);
108+
const monitorContextInstance = new MonitorConnectionContext(monitor, mockClient, 30000, 5000, 3, mockPluginService);
109109
monitor.startMonitoring(monitorContextInstance);
110110
// Should end by itself.
111111
monitor.run();

tests/unit/notification_pipeline.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class TestPlugin extends DefaultPlugin {
3131
return Promise.resolve(OldConnectionSuggestionAction.NO_OPINION);
3232
}
3333

34-
notifyHostListChanged(changes: Map<string, Set<HostChangeOptions>>): void {
34+
notifyHostListChanged(changes: Map<string, Set<HostChangeOptions>>): Promise<void> {
3535
this.counter++;
36-
return;
36+
return Promise.resolve();
3737
}
3838

3939
resetCounter() {

tests/unit/read_write_splitting.test.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import { OldConnectionSuggestionAction } from "../../common/lib/old_connection_s
3131
import { HostListProvider } from "../../common/lib/host_list_provider/host_list_provider";
3232
import { WrapperProperties } from "../../common/lib/wrapper_property";
3333
import { ClientWrapper } from "../../common/lib/client_wrapper";
34+
import { NodePostgresDriverDialect } from "../../pg/lib/dialect/node_postgres_driver_dialect";
35+
import { DriverDialect } from "../../common/lib/driver_dialect/driver_dialect";
36+
import { MySQL2DriverDialect } from "../../mysql/lib/dialect/mysql2_driver_dialect";
3437

3538
const properties: Map<string, any> = new Map();
3639
const builder = new HostInfoBuilder({ hostAvailabilityStrategy: new SimpleHostAvailabilityStrategy() });
@@ -53,6 +56,7 @@ const mockHostListProvider: HostListProvider = mock<HostListProvider>();
5356
const mockClosedReaderClient: AwsClient = mock(AwsMySQLClient);
5457
const mockClosedWriterClient: AwsClient = mock(AwsMySQLClient);
5558
const mockDialect: MySQLDatabaseDialect = mock(MySQLDatabaseDialect);
59+
const mockDriverDialect: DriverDialect = mock(MySQL2DriverDialect);
5660
const mockChanges: Set<HostChangeOptions> = mock(Set<HostChangeOptions>);
5761

5862
const clientWrapper: ClientWrapper = {
@@ -79,6 +83,9 @@ describe("reader write splitting test", () => {
7983
when(mockPluginService.getHostListProvider()).thenReturn(instance(mockHostListProvider));
8084
when(mockPluginService.getHosts()).thenReturn(defaultHosts);
8185
when(mockPluginService.isInTransaction()).thenReturn(false);
86+
when(mockPluginService.getDialect()).thenReturn(mockDialect);
87+
when(mockPluginService.getDriverDialect()).thenReturn(mockDriverDialect);
88+
when(mockDriverDialect.connect(anything())).thenReturn(Promise.resolve(mockReaderClient));
8289
properties.clear();
8390
});
8491

@@ -101,8 +108,6 @@ describe("reader write splitting test", () => {
101108
when(mockPluginService.getCurrentClient()).thenReturn(instance(mockWriterClient));
102109
when(await mockWriterClient.isValid()).thenReturn(true);
103110
when(mockPluginService.getCurrentHostInfo()).thenReturn(writerHost);
104-
when(mockPluginService.getDialect()).thenReturn(mockDialect);
105-
when(mockDialect.connect(anything())).thenReturn(Promise.resolve(mockReaderClient));
106111
when(mockPluginService.connect(anything(), anything())).thenResolve(mockReaderWrapper);
107112

108113
const target = new ReadWriteSplittingPlugin(
@@ -127,8 +132,6 @@ describe("reader write splitting test", () => {
127132
when(mockPluginService.getCurrentClient()).thenReturn(instance(mockReaderClient));
128133
when(await mockReaderClient.isValid()).thenReturn(true);
129134
when(mockPluginService.getCurrentHostInfo()).thenReturn(readerHost1);
130-
when(mockPluginService.getDialect()).thenReturn(mockDialect);
131-
when(mockDialect.connect(anything())).thenReturn(Promise.resolve(mockReaderClient));
132135
when(mockPluginService.connect(anything(), anything())).thenResolve(mockWriterWrapper);
133136

134137
const target = new ReadWriteSplittingPlugin(
@@ -153,8 +156,6 @@ describe("reader write splitting test", () => {
153156
when(mockPluginService.getCurrentClient()).thenReturn(instance(mockReaderClient));
154157
when(await mockReaderClient.isValid()).thenReturn(true);
155158
when(mockPluginService.getCurrentHostInfo()).thenReturn(readerHost1);
156-
when(mockPluginService.getDialect()).thenReturn(mockDialect);
157-
when(mockDialect.connect(anything())).thenReturn(Promise.resolve(mockReaderClient));
158159
when(mockPluginService.connect(anything(), anything())).thenResolve(mockReaderWrapper);
159160

160161
const target = new ReadWriteSplittingPlugin(
@@ -179,8 +180,6 @@ describe("reader write splitting test", () => {
179180
when(mockPluginService.getCurrentClient()).thenReturn(instance(mockWriterClient));
180181
when(await mockWriterClient.isValid()).thenReturn(true);
181182
when(mockPluginService.getCurrentHostInfo()).thenReturn(writerHost);
182-
when(mockPluginService.getDialect()).thenReturn(mockDialect);
183-
when(mockDialect.connect(anything())).thenReturn(Promise.resolve(mockReaderClient));
184183
when(mockPluginService.connect(anything(), anything())).thenResolve(mockReaderWrapper);
185184

186185
const target = new ReadWriteSplittingPlugin(
@@ -207,8 +206,6 @@ describe("reader write splitting test", () => {
207206
when(mockWriterClient.targetClient).thenReturn(mockWriterWrapper);
208207
when(mockWriterClient.targetClient && (await mockPluginService.isClientValid(mockWriterClient.targetClient))).thenReturn(true);
209208
when(mockPluginService.getCurrentHostInfo()).thenReturn(writerHost);
210-
when(mockPluginService.getDialect()).thenReturn(mockDialect);
211-
when(mockDialect.connect(anything())).thenReturn(Promise.resolve(mockReaderClient));
212209
when(mockPluginService.connect(anything(), anything())).thenReturn(Promise.resolve(mockWriterWrapper));
213210

214211
const target = new ReadWriteSplittingPlugin(

tests/unit/reader_failover_handler.test.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { anything, instance, mock, reset, verify, when } from "ts-mockito";
2424
import { ClientWrapper } from "../../common/lib/client_wrapper";
2525
import { PgDatabaseDialect } from "../../pg/lib/dialect/pg_database_dialect";
2626
import { FailoverRestriction } from "../../common/lib/plugins/failover/failover_restriction";
27+
import { NodePostgresDriverDialect } from "../../pg/lib/dialect/node_postgres_driver_dialect";
2728

2829
const host1 = new HostInfo("writer", 1234, HostRole.WRITER);
2930
const host2 = new HostInfo("reader1", 1234, HostRole.READER);
@@ -33,7 +34,8 @@ const host5 = new HostInfo("reader4", 1234, HostRole.READER);
3334
const host6 = new HostInfo("reader5", 1234, HostRole.READER);
3435
const defaultHosts = [host1, host2, host3, host4, host5, host6];
3536
const properties = new Map();
36-
const dialect = mock(PgDatabaseDialect);
37+
const mockDatabaseDialect = mock(PgDatabaseDialect);
38+
const mockDriverDialect = mock(NodePostgresDriverDialect);
3739
const clientWrapper: ClientWrapper = {
3840
client: undefined,
3941
hostInfo: mock(HostInfo),
@@ -46,8 +48,11 @@ const mockPluginService = mock(PluginService);
4648

4749
describe("reader failover handler", () => {
4850
beforeEach(() => {
49-
when(dialect.getFailoverRestrictions()).thenReturn([]);
50-
when(mockPluginService.getDialect()).thenReturn(instance(dialect));
51+
when(mockDatabaseDialect.getFailoverRestrictions()).thenReturn([]);
52+
when(mockPluginService.getDialect()).thenReturn(mockDatabaseDialect);
53+
when(mockPluginService.getDriverDialect()).thenReturn(mockDriverDialect);
54+
when(mockDriverDialect.createTargetClient(anything())).thenReturn(mockTargetClient);
55+
when(mockDriverDialect.connect(anything())).thenResolve(mockTargetClient);
5156
});
5257
afterEach(() => {
5358
reset(mockPluginService);
@@ -97,8 +102,6 @@ describe("reader failover handler", () => {
97102
const currentHostIndex = 2;
98103

99104
when(mockPluginService.getHosts()).thenReturn(hosts);
100-
when(mockPluginService.createTargetClient(anything())).thenReturn(mockTargetClient);
101-
when(dialect.connect(anything())).thenResolve(mockTargetClient);
102105
when(mockPluginService.forceConnect(anything(), properties)).thenCall(async () => {
103106
await new Promise((resolve, reject) => {
104107
timeoutId = setTimeout(resolve, 20000);

0 commit comments

Comments
 (0)