github.com/ethersphere/bee/v2@v2.2.0/pkg/p2p/specwrapper_test.go (about) 1 // Copyright 2021 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package p2p_test 6 7 import ( 8 "context" 9 "errors" 10 "testing" 11 "time" 12 13 "github.com/ethersphere/bee/v2/pkg/p2p" 14 ) 15 16 func newTestProtocol(h p2p.HandlerFunc) p2p.ProtocolSpec { 17 return p2p.ProtocolSpec{ 18 Name: "test", 19 Version: "1.0", 20 StreamSpecs: []p2p.StreamSpec{ 21 { 22 Name: "testStreamName1", 23 Handler: h, 24 }, { 25 Name: "testStreamName2", 26 Handler: h, 27 }, 28 }, 29 } 30 } 31 32 func TestBlocklistError(t *testing.T) { 33 t.Parallel() 34 35 tp := newTestProtocol(func(context.Context, p2p.Peer, p2p.Stream) error { 36 return errors.New("test") 37 }) 38 39 p2p.WithBlocklistStreams(1*time.Minute, tp) 40 41 for _, sp := range tp.StreamSpecs { 42 err := sp.Handler(context.Background(), p2p.Peer{}, nil) 43 var discErr *p2p.BlockPeerError 44 if !errors.As(err, &discErr) { 45 t.Error("unexpected error type") 46 } 47 if !errors.Is(err, p2p.ErrUnexpected) { 48 t.Error("unexpected wrapped error type") 49 } 50 } 51 } 52 53 func TestDisconnectError(t *testing.T) { 54 t.Parallel() 55 56 tp := newTestProtocol(func(context.Context, p2p.Peer, p2p.Stream) error { 57 return errors.New("test") 58 }) 59 60 p2p.WithDisconnectStreams(tp) 61 62 for _, sp := range tp.StreamSpecs { 63 err := sp.Handler(context.Background(), p2p.Peer{}, nil) 64 var discErr *p2p.DisconnectError 65 if !errors.As(err, &discErr) { 66 t.Error("unexpected error type") 67 } 68 if !errors.Is(err, p2p.ErrUnexpected) { 69 t.Error("unexpected wrapped error type") 70 } 71 } 72 }