Merge pull request #184 from mrd0ll4r/tests-smallfix

store: fix various small test issues
This commit is contained in:
Jimmy Zelinskie 2016-07-09 21:58:54 -04:00 committed by GitHub
commit 40d0236596
3 changed files with 32 additions and 14 deletions

View file

@ -145,12 +145,17 @@ type Peer struct {
Port uint16 Port uint16
} }
// Equal reports whether p and x are the same.
func (p Peer) Equal(x Peer) bool {
return p.EqualEndpoint(x) && p.ID == x.ID
}
// EqualEndpoint reports whether p and x have the same endpoint.
func (p Peer) EqualEndpoint(x Peer) bool {
return p.Port == x.Port && p.IP.Equal(x.IP)
}
// Params is used to fetch request parameters. // Params is used to fetch request parameters.
type Params interface { type Params interface {
String(key string) (string, error) String(key string) (string, error)
} }
// Equal reports whether peer and x are the same.
func (peer Peer) Equal(x Peer) bool {
return peer.ID == x.ID && peer.Port == x.Port && peer.IP.Equal(x.IP)
}

View file

@ -857,6 +857,8 @@ func generatePeers() (a [num1KElements]chihaya.Peer) {
func PreparePeerStoreBenchmarker(driver PeerStoreDriver) PeerStoreBenchmarker { func PreparePeerStoreBenchmarker(driver PeerStoreDriver) PeerStoreBenchmarker {
return peerStoreBench{ return peerStoreBench{
driver: driver, driver: driver,
infohashes: generateInfohashes(),
peers: generatePeers(),
} }
} }

View file

@ -359,6 +359,10 @@ func (s *ipStoreTester) TestHasAllHasAnyNetworks(t *testing.T, cfg *DriverConfig
// Every benchmark expects a new, clean storage. Every benchmark should be // Every benchmark expects a new, clean storage. Every benchmark should be
// called with a DriverConfig that ensures this. // called with a DriverConfig that ensures this.
type PeerStoreTester interface { type PeerStoreTester interface {
// CompareEndpoints sets the function used to compare peers to a
// comparison that only compares endpoints and omits PeerIDs.
CompareEndpoints()
TestPeerStore(*testing.T, *DriverConfig) TestPeerStore(*testing.T, *DriverConfig)
} }
@ -366,19 +370,26 @@ var _ PeerStoreTester = &peerStoreTester{}
type peerStoreTester struct { type peerStoreTester struct {
driver PeerStoreDriver driver PeerStoreDriver
equalityFunc func(a, b chihaya.Peer) bool
} }
// PreparePeerStoreTester prepares a reusable suite for PeerStore driver // PreparePeerStoreTester prepares a reusable suite for PeerStore driver
// tests. // tests.
// The tester will use PeerIDs and endpoints to compare peers.
func PreparePeerStoreTester(driver PeerStoreDriver) PeerStoreTester { func PreparePeerStoreTester(driver PeerStoreDriver) PeerStoreTester {
return &peerStoreTester{ return &peerStoreTester{
driver: driver, driver: driver,
equalityFunc: func(a, b chihaya.Peer) bool { return a.Equal(b) },
} }
} }
func peerInSlice(peer chihaya.Peer, peers []chihaya.Peer) bool { func (pt *peerStoreTester) CompareEndpoints() {
pt.equalityFunc = func(a, b chihaya.Peer) bool { return a.EqualEndpoint(b) }
}
func (pt *peerStoreTester) peerInSlice(peer chihaya.Peer, peers []chihaya.Peer) bool {
for _, v := range peers { for _, v := range peers {
if v.Equal(peer) { if pt.equalityFunc(peer, v) {
return true return true
} }
} }
@ -453,9 +464,9 @@ func (pt *peerStoreTester) TestPeerStore(t *testing.T, cfg *DriverConfig) {
} }
if p.seeder { if p.seeder {
require.True(t, peerInSlice(peer, seeders)) require.True(t, pt.peerInSlice(peer, seeders))
} else { } else {
require.True(t, peerInSlice(peer, leechers)) require.True(t, pt.peerInSlice(peer, leechers))
} }
if p.seeder { if p.seeder {
@ -506,8 +517,8 @@ func (pt *peerStoreTester) TestPeerStore(t *testing.T, cfg *DriverConfig) {
err = s.CollectGarbage(time.Now()) err = s.CollectGarbage(time.Now())
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, s.NumLeechers(hash), 0) require.Equal(t, 0, s.NumLeechers(hash))
require.Equal(t, s.NumSeeders(hash), 0) require.Equal(t, 0, s.NumSeeders(hash))
errChan := s.Stop() errChan := s.Stop()
err = <-errChan err = <-errChan