Skip to content

Commit 1a83cc7

Browse files
committed
fixed unit tests
1 parent 8f816ee commit 1a83cc7

File tree

4 files changed

+52
-53
lines changed

4 files changed

+52
-53
lines changed

tests/Test/BinlogPositionTest.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ namespace Test
1212
[Trait("Category", "Replication")]
1313
public class BinlogPositionTest
1414
{
15-
private readonly MySQLFixture _mysqlFixture;
1615
protected readonly ITestOutputHelper _outputHelper;
1716

1817
public BinlogPositionTest(ITestOutputHelper outputHelper)
1918
{
2019
_outputHelper = outputHelper;
21-
_mysqlFixture = MySQLFixture.Instance;
22-
using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
2320
}
2421

2522
// Unit tests for BinlogPosition class
@@ -98,13 +95,15 @@ public void Properties_CanBeModified()
9895
[Fact]
9996
public async Task GetCurrentBinlogPosition_ReturnsValidPosition()
10097
{
98+
using var mysqlFixture = MySQLFixture.CreateMySQLFixture();
99+
101100
// Execute a simple command to ensure we have binlog activity
102-
var cmd = _mysqlFixture.CreateCommand();
101+
var cmd = mysqlFixture.CreateCommand();
103102
cmd.CommandText = "SELECT 1";
104103
await cmd.ExecuteScalarAsync();
105104

106105
// Get the current binlog position
107-
var client = _mysqlFixture.Client;
106+
var client = mysqlFixture.Client;
108107
var currentPosition = client.CurrentPosition;
109108

110109
// Assert
@@ -117,18 +116,20 @@ public async Task GetCurrentBinlogPosition_ReturnsValidPosition()
117116
[Fact]
118117
public async Task BinlogPosition_ShouldAdvanceAfterOperations()
119118
{
119+
using var mysqlFixture = MySQLFixture.CreateMySQLFixture();
120+
120121
// Get the current binlog position to use as reference
121-
var client = _mysqlFixture.Client;
122+
var client = mysqlFixture.Client;
122123
var initialPosition = client.CurrentPosition;
123124
_outputHelper.WriteLine($"Initial binlog position: {initialPosition}");
124125

125126
// Execute an operation to advance binlog position
126-
var cmd = _mysqlFixture.CreateCommand();
127+
var cmd = mysqlFixture.CreateCommand();
127128
cmd.CommandText = "INSERT INTO pet (name, owner, species) VALUES ('TestPet', 'TestOwner', 'TestSpecies')";
128129
await cmd.ExecuteNonQueryAsync();
129130

130131
// Receive an event to ensure binlog has progressed
131-
var eventLog = await _mysqlFixture.ReceiveAsync<WriteRowsEvent>();
132+
var eventLog = await mysqlFixture.ReceiveAsync<WriteRowsEvent>();
132133
Assert.NotNull(eventLog);
133134

134135
// Check that position has advanced
@@ -147,8 +148,10 @@ public async Task BinlogPosition_ShouldAdvanceAfterOperations()
147148
[Fact]
148149
public async Task PositionChanged_EventFires_WhenPositionChanges()
149150
{
151+
using var mysqlFixture = MySQLFixture.CreateMySQLFixture();
152+
150153
// Set up event handler to detect position changes
151-
var client = _mysqlFixture.Client;
154+
var client = mysqlFixture.Client;
152155
var positionChangedEvent = new ManualResetEventSlim(false);
153156
BinlogPosition capturedPosition = null;
154157

@@ -162,11 +165,11 @@ public async Task PositionChanged_EventFires_WhenPositionChanges()
162165
try
163166
{
164167
// Execute an operation that will trigger binlog position change
165-
var cmd = _mysqlFixture.CreateCommand();
168+
var cmd = mysqlFixture.CreateCommand();
166169
cmd.CommandText = "INSERT INTO pet (name, owner, species) VALUES ('EventTest', 'EventOwner', 'EventSpecies')";
167170
await cmd.ExecuteNonQueryAsync();
168171

169-
await _mysqlFixture.ReceiveAsync<WriteRowsEvent>();
172+
await mysqlFixture.ReceiveAsync<WriteRowsEvent>();
170173

171174
// Wait for the position changed event to fire
172175
var eventFired = positionChangedEvent.Wait(TimeSpan.FromSeconds(5));
@@ -186,18 +189,20 @@ public async Task PositionChanged_EventFires_WhenPositionChanges()
186189
[Fact]
187190
public async Task RotateEvent_UpdatesBinlogPosition()
188191
{
192+
using var mysqlFixture = MySQLFixture.CreateMySQLFixture();
193+
189194
// Force a log rotation
190-
var cmd = _mysqlFixture.CreateCommand();
195+
var cmd = mysqlFixture.CreateCommand();
191196
cmd.CommandText = "FLUSH LOGS";
192197
await cmd.ExecuteNonQueryAsync();
193198

194199
// We should receive a rotate event
195-
var rotateEvent = await _mysqlFixture.ReceiveAsync<RotateEvent>();
200+
var rotateEvent = await mysqlFixture.ReceiveAsync<RotateEvent>();
196201
Assert.NotNull(rotateEvent);
197202
_outputHelper.WriteLine($"Received rotate event with next log: {rotateEvent.NextBinlogFileName} at position: {rotateEvent.RotatePosition}");
198203

199204
// Get current position after rotation
200-
var currentPosition = _mysqlFixture.Client.CurrentPosition;
205+
var currentPosition = mysqlFixture.Client.CurrentPosition;
201206
_outputHelper.WriteLine($"Current binlog position after rotation: {currentPosition}");
202207

203208
// The rotate event's next binlog file name should match our current filename
@@ -207,8 +212,10 @@ public async Task RotateEvent_UpdatesBinlogPosition()
207212
[Fact]
208213
public async Task ConnectWithBinlogPosition_ShouldStartFromSpecifiedPosition()
209214
{
215+
using var mysqlFixture = MySQLFixture.CreateMySQLFixture();
216+
210217
// First get the current position from our existing connection to use as reference
211-
var currentPosition = _mysqlFixture.Client.CurrentPosition;
218+
var currentPosition = mysqlFixture.Client.CurrentPosition;
212219
_outputHelper.WriteLine($"Current binlog position: {currentPosition}");
213220

214221
// Create a new replication client
@@ -235,7 +242,7 @@ public async Task ConnectWithBinlogPosition_ShouldStartFromSpecifiedPosition()
235242
_outputHelper.WriteLine($"Successfully connected with position: {newClient.CurrentPosition}");
236243

237244
// Now insert a record to generate a new event
238-
var cmd = _mysqlFixture.CreateCommand();
245+
var cmd = mysqlFixture.CreateCommand();
239246
cmd.CommandText = "INSERT INTO pet (name, owner, species) VALUES ('PositionTest', 'PositionTestOwner', 'PositionTestSpecies')";
240247
await cmd.ExecuteNonQueryAsync();
241248

tests/Test/DataTypesTest.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@
77
using SciSharp.MySQL.Replication;
88
using SciSharp.MySQL.Replication.Events;
99
using Xunit;
10-
using Xunit.Abstractions;
11-
using Microsoft.Extensions.Logging;
12-
using Microsoft.Extensions.Logging.Console;
13-
using System.Collections;
14-
using System.Data;
15-
using System.Globalization;
1610

1711
namespace Test
1812
{
1913
[Trait("Category", "DataTypes")]
2014
public class DataTypesTest
2115
{
22-
private readonly MySQLFixture _mysqlFixture = MySQLFixture.Instance;
23-
2416
[Fact]
2517
public async Task TestDateTimeType()
2618
{
@@ -220,14 +212,16 @@ await TestDataType<int>("year_table", currentValue, 2024, (reader, index) =>
220212

221213
private async Task TestDataType<TDateType>(string tableName, TDateType currentValue, TDateType updateValue, Func<MySqlDataReader, int, TDateType> dataReader)
222214
{
215+
var mysqlFixture = MySQLFixture.CreateMySQLFixture();
216+
223217
// Insert a new row into the table
224-
var command = _mysqlFixture.CreateCommand();
218+
var command = mysqlFixture.CreateCommand();
225219
command.CommandText = $"insert into {tableName} (value) values (@value);SELECT LAST_INSERT_ID();";
226220
command.Parameters.AddWithValue("@value", currentValue);
227221
var id = (Int32)(UInt64)await command.ExecuteScalarAsync();
228222

229223
// Validate the WriteRowsEvent
230-
var writeRowsEvent = await _mysqlFixture.ReceiveAsync<WriteRowsEvent>();
224+
var writeRowsEvent = await mysqlFixture.ReceiveAsync<WriteRowsEvent>();
231225

232226
Assert.Equal(1, writeRowsEvent.RowSet.Rows.Count);
233227
Assert.Equal("id", writeRowsEvent.RowSet.ColumnNames[0]);
@@ -239,7 +233,7 @@ private async Task TestDataType<TDateType>(string tableName, TDateType currentVa
239233
Assert.Equal(currentValue, (TDateType)valueFromClient);
240234

241235
// Validate the data in the database with query
242-
command = _mysqlFixture.CreateCommand();
236+
command = mysqlFixture.CreateCommand();
243237
command.CommandText = $"select value from {tableName} where id = @id";
244238
command.Parameters.AddWithValue("@id", id);
245239

@@ -253,15 +247,15 @@ private async Task TestDataType<TDateType>(string tableName, TDateType currentVa
253247
Assert.Equal(currentValue, savedValue);
254248

255249
// Update the row
256-
command = _mysqlFixture.CreateCommand();
250+
command = mysqlFixture.CreateCommand();
257251
command.CommandText = $"update {tableName} set value=@value where id = @id";
258252
command.Parameters.AddWithValue("@id", id);
259253
command.Parameters.AddWithValue("@value", updateValue);
260254

261255
Assert.Equal(1, await command.ExecuteNonQueryAsync());
262256

263257
// Validate the UpdateRowsEvent
264-
var updateRowsEvent = await _mysqlFixture.ReceiveAsync<UpdateRowsEvent>();
258+
var updateRowsEvent = await mysqlFixture.ReceiveAsync<UpdateRowsEvent>();
265259
Assert.Equal(1, updateRowsEvent.RowSet.Rows.Count);
266260
Assert.Equal("id", updateRowsEvent.RowSet.ColumnNames[0]);
267261
Assert.Equal("value", updateRowsEvent.RowSet.ColumnNames[1]);
@@ -275,14 +269,14 @@ private async Task TestDataType<TDateType>(string tableName, TDateType currentVa
275269
Assert.Equal(updateValue, valueCellValue.NewValue);
276270

277271
// Delete the row
278-
command = _mysqlFixture.CreateCommand();
272+
command = mysqlFixture.CreateCommand();
279273
command.CommandText = $"delete from {tableName} where id = @id";
280274
command.Parameters.AddWithValue("@id", id);
281275

282276
await command.ExecuteNonQueryAsync();
283277

284278
// Validate the DeleteRowsEvent
285-
var deleteRowsEvent = await _mysqlFixture.ReceiveAsync<DeleteRowsEvent>();
279+
var deleteRowsEvent = await mysqlFixture.ReceiveAsync<DeleteRowsEvent>();
286280
Assert.Equal(1, deleteRowsEvent.RowSet.Rows.Count);
287281
Assert.Equal("id", deleteRowsEvent.RowSet.ColumnNames[0]);
288282
Assert.Equal("value", deleteRowsEvent.RowSet.ColumnNames[1]);

tests/Test/MainTest.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,59 +15,57 @@ namespace Test
1515
[Trait("Category", "Replication")]
1616
public class MainTest
1717
{
18-
private readonly MySQLFixture _mysqlFixture = MySQLFixture.Instance;
19-
2018
protected readonly ITestOutputHelper _outputHelper;
2119

22-
private readonly ILogger _logger;
23-
2420
public MainTest(ITestOutputHelper outputHelper)
2521
{
2622
_outputHelper = outputHelper;
27-
using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
28-
_logger = loggerFactory.CreateLogger<MainTest>();
2923
}
3024

3125
[Fact]
3226
public async Task TestReceiveEvent()
3327
{
28+
using var mysqlFixture = MySQLFixture.CreateMySQLFixture();
29+
3430
// insert
35-
var cmd = _mysqlFixture.CreateCommand();
31+
var cmd = mysqlFixture.CreateCommand();
3632
cmd.CommandText = "INSERT INTO pet (name, owner, species, sex, birth, death) values ('Rokie', 'Kerry', 'abc', 'F', '1982-04-20', '3000-01-01'); SELECT LAST_INSERT_ID();";
3733
var id = (UInt64)(await cmd.ExecuteScalarAsync());
3834

3935
// update
40-
cmd = _mysqlFixture.CreateCommand();
36+
cmd = mysqlFixture.CreateCommand();
4137
cmd.CommandText = "update pet set owner='Linda' where `id`=" + id;
4238
await cmd.ExecuteNonQueryAsync();
4339

4440
// delete
45-
cmd = _mysqlFixture.CreateCommand();
41+
cmd = mysqlFixture.CreateCommand();
4642
cmd.CommandText = "delete from pet where `id`= " + id;
4743
await cmd.ExecuteNonQueryAsync();
4844

49-
RowsEvent eventLog = await _mysqlFixture.ReceiveAsync<WriteRowsEvent>();
45+
RowsEvent eventLog = await mysqlFixture.ReceiveAsync<WriteRowsEvent>();
5046
Assert.NotNull(eventLog);
5147
_outputHelper.WriteLine(eventLog.ToString() + "\r\n");
5248

53-
eventLog = await _mysqlFixture.ReceiveAsync<UpdateRowsEvent>();
49+
eventLog = await mysqlFixture.ReceiveAsync<UpdateRowsEvent>();
5450
Assert.NotNull(eventLog);
5551
_outputHelper.WriteLine(eventLog.ToString() + "\r\n");
5652

57-
eventLog = await _mysqlFixture.ReceiveAsync<DeleteRowsEvent>();
53+
eventLog = await mysqlFixture.ReceiveAsync<DeleteRowsEvent>();
5854
Assert.NotNull(eventLog);
5955
_outputHelper.WriteLine(eventLog.ToString() + "\r\n");
6056
}
6157

6258
[Fact]
6359
public async Task TestInsertEvent()
6460
{
61+
using var mysqlFixture = MySQLFixture.CreateMySQLFixture();
62+
6563
// insert
66-
var cmd = _mysqlFixture.CreateCommand();
64+
var cmd = mysqlFixture.CreateCommand();
6765
cmd.CommandText = "INSERT INTO pet (name, owner, species, sex, birth, death, timeUpdated) values ('Rokie', 'Kerry', 'abc', 'F', '1992-05-20', '3000-01-01', now()); SELECT LAST_INSERT_ID();";
6866
var id = (UInt64)(await cmd.ExecuteScalarAsync());
6967

70-
var eventLog = await _mysqlFixture.ReceiveAsync<WriteRowsEvent>();
68+
var eventLog = await mysqlFixture.ReceiveAsync<WriteRowsEvent>();
7169

7270
Assert.NotNull(eventLog);
7371

@@ -85,13 +83,15 @@ public async Task TestInsertEvent()
8583
[Fact]
8684
public async Task TestUpdateEvent()
8785
{
86+
using var mysqlFixture = MySQLFixture.CreateMySQLFixture();
87+
8888
// insert
89-
var cmd = _mysqlFixture.CreateCommand();
89+
var cmd = mysqlFixture.CreateCommand();
9090
cmd.CommandText = "INSERT INTO pet (name, owner, species, sex, birth, death, timeUpdated) values ('Rokie', 'Kerry', 'abc', 'F', '1992-05-20', '3000-01-01', now());";
9191
await cmd.ExecuteNonQueryAsync();
9292

9393
// query
94-
cmd = _mysqlFixture.CreateCommand();
94+
cmd = mysqlFixture.CreateCommand();
9595
cmd.CommandText = "select * from pet order by `id` desc limit 1;";
9696

9797
var oldValues = new Dictionary<string, object>();
@@ -111,11 +111,11 @@ public async Task TestUpdateEvent()
111111
var id = oldValues["id"];
112112

113113
// update
114-
cmd = _mysqlFixture.CreateCommand();
114+
cmd = mysqlFixture.CreateCommand();
115115
cmd.CommandText = "update pet set owner='Linda', timeUpdated=now() where `id`=" + id;
116116
await cmd.ExecuteNonQueryAsync();
117117

118-
var eventLog = await _mysqlFixture.ReceiveAsync<UpdateRowsEvent>();
118+
var eventLog = await mysqlFixture.ReceiveAsync<UpdateRowsEvent>();
119119

120120
_outputHelper.WriteLine(eventLog.ToString() + "\r\n");
121121

@@ -144,7 +144,7 @@ public async Task TestUpdateEvent()
144144
[Fact]
145145
public async Task TestGetEventLogStream()
146146
{
147-
var mysqlFixture = MySQLFixture.CreateMySQLFixture(3);
147+
var mysqlFixture = MySQLFixture.CreateMySQLFixture();
148148

149149
// Insert a new pet
150150
var cmd = mysqlFixture.CreateCommand();

tests/Test/MySQLFixture.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ public class MySQLFixture : IDisposable
1919

2020
private SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
2121

22-
public static MySQLFixture Instance { get; } = new MySQLFixture(1);
23-
24-
internal static MySQLFixture CreateMySQLFixture(int serverId)
22+
internal static MySQLFixture CreateMySQLFixture(int serverId = 1)
2523
{
2624
return new MySQLFixture(serverId);
2725
}

0 commit comments

Comments
 (0)