-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from grafana/do-not-log-connection-refused-as-…
…warning Do not log 'connection refused' as warning in memberlist client
- Loading branch information
Showing
3 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package memberlist | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grafana/dskit/concurrency" | ||
"github.com/grafana/dskit/flagext" | ||
) | ||
|
||
func TestTCPTransport_WriteTo_ShouldNotLogAsWarningExpectedFailures(t *testing.T) { | ||
tests := map[string]struct { | ||
setup func(t *testing.T, cfg *TCPTransportConfig) | ||
remoteAddr string | ||
expectedLogs string | ||
unexpectedLogs string | ||
}{ | ||
"should not log 'connection refused' by default": { | ||
remoteAddr: "localhost:12345", | ||
unexpectedLogs: "connection refused", | ||
}, | ||
"should log 'connection refused' if debug log level is enabled": { | ||
setup: func(t *testing.T, cfg *TCPTransportConfig) { | ||
cfg.TransportDebug = true | ||
}, | ||
remoteAddr: "localhost:12345", | ||
expectedLogs: "connection refused", | ||
}, | ||
} | ||
|
||
for testName, testData := range tests { | ||
t.Run(testName, func(t *testing.T) { | ||
logs := &concurrency.SyncBuffer{} | ||
logger := log.NewLogfmtLogger(logs) | ||
|
||
cfg := TCPTransportConfig{} | ||
flagext.DefaultValues(&cfg) | ||
cfg.BindAddrs = []string{"localhost"} | ||
cfg.BindPort = 0 | ||
if testData.setup != nil { | ||
testData.setup(t, &cfg) | ||
} | ||
|
||
transport, err := NewTCPTransport(cfg, logger) | ||
require.NoError(t, err) | ||
|
||
_, err = transport.WriteTo([]byte("test"), testData.remoteAddr) | ||
require.NoError(t, err) | ||
|
||
if testData.expectedLogs != "" { | ||
assert.Contains(t, logs.String(), testData.expectedLogs) | ||
} | ||
if testData.unexpectedLogs != "" { | ||
assert.NotContains(t, logs.String(), testData.unexpectedLogs) | ||
} | ||
}) | ||
} | ||
} |