github.com/phillinzzz/newBsc@v1.1.6/eth/protocols/diff/peer_test.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // This file contains some shares testing functionality, common to  multiple
    18  // different files and modules being tested.
    19  
    20  package diff
    21  
    22  import (
    23  	"crypto/rand"
    24  
    25  	"github.com/phillinzzz/newBsc/p2p"
    26  	"github.com/phillinzzz/newBsc/p2p/enode"
    27  )
    28  
    29  // testPeer is a simulated peer to allow testing direct network calls.
    30  type testPeer struct {
    31  	*Peer
    32  
    33  	net p2p.MsgReadWriter // Network layer reader/writer to simulate remote messaging
    34  	app *p2p.MsgPipeRW    // Application layer reader/writer to simulate the local side
    35  }
    36  
    37  // newTestPeer creates a new peer registered at the given data backend.
    38  func newTestPeer(name string, version uint, backend Backend) (*testPeer, <-chan error) {
    39  	// Create a message pipe to communicate through
    40  	app, net := p2p.MsgPipe()
    41  
    42  	// Start the peer on a new thread
    43  	var id enode.ID
    44  	rand.Read(id[:])
    45  
    46  	peer := NewPeer(version, p2p.NewPeer(id, name, nil), net)
    47  	errc := make(chan error, 1)
    48  	go func() {
    49  		errc <- backend.RunPeer(peer, func(peer *Peer) error {
    50  			return Handle(backend, peer)
    51  		})
    52  	}()
    53  	return &testPeer{app: app, net: net, Peer: peer}, errc
    54  }
    55  
    56  // close terminates the local side of the peer, notifying the remote protocol
    57  // manager of termination.
    58  func (p *testPeer) close() {
    59  	p.Peer.Close()
    60  	p.app.Close()
    61  }