MCPcopy
hub / github.com/grafana/dskit / TestTCPTransportWriteToUnreachableAddr

Function TestTCPTransportWriteToUnreachableAddr

kv/memberlist/tcp_transport_test.go:81–122  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

79}
80
81func TestTCPTransportWriteToUnreachableAddr(t *testing.T) {
82 writeCt := 50
83
84 // Listen for TCP connections on a random port
85 listener, err := net.Listen("tcp", "127.0.0.1:0")
86 require.NoError(t, err)
87 defer listener.Close()
88
89 logs := &concurrency.SyncBuffer{}
90 logger := log.NewLogfmtLogger(logs)
91
92 cfg := TCPTransportConfig{}
93 flagext.DefaultValues(&cfg)
94 cfg.BindAddrs = getLocalhostAddrs()
95 cfg.MaxConcurrentWrites = writeCt
96 cfg.PacketDialTimeout = 500 * time.Millisecond
97 transport, err := NewTCPTransport(cfg, logger, nil)
98 require.NoError(t, err)
99
100 // Configure TLS only for writes. The dialing should timeout (because of the timeoutReader)
101 transport.cfg.TLSEnabled = true
102 transport.cfg.TLS = tls.ClientConfig{
103 Reader: &timeoutReader{},
104 CertPath: "fake",
105 KeyPath: "fake",
106 CAPath: "fake",
107 }
108
109 timeStart := time.Now()
110
111 for i := 0; i < writeCt; i++ {
112 _, err = transport.WriteTo([]byte("test"), listener.Addr().String())
113 require.NoError(t, err)
114 }
115
116 require.NoError(t, transport.Shutdown())
117
118 gotErrorCt := strings.Count(logs.String(), "context deadline exceeded")
119 assert.Equal(t, writeCt, gotErrorCt, "expected %d errors, got %d", writeCt, gotErrorCt)
120 assert.GreaterOrEqual(t, time.Since(timeStart), 500*time.Millisecond, "expected to take at least 500ms (timeout duration)")
121 assert.LessOrEqual(t, time.Since(timeStart), 2*time.Second, "expected to take less than 2s (timeout + a good margin), writing to unreachable addresses should not block")
122}
123
124func TestTCPTransportWriterAcquireTimeout(t *testing.T) {
125 // Listen for TCP connections on a random port

Callers

nothing calls this directly

Calls 10

WriteToMethod · 0.95
ShutdownMethod · 0.95
StringMethod · 0.95
DefaultValuesFunction · 0.92
getLocalhostAddrsFunction · 0.85
NewTCPTransportFunction · 0.85
CloseMethod · 0.65
StringMethod · 0.65
CountMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected