Commit 539b7504 authored by Andreas Bigger's avatar Andreas Bigger

remove band scorer mock

parent 4b0f4739
// Code generated by mockery v2.22.1. DO NOT EDIT.
package mocks
import mock "github.com/stretchr/testify/mock"
// BandScorer is an autogenerated mock type for the BandScorer type
type BandScorer struct {
mock.Mock
}
// Bucket provides a mock function with given fields: score
func (_m *BandScorer) Bucket(score float64) string {
ret := _m.Called(score)
var r0 string
if rf, ok := ret.Get(0).(func(float64) string); ok {
r0 = rf(score)
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// Parse provides a mock function with given fields: str
func (_m *BandScorer) Parse(str string) error {
ret := _m.Called(str)
var r0 error
if rf, ok := ret.Get(0).(func(string) error); ok {
r0 = rf(str)
} else {
r0 = ret.Error(0)
}
return r0
}
// Reset provides a mock function with given fields:
func (_m *BandScorer) Reset() {
_m.Called()
}
type mockConstructorTestingTNewBandScorer interface {
mock.TestingT
Cleanup(func())
}
// NewBandScorer creates a new instance of BandScorer. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
func NewBandScorer(t mockConstructorTestingTNewBandScorer) *BandScorer {
mock := &BandScorer{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}
...@@ -19,6 +19,7 @@ type scorer struct { ...@@ -19,6 +19,7 @@ type scorer struct {
bandScoreThresholds BandScorer bandScoreThresholds BandScorer
} }
// scorePair holds a band and its corresponding threshold.
type scorePair struct { type scorePair struct {
band string band string
threshold float64 threshold float64
...@@ -36,8 +37,6 @@ type BandScoreThresholds struct { ...@@ -36,8 +37,6 @@ type BandScoreThresholds struct {
// Implementations are expected to construct internals using the // Implementations are expected to construct internals using the
// [Parse] function and then expose the [Bucket] function for // [Parse] function and then expose the [Bucket] function for
// downstream [BandScorer] consumers. // downstream [BandScorer] consumers.
//
//go:generate mockery --name BandScorer --output mocks/
type BandScorer interface { type BandScorer interface {
Parse(str string) error Parse(str string) error
Bucket(score float64) string Bucket(score float64) string
......
...@@ -17,11 +17,11 @@ type PeerScorerTestSuite struct { ...@@ -17,11 +17,11 @@ type PeerScorerTestSuite struct {
suite.Suite suite.Suite
// mockConnGater *p2pMocks.ConnectionGater // mockConnGater *p2pMocks.ConnectionGater
mockGater *p2pMocks.PeerGater mockGater *p2pMocks.PeerGater
mockStore *p2pMocks.Peerstore mockStore *p2pMocks.Peerstore
mockMetricer *p2pMocks.GossipMetricer mockMetricer *p2pMocks.GossipMetricer
mockBandScorer *p2pMocks.BandScorer bandScorer p2p.BandScorer
logger log.Logger logger log.Logger
} }
// SetupTest sets up the test suite. // SetupTest sets up the test suite.
...@@ -29,7 +29,8 @@ func (testSuite *PeerScorerTestSuite) SetupTest() { ...@@ -29,7 +29,8 @@ func (testSuite *PeerScorerTestSuite) SetupTest() {
testSuite.mockGater = &p2pMocks.PeerGater{} testSuite.mockGater = &p2pMocks.PeerGater{}
testSuite.mockStore = &p2pMocks.Peerstore{} testSuite.mockStore = &p2pMocks.Peerstore{}
testSuite.mockMetricer = &p2pMocks.GossipMetricer{} testSuite.mockMetricer = &p2pMocks.GossipMetricer{}
testSuite.mockBandScorer = &p2pMocks.BandScorer{} testSuite.bandScorer = &p2p.BandScoreThresholds{}
testSuite.NoError(testSuite.bandScorer.Parse("0:graylist;"))
testSuite.logger = testlog.Logger(testSuite.T(), log.LvlError) testSuite.logger = testlog.Logger(testSuite.T(), log.LvlError)
} }
...@@ -38,45 +39,41 @@ func TestPeerScorer(t *testing.T) { ...@@ -38,45 +39,41 @@ func TestPeerScorer(t *testing.T) {
suite.Run(t, new(PeerScorerTestSuite)) suite.Run(t, new(PeerScorerTestSuite))
} }
// TestPeerScorerOnConnect ensures we can call the OnConnect method on the peer scorer. // TestScorer_OnConnect ensures we can call the OnConnect method on the peer scorer.
func (testSuite *PeerScorerTestSuite) TestPeerScorerOnConnect() { func (testSuite *PeerScorerTestSuite) TestScorer_OnConnect() {
scorer := p2p.NewScorer( scorer := p2p.NewScorer(
testSuite.mockGater, testSuite.mockGater,
testSuite.mockStore, testSuite.mockStore,
testSuite.mockMetricer, testSuite.mockMetricer,
testSuite.mockBandScorer, testSuite.bandScorer,
testSuite.logger, testSuite.logger,
) )
scorer.OnConnect() scorer.OnConnect()
} }
// TestPeerScorerOnDisconnect ensures we can call the OnDisconnect method on the peer scorer. // TestScorer_OnDisconnect ensures we can call the OnDisconnect method on the peer scorer.
func (testSuite *PeerScorerTestSuite) TestPeerScorerOnDisconnect() { func (testSuite *PeerScorerTestSuite) TestScorer_OnDisconnect() {
scorer := p2p.NewScorer( scorer := p2p.NewScorer(
testSuite.mockGater, testSuite.mockGater,
testSuite.mockStore, testSuite.mockStore,
testSuite.mockMetricer, testSuite.mockMetricer,
testSuite.mockBandScorer, testSuite.bandScorer,
testSuite.logger, testSuite.logger,
) )
scorer.OnDisconnect() scorer.OnDisconnect()
} }
// TestSnapshotHook tests running the snapshot hook on the peer scorer. // TestScorer_SnapshotHook tests running the snapshot hook on the peer scorer.
func (testSuite *PeerScorerTestSuite) TestSnapshotHook() { func (testSuite *PeerScorerTestSuite) TestScorer_SnapshotHook() {
scorer := p2p.NewScorer( scorer := p2p.NewScorer(
testSuite.mockGater, testSuite.mockGater,
testSuite.mockStore, testSuite.mockStore,
testSuite.mockMetricer, testSuite.mockMetricer,
testSuite.mockBandScorer, testSuite.bandScorer,
testSuite.logger, testSuite.logger,
) )
inspectFn := scorer.SnapshotHook() inspectFn := scorer.SnapshotHook()
// Mock the band scorer calls
testSuite.mockBandScorer.On("Reset").Return(nil)
testSuite.mockBandScorer.On("Bucket", float64(-100)).Return("graylist")
// Mock the peer gater call // Mock the peer gater call
testSuite.mockGater.On("Update", peer.ID("peer1"), float64(-100)).Return(nil) testSuite.mockGater.On("Update", peer.ID("peer1"), float64(-100)).Return(nil)
...@@ -94,22 +91,18 @@ func (testSuite *PeerScorerTestSuite) TestSnapshotHook() { ...@@ -94,22 +91,18 @@ func (testSuite *PeerScorerTestSuite) TestSnapshotHook() {
inspectFn(snapshotMap) inspectFn(snapshotMap)
} }
// TestSnapshotHookBlockPeer tests running the snapshot hook on the peer scorer with a peer score below the threshold. // TestScorer_SnapshotHookBlocksPeer tests running the snapshot hook on the peer scorer with a peer score below the threshold.
// This implies that the peer should be blocked. // This implies that the peer should be blocked.
func (testSuite *PeerScorerTestSuite) TestSnapshotHookBlockPeer() { func (testSuite *PeerScorerTestSuite) TestScorer_SnapshotHookBlocksPeer() {
scorer := p2p.NewScorer( scorer := p2p.NewScorer(
testSuite.mockGater, testSuite.mockGater,
testSuite.mockStore, testSuite.mockStore,
testSuite.mockMetricer, testSuite.mockMetricer,
testSuite.mockBandScorer, testSuite.bandScorer,
testSuite.logger, testSuite.logger,
) )
inspectFn := scorer.SnapshotHook() inspectFn := scorer.SnapshotHook()
// Mock the band scorer calls
testSuite.mockBandScorer.On("Reset").Return(nil)
testSuite.mockBandScorer.On("Bucket", float64(-101)).Return("graylist")
// Mock the peer gater call // Mock the peer gater call
testSuite.mockGater.On("Update", peer.ID("peer1"), float64(-101)).Return(nil) testSuite.mockGater.On("Update", peer.ID("peer1"), float64(-101)).Return(nil)
......
...@@ -27,11 +27,11 @@ import ( ...@@ -27,11 +27,11 @@ import (
type PeerScoresTestSuite struct { type PeerScoresTestSuite struct {
suite.Suite suite.Suite
mockGater *p2pMocks.ConnectionGater mockGater *p2pMocks.ConnectionGater
mockStore *p2pMocks.Peerstore mockStore *p2pMocks.Peerstore
mockMetricer *p2pMocks.GossipMetricer mockMetricer *p2pMocks.GossipMetricer
mockBandScorer *p2pMocks.BandScorer bandScorer p2p.BandScorer
logger log.Logger logger log.Logger
} }
// SetupTest sets up the test suite. // SetupTest sets up the test suite.
...@@ -39,7 +39,8 @@ func (testSuite *PeerScoresTestSuite) SetupTest() { ...@@ -39,7 +39,8 @@ func (testSuite *PeerScoresTestSuite) SetupTest() {
testSuite.mockGater = &p2pMocks.ConnectionGater{} testSuite.mockGater = &p2pMocks.ConnectionGater{}
testSuite.mockStore = &p2pMocks.Peerstore{} testSuite.mockStore = &p2pMocks.Peerstore{}
testSuite.mockMetricer = &p2pMocks.GossipMetricer{} testSuite.mockMetricer = &p2pMocks.GossipMetricer{}
testSuite.mockBandScorer = &p2pMocks.BandScorer{} testSuite.bandScorer = &p2p.BandScoreThresholds{}
testSuite.NoError(testSuite.bandScorer.Parse("0:graylist;"))
testSuite.logger = testlog.Logger(testSuite.T(), log.LvlError) testSuite.logger = testlog.Logger(testSuite.T(), log.LvlError)
} }
...@@ -70,7 +71,7 @@ func newGossipSubs(testSuite *PeerScoresTestSuite, ctx context.Context, hosts [] ...@@ -70,7 +71,7 @@ func newGossipSubs(testSuite *PeerScoresTestSuite, ctx context.Context, hosts []
rt := pubsub.DefaultGossipSubRouter(h) rt := pubsub.DefaultGossipSubRouter(h)
opts := []pubsub.Option{} opts := []pubsub.Option{}
opts = append(opts, p2p.ConfigurePeerScoring(h, testSuite.mockGater, &p2p.Config{ opts = append(opts, p2p.ConfigurePeerScoring(h, testSuite.mockGater, &p2p.Config{
BandScoreThresholds: testSuite.mockBandScorer, BandScoreThresholds: testSuite.bandScorer,
PeerScoring: pubsub.PeerScoreParams{ PeerScoring: pubsub.PeerScoreParams{
AppSpecificScore: func(p peer.ID) float64 { AppSpecificScore: func(p peer.ID) float64 {
if p == hosts[0].ID() { if p == hosts[0].ID() {
...@@ -121,9 +122,6 @@ func (testSuite *PeerScoresTestSuite) TestNegativeScores() { ...@@ -121,9 +122,6 @@ func (testSuite *PeerScoresTestSuite) TestNegativeScores() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
testSuite.mockBandScorer.On("Reset").Return(nil)
testSuite.mockBandScorer.On("Bucket", float64(0)).Return("nopx")
testSuite.mockBandScorer.On("Bucket", float64(-1000)).Return("graylist")
testSuite.mockMetricer.On("SetPeerScores", mock.Anything).Return(nil) testSuite.mockMetricer.On("SetPeerScores", mock.Anything).Return(nil)
testSuite.mockGater.On("ListBlockedPeers").Return([]peer.ID{}) testSuite.mockGater.On("ListBlockedPeers").Return([]peer.ID{})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment