1package telemetry23import (4 "net"5 "testing"6 "time"7)89func TestOTLPCloseIsBounded(t *testing.T) {10 c := newCollector(t)11 c.delay = 2 * time.Second12 otlpSetup(t, OTLPConfig{Endpoint: c.srv.URL, Timeout: 100 * time.Millisecond})13 begin := time.Now()14 for range 100 {15 Start("sort").End() // never waits on the network16 }17 if d := time.Since(begin); d > 100*time.Millisecond {18 t.Errorf("100 spans took %v", d)19 }20 Close()21 if d := time.Since(begin); d > 1500*time.Millisecond {22 t.Errorf("Close with a stuck collector took %v", d)23 }24}2526// An endpoint that can't be reached (an inherited OTEL_* variable naming27// a collector that's gone) costs nothing at exit once a send has failed.28func TestOTLPCloseSkipsADeadEndpoint(t *testing.T) {29 l, err := net.Listen("tcp", "127.0.0.1:0")30 if err != nil {31 t.Fatal(err)32 }33 dead := "http://" + l.Addr().String()34 l.Close() // nothing listens there now35 otlpSetup(t, OTLPConfig{Endpoint: dead, Timeout: 3 * time.Second})36 for range batchSize {37 Start("sort").End()38 }39 deadline := time.Now().Add(5 * time.Second)40 for {41 exp.mu.Lock()42 down := exp.down43 exp.mu.Unlock()44 if down {45 break46 }47 if time.Now().After(deadline) {48 t.Fatal("the failed send was never noticed")49 }50 time.Sleep(10 * time.Millisecond)51 }52 Start("sort").End()53 begin := time.Now()54 Close()55 if d := time.Since(begin); d > 200*time.Millisecond {56 t.Errorf("Close after a failed send took %v", d)57 }58}