@@ -136,4 +136,53 @@ func TestReplTracker(t *testing.T) {
136136 assert .False (t , rt .hw .isOpen )
137137 assert .False (t , rt .hr .isOpen )
138138 })
139+ t .Run ("metric reset on promotion" , func (t * testing.T ) {
140+ // Clean up the global metric after test
141+ defer replicationLagSeconds .Reset ()
142+
143+ rt := NewReplTracker (env , alias )
144+ rt .InitDBConfig (target , mysqld )
145+
146+ // Start as replica
147+ rt .MakeNonPrimary ()
148+ assert .False (t , rt .isPrimary )
149+
150+ // Simulate having lag (would normally be set by poller)
151+ replicationLagSeconds .Set (42 )
152+ assert .Equal (t , int64 (42 ), replicationLagSeconds .Get ())
153+
154+ // Promote to primary
155+ rt .MakePrimary ()
156+ assert .True (t , rt .isPrimary )
157+
158+ // Verify metric is reset
159+ assert .Equal (t , int64 (0 ), replicationLagSeconds .Get ())
160+
161+ rt .Close ()
162+ })
163+ t .Run ("metric reset on status when primary" , func (t * testing.T ) {
164+ // Clean up the global metric after test
165+ defer replicationLagSeconds .Reset ()
166+
167+ rt := NewReplTracker (env , alias )
168+ rt .InitDBConfig (target , mysqld )
169+
170+ // Set as primary
171+ rt .MakePrimary ()
172+ assert .True (t , rt .isPrimary )
173+
174+ // Simulate metric having a stale value (shouldn't happen, but be defensive)
175+ replicationLagSeconds .Set (99 )
176+ assert .Equal (t , int64 (99 ), replicationLagSeconds .Get ())
177+
178+ // Call Status() which should reset the metric
179+ lag , err := rt .Status ()
180+ assert .NoError (t , err )
181+ assert .Equal (t , time .Duration (0 ), lag )
182+
183+ // Verify metric is reset
184+ assert .Equal (t , int64 (0 ), replicationLagSeconds .Get ())
185+
186+ rt .Close ()
187+ })
139188}
0 commit comments