status-go/services/shhext/mailservers/peerstore_test.go

60 lines
1.5 KiB
Go

package mailservers
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/status-im/status-go/eth-node/types"
)
func RandomNode() (*enode.Node, error) {
pkey, err := crypto.GenerateKey()
if err != nil {
return nil, err
}
return enode.NewV4(&pkey.PublicKey, nil, 0, 0), nil
}
func TestUpdateResetsInternalStorage(t *testing.T) {
store := NewPeerStore(newInMemCache(t))
r1, err := RandomNode()
require.NoError(t, err)
r2, err := RandomNode()
require.NoError(t, err)
require.NoError(t, store.Update([]*enode.Node{r1, r2}))
require.True(t, store.Exist(types.EnodeID(r1.ID())))
require.True(t, store.Exist(types.EnodeID(r2.ID())))
require.NoError(t, store.Update([]*enode.Node{r2}))
require.False(t, store.Exist(types.EnodeID(r1.ID())))
require.True(t, store.Exist(types.EnodeID(r2.ID())))
}
func TestGetNodeByID(t *testing.T) {
store := NewPeerStore(newInMemCache(t))
r1, err := RandomNode()
require.NoError(t, err)
require.NoError(t, store.Update([]*enode.Node{r1}))
require.Equal(t, r1, store.Get(types.EnodeID(r1.ID())))
require.Nil(t, store.Get(types.EnodeID{1}))
}
type fakePeerProvider struct {
peers []*p2p.Peer
}
func (f fakePeerProvider) Peers() []*p2p.Peer {
return f.peers
}
func TestNoConnected(t *testing.T) {
provider := fakePeerProvider{}
store := NewPeerStore(newInMemCache(t))
_, err := GetFirstConnected(provider, store)
require.EqualError(t, ErrNoConnected, err.Error())
}