github.com/MetalBlockchain/metalgo@v1.11.9/network/peer/example_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package peer 5 6 import ( 7 "context" 8 "fmt" 9 "net/netip" 10 "time" 11 12 "github.com/MetalBlockchain/metalgo/message" 13 "github.com/MetalBlockchain/metalgo/snow/networking/router" 14 "github.com/MetalBlockchain/metalgo/utils/constants" 15 ) 16 17 func ExampleStartTestPeer() { 18 ctx := context.Background() 19 ctx, cancel := context.WithTimeout(ctx, 15*time.Second) 20 defer cancel() 21 22 peerIP := netip.AddrPortFrom( 23 netip.IPv6Loopback(), 24 9651, 25 ) 26 peer, err := StartTestPeer( 27 ctx, 28 peerIP, 29 constants.LocalID, 30 router.InboundHandlerFunc(func(_ context.Context, msg message.InboundMessage) { 31 fmt.Printf("handling %s\n", msg.Op()) 32 }), 33 ) 34 if err != nil { 35 panic(err) 36 } 37 38 // Send messages here with [peer.Send]. 39 40 peer.StartClose() 41 err = peer.AwaitClosed(ctx) 42 if err != nil { 43 panic(err) 44 } 45 }