@@ -224,3 +224,38 @@ func TestChangeNotifierNormal(t *testing.T) {
224
224
})
225
225
}
226
226
}
227
+
228
+ func TestChangeNotifierSetPreference (t * testing.T ) {
229
+ ctrl := gomock .NewController (t )
230
+
231
+ tvm := blockmock .NewFullVM (ctrl )
232
+ tvm .EXPECT ().SetPreference (gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
233
+
234
+ var invoked bool
235
+ nf := block.ChangeNotifier {
236
+ OnChange : func () {
237
+ invoked = true
238
+ },
239
+ ChainVM : tvm ,
240
+ }
241
+
242
+ // First time SetPreference is called, it should invoke OnChange
243
+ require .NoError (t , nf .SetPreference (context .Background (), ids .Empty ), "expected SetPreference to succeed" )
244
+ require .True (t , invoked , "expected to have been invoked on first SetPreference call" )
245
+
246
+ invoked = false
247
+ // Second time SetPreference is called with the same block ID, it should not invoke OnChange
248
+ require .NoError (t , nf .SetPreference (context .Background (), ids .Empty ), "expected SetPreference to succeed on second call with same block ID" )
249
+ require .False (t , invoked , "expected not to have been invoked on second SetPreference call with same block ID" )
250
+
251
+ invoked = false
252
+ // Third time SetPreference is called with a different block ID, it should invoke OnChange again
253
+ testID := ids .GenerateTestID ()
254
+ require .NoError (t , nf .SetPreference (context .Background (), testID ), "expected SetPreference to succeed on third call with different block ID" )
255
+ require .True (t , invoked , "expected to have been invoked on third SetPreference call with different block ID" )
256
+
257
+ invoked = false
258
+ // Fourth time SetPreference is called with the same block ID, it should not invoke OnChange
259
+ require .NoError (t , nf .SetPreference (context .Background (), testID ), "expected SetPreference to succeed on fourth call with same block ID" )
260
+ require .False (t , invoked , "expected not to have been invoked on fourth SetPreference call with same block ID" )
261
+ }
0 commit comments