diff --git a/plugin/evm/validators/mock_listener.go b/plugin/evm/validators/mock_listener.go new file mode 100644 index 0000000000..d67703007d --- /dev/null +++ b/plugin/evm/validators/mock_listener.go @@ -0,0 +1,76 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/ava-labs/subnet-evm/plugin/evm/validators (interfaces: StateCallbackListener) +// +// Generated by this command: +// +// mockgen -package=validators -destination=plugin/evm/validators/mock_listener.go github.com/ava-labs/subnet-evm/plugin/evm/validators StateCallbackListener +// + +// Package validators is a generated GoMock package. +package validators + +import ( + reflect "reflect" + + ids "github.com/ava-labs/avalanchego/ids" + gomock "go.uber.org/mock/gomock" +) + +// MockStateCallbackListener is a mock of StateCallbackListener interface. +type MockStateCallbackListener struct { + ctrl *gomock.Controller + recorder *MockStateCallbackListenerMockRecorder +} + +// MockStateCallbackListenerMockRecorder is the mock recorder for MockStateCallbackListener. +type MockStateCallbackListenerMockRecorder struct { + mock *MockStateCallbackListener +} + +// NewMockStateCallbackListener creates a new mock instance. +func NewMockStateCallbackListener(ctrl *gomock.Controller) *MockStateCallbackListener { + mock := &MockStateCallbackListener{ctrl: ctrl} + mock.recorder = &MockStateCallbackListenerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockStateCallbackListener) EXPECT() *MockStateCallbackListenerMockRecorder { + return m.recorder +} + +// OnValidatorAdded mocks base method. +func (m *MockStateCallbackListener) OnValidatorAdded(arg0 ids.ID, arg1 ids.NodeID, arg2 uint64, arg3 bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "OnValidatorAdded", arg0, arg1, arg2, arg3) +} + +// OnValidatorAdded indicates an expected call of OnValidatorAdded. +func (mr *MockStateCallbackListenerMockRecorder) OnValidatorAdded(arg0, arg1, arg2, arg3 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OnValidatorAdded", reflect.TypeOf((*MockStateCallbackListener)(nil).OnValidatorAdded), arg0, arg1, arg2, arg3) +} + +// OnValidatorRemoved mocks base method. +func (m *MockStateCallbackListener) OnValidatorRemoved(arg0 ids.ID, arg1 ids.NodeID) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "OnValidatorRemoved", arg0, arg1) +} + +// OnValidatorRemoved indicates an expected call of OnValidatorRemoved. +func (mr *MockStateCallbackListenerMockRecorder) OnValidatorRemoved(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OnValidatorRemoved", reflect.TypeOf((*MockStateCallbackListener)(nil).OnValidatorRemoved), arg0, arg1) +} + +// OnValidatorStatusUpdated mocks base method. +func (m *MockStateCallbackListener) OnValidatorStatusUpdated(arg0 ids.ID, arg1 ids.NodeID, arg2 bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "OnValidatorStatusUpdated", arg0, arg1, arg2) +} + +// OnValidatorStatusUpdated indicates an expected call of OnValidatorStatusUpdated. +func (mr *MockStateCallbackListenerMockRecorder) OnValidatorStatusUpdated(arg0, arg1, arg2 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OnValidatorStatusUpdated", reflect.TypeOf((*MockStateCallbackListener)(nil).OnValidatorStatusUpdated), arg0, arg1, arg2) +} diff --git a/plugin/evm/validators/state_test.go b/plugin/evm/validators/state_test.go index 2babe1d6ab..ecfd7d34a9 100644 --- a/plugin/evm/validators/state_test.go +++ b/plugin/evm/validators/state_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" "github.com/ava-labs/avalanchego/codec" "github.com/ava-labs/avalanchego/database" @@ -216,71 +217,34 @@ func TestStateListener(t *testing.T) { db := memdb.New() state, err := NewState(db) require.NoError(err) + ctrl := gomock.NewController(t) + defer ctrl.Finish() expectedvID := ids.GenerateTestID() expectedNodeID := ids.GenerateTestNodeID() expectedStartTime := time.Now() + mockListener := NewMockStateCallbackListener(ctrl) + // add initial validator to test RegisterListener + initialvID := ids.GenerateTestID() + initialNodeID := ids.GenerateTestNodeID() + initialStartTime := time.Now() - // add listener - listener := &testCallbackListener{ - t: t, - onAdd: func(vID ids.ID, nodeID ids.NodeID, startTime uint64, isActive bool) { - require.Equal(expectedvID, vID) - require.Equal(expectedNodeID, nodeID) - require.Equal(uint64(expectedStartTime.Unix()), startTime) - require.True(isActive) - }, - onRemove: func(vID ids.ID, nodeID ids.NodeID) { - require.Equal(expectedvID, vID) - require.Equal(expectedNodeID, nodeID) - }, - onStatusUpdate: func(vID ids.ID, nodeID ids.NodeID, isActive bool) { - require.Equal(expectedvID, vID) - require.Equal(expectedNodeID, nodeID) - require.False(isActive) - }, - } - state.RegisterListener(listener) + // add initial validator + require.NoError(state.AddValidator(initialvID, initialNodeID, uint64(initialStartTime.Unix()), true)) + + // register listener + mockListener.EXPECT().OnValidatorAdded(initialvID, initialNodeID, uint64(initialStartTime.Unix()), true) + state.RegisterListener(mockListener) // add new validator + mockListener.EXPECT().OnValidatorAdded(expectedvID, expectedNodeID, uint64(expectedStartTime.Unix()), true) require.NoError(state.AddValidator(expectedvID, expectedNodeID, uint64(expectedStartTime.Unix()), true)) // set status + mockListener.EXPECT().OnValidatorStatusUpdated(expectedvID, expectedNodeID, false) require.NoError(state.SetStatus(expectedvID, false)) // remove validator + mockListener.EXPECT().OnValidatorRemoved(expectedvID, expectedNodeID) require.NoError(state.DeleteValidator(expectedvID)) } - -var _ StateCallbackListener = (*testCallbackListener)(nil) - -type testCallbackListener struct { - t *testing.T - onAdd func(vID ids.ID, nodeID ids.NodeID, startTime uint64, isActive bool) - onRemove func(ids.ID, ids.NodeID) - onStatusUpdate func(ids.ID, ids.NodeID, bool) -} - -func (t *testCallbackListener) OnValidatorAdded(vID ids.ID, nodeID ids.NodeID, startTime uint64, isActive bool) { - if t.onAdd != nil { - t.onAdd(vID, nodeID, startTime, isActive) - } else { - t.t.Fail() - } -} - -func (t *testCallbackListener) OnValidatorRemoved(vID ids.ID, nodeID ids.NodeID) { - if t.onRemove != nil { - t.onRemove(vID, nodeID) - } else { - t.t.Fail() - } -} - -func (t *testCallbackListener) OnValidatorStatusUpdated(vID ids.ID, nodeID ids.NodeID, isActive bool) { - if t.onStatusUpdate != nil { - t.onStatusUpdate(vID, nodeID, isActive) - } else { - t.t.Fail() - } -} diff --git a/scripts/mocks.mockgen.txt b/scripts/mocks.mockgen.txt index 391dc8e13c..73aa62ccd7 100644 --- a/scripts/mocks.mockgen.txt +++ b/scripts/mocks.mockgen.txt @@ -1,2 +1,3 @@ github.com/ava-labs/subnet-evm/precompile/precompileconfig=Predicater,Config,ChainConfig,Accepter=precompile/precompileconfig/mocks.go github.com/ava-labs/subnet-evm/precompile/contract=BlockContext,AccessibleState,StateDB=precompile/contract/mocks.go +github.com/ava-labs/subnet-evm/plugin/evm/validators=StateCallbackListener=plugin/evm/validators/mock_listener.go