@@ -12,14 +12,11 @@ namespace Test
12
12
[ Trait ( "Category" , "Replication" ) ]
13
13
public class BinlogPositionTest
14
14
{
15
- private readonly MySQLFixture _mysqlFixture ;
16
15
protected readonly ITestOutputHelper _outputHelper ;
17
16
18
17
public BinlogPositionTest ( ITestOutputHelper outputHelper )
19
18
{
20
19
_outputHelper = outputHelper ;
21
- _mysqlFixture = MySQLFixture . Instance ;
22
- using var loggerFactory = LoggerFactory . Create ( builder => builder . AddConsole ( ) ) ;
23
20
}
24
21
25
22
// Unit tests for BinlogPosition class
@@ -98,13 +95,15 @@ public void Properties_CanBeModified()
98
95
[ Fact ]
99
96
public async Task GetCurrentBinlogPosition_ReturnsValidPosition ( )
100
97
{
98
+ using var mysqlFixture = MySQLFixture . CreateMySQLFixture ( ) ;
99
+
101
100
// Execute a simple command to ensure we have binlog activity
102
- var cmd = _mysqlFixture . CreateCommand ( ) ;
101
+ var cmd = mysqlFixture . CreateCommand ( ) ;
103
102
cmd . CommandText = "SELECT 1" ;
104
103
await cmd . ExecuteScalarAsync ( ) ;
105
104
106
105
// Get the current binlog position
107
- var client = _mysqlFixture . Client ;
106
+ var client = mysqlFixture . Client ;
108
107
var currentPosition = client . CurrentPosition ;
109
108
110
109
// Assert
@@ -117,18 +116,20 @@ public async Task GetCurrentBinlogPosition_ReturnsValidPosition()
117
116
[ Fact ]
118
117
public async Task BinlogPosition_ShouldAdvanceAfterOperations ( )
119
118
{
119
+ using var mysqlFixture = MySQLFixture . CreateMySQLFixture ( ) ;
120
+
120
121
// Get the current binlog position to use as reference
121
- var client = _mysqlFixture . Client ;
122
+ var client = mysqlFixture . Client ;
122
123
var initialPosition = client . CurrentPosition ;
123
124
_outputHelper . WriteLine ( $ "Initial binlog position: { initialPosition } ") ;
124
125
125
126
// Execute an operation to advance binlog position
126
- var cmd = _mysqlFixture . CreateCommand ( ) ;
127
+ var cmd = mysqlFixture . CreateCommand ( ) ;
127
128
cmd . CommandText = "INSERT INTO pet (name, owner, species) VALUES ('TestPet', 'TestOwner', 'TestSpecies')" ;
128
129
await cmd . ExecuteNonQueryAsync ( ) ;
129
130
130
131
// Receive an event to ensure binlog has progressed
131
- var eventLog = await _mysqlFixture . ReceiveAsync < WriteRowsEvent > ( ) ;
132
+ var eventLog = await mysqlFixture . ReceiveAsync < WriteRowsEvent > ( ) ;
132
133
Assert . NotNull ( eventLog ) ;
133
134
134
135
// Check that position has advanced
@@ -147,8 +148,10 @@ public async Task BinlogPosition_ShouldAdvanceAfterOperations()
147
148
[ Fact ]
148
149
public async Task PositionChanged_EventFires_WhenPositionChanges ( )
149
150
{
151
+ using var mysqlFixture = MySQLFixture . CreateMySQLFixture ( ) ;
152
+
150
153
// Set up event handler to detect position changes
151
- var client = _mysqlFixture . Client ;
154
+ var client = mysqlFixture . Client ;
152
155
var positionChangedEvent = new ManualResetEventSlim ( false ) ;
153
156
BinlogPosition capturedPosition = null ;
154
157
@@ -162,11 +165,11 @@ public async Task PositionChanged_EventFires_WhenPositionChanges()
162
165
try
163
166
{
164
167
// Execute an operation that will trigger binlog position change
165
- var cmd = _mysqlFixture . CreateCommand ( ) ;
168
+ var cmd = mysqlFixture . CreateCommand ( ) ;
166
169
cmd . CommandText = "INSERT INTO pet (name, owner, species) VALUES ('EventTest', 'EventOwner', 'EventSpecies')" ;
167
170
await cmd . ExecuteNonQueryAsync ( ) ;
168
171
169
- await _mysqlFixture . ReceiveAsync < WriteRowsEvent > ( ) ;
172
+ await mysqlFixture . ReceiveAsync < WriteRowsEvent > ( ) ;
170
173
171
174
// Wait for the position changed event to fire
172
175
var eventFired = positionChangedEvent . Wait ( TimeSpan . FromSeconds ( 5 ) ) ;
@@ -186,18 +189,20 @@ public async Task PositionChanged_EventFires_WhenPositionChanges()
186
189
[ Fact ]
187
190
public async Task RotateEvent_UpdatesBinlogPosition ( )
188
191
{
192
+ using var mysqlFixture = MySQLFixture . CreateMySQLFixture ( ) ;
193
+
189
194
// Force a log rotation
190
- var cmd = _mysqlFixture . CreateCommand ( ) ;
195
+ var cmd = mysqlFixture . CreateCommand ( ) ;
191
196
cmd . CommandText = "FLUSH LOGS" ;
192
197
await cmd . ExecuteNonQueryAsync ( ) ;
193
198
194
199
// We should receive a rotate event
195
- var rotateEvent = await _mysqlFixture . ReceiveAsync < RotateEvent > ( ) ;
200
+ var rotateEvent = await mysqlFixture . ReceiveAsync < RotateEvent > ( ) ;
196
201
Assert . NotNull ( rotateEvent ) ;
197
202
_outputHelper . WriteLine ( $ "Received rotate event with next log: { rotateEvent . NextBinlogFileName } at position: { rotateEvent . RotatePosition } ") ;
198
203
199
204
// Get current position after rotation
200
- var currentPosition = _mysqlFixture . Client . CurrentPosition ;
205
+ var currentPosition = mysqlFixture . Client . CurrentPosition ;
201
206
_outputHelper . WriteLine ( $ "Current binlog position after rotation: { currentPosition } ") ;
202
207
203
208
// The rotate event's next binlog file name should match our current filename
@@ -207,8 +212,10 @@ public async Task RotateEvent_UpdatesBinlogPosition()
207
212
[ Fact ]
208
213
public async Task ConnectWithBinlogPosition_ShouldStartFromSpecifiedPosition ( )
209
214
{
215
+ using var mysqlFixture = MySQLFixture . CreateMySQLFixture ( ) ;
216
+
210
217
// 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 ;
212
219
_outputHelper . WriteLine ( $ "Current binlog position: { currentPosition } ") ;
213
220
214
221
// Create a new replication client
@@ -235,7 +242,7 @@ public async Task ConnectWithBinlogPosition_ShouldStartFromSpecifiedPosition()
235
242
_outputHelper . WriteLine ( $ "Successfully connected with position: { newClient . CurrentPosition } ") ;
236
243
237
244
// Now insert a record to generate a new event
238
- var cmd = _mysqlFixture . CreateCommand ( ) ;
245
+ var cmd = mysqlFixture . CreateCommand ( ) ;
239
246
cmd . CommandText = "INSERT INTO pet (name, owner, species) VALUES ('PositionTest', 'PositionTestOwner', 'PositionTestSpecies')" ;
240
247
await cmd . ExecuteNonQueryAsync ( ) ;
241
248
0 commit comments