github.com/ethersphere/bee/v2@v2.2.0/pkg/p2p/libp2p/tracing_test.go (about) 1 // Copyright 2020 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 libp2p_test 6 7 import ( 8 "context" 9 "fmt" 10 "testing" 11 "time" 12 13 "github.com/ethersphere/bee/v2/pkg/p2p" 14 "github.com/ethersphere/bee/v2/pkg/p2p/libp2p" 15 "github.com/ethersphere/bee/v2/pkg/tracing" 16 ) 17 18 func TestTracing(t *testing.T) { 19 t.Parallel() 20 21 tracer1, closer1, err := tracing.NewTracer(&tracing.Options{ 22 Enabled: true, 23 ServiceName: "bee-test", 24 }) 25 if err != nil { 26 t.Fatal(err) 27 } 28 defer closer1.Close() 29 30 tracer2, closer2, err := tracing.NewTracer(&tracing.Options{ 31 Enabled: true, 32 ServiceName: "bee-test", 33 }) 34 if err != nil { 35 t.Fatal(err) 36 } 37 defer closer2.Close() 38 39 s1, overlay1 := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{ 40 FullNode: true, 41 }}) 42 43 s2, _ := newService(t, 1, libp2pServiceOpts{}) 44 45 var handledTracingSpan string 46 handled := make(chan struct{}) 47 if err := s1.AddProtocol(newTestProtocol(func(ctx context.Context, _ p2p.Peer, _ p2p.Stream) error { 48 49 span, _, _ := tracer1.StartSpanFromContext(ctx, "test-p2p-handler", nil) 50 defer span.Finish() 51 52 handledTracingSpan = fmt.Sprint(span.Context()) 53 close(handled) 54 return nil 55 })); err != nil { 56 t.Fatal(err) 57 } 58 59 addr := serviceUnderlayAddress(t, s1) 60 61 connectContext, connectCancel := context.WithCancel(context.Background()) 62 defer connectCancel() 63 64 if _, err := s2.Connect(connectContext, addr); err != nil { 65 t.Fatal(err) 66 } 67 68 ctx, cancel := context.WithCancel(context.Background()) 69 defer cancel() 70 71 span, _, ctx := tracer2.StartSpanFromContext(ctx, "test-p2p-client", nil) 72 defer span.Finish() 73 74 if fmt.Sprint(span.Context()) == "" { 75 t.Error("not tracing span context to send") 76 } 77 78 stream, err := s2.NewStream(ctx, overlay1, nil, testProtocolName, testProtocolVersion, testStreamName) 79 if err != nil { 80 t.Fatal(err) 81 } 82 defer stream.Close() 83 84 select { 85 case <-handled: 86 case <-time.After(30 * time.Second): 87 t.Fatal("timeout waiting for handler") 88 } 89 90 if handledTracingSpan == "" { 91 t.Error("got not tracing span context in handler") 92 } 93 }