github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/rpc/jsonrpc/client/integration_test.go (about) 1 //go:build release 2 // +build release 3 4 // The code in here is comprehensive as an integration 5 // test and is long, hence is only run before releases. 6 7 package client 8 9 import ( 10 "bytes" 11 "context" 12 "errors" 13 "net" 14 "regexp" 15 "testing" 16 "time" 17 18 "github.com/stretchr/testify/require" 19 ) 20 21 func TestWSClientReconnectWithJitter(t *testing.T) { 22 const numClients = 8 23 const maxReconnectAttempts = 3 24 const maxSleepTime = time.Duration(((1<<maxReconnectAttempts)-1)+maxReconnectAttempts) * time.Second 25 26 ctx, cancel := context.WithCancel(context.Background()) 27 defer cancel() 28 29 failDialer := func(net, addr string) (net.Conn, error) { 30 return nil, errors.New("not connected") 31 } 32 33 clientMap := make(map[int]*WSClient) 34 buf := new(bytes.Buffer) 35 for i := 0; i < numClients; i++ { 36 c, err := NewWS("tcp://foo", "/websocket") 37 require.NoError(t, err) 38 c.Dialer = failDialer 39 c.maxReconnectAttempts = maxReconnectAttempts 40 c.Start(ctx) 41 42 // Not invoking defer c.Stop() because 43 // after all the reconnect attempts have been 44 // exhausted, c.Stop is implicitly invoked. 45 clientMap[i] = c 46 // Trigger the reconnect routine that performs exponential backoff. 47 go c.reconnect(ctx) 48 } 49 50 // Next we have to examine the logs to ensure that no single time was repeated 51 backoffDurRegexp := regexp.MustCompile(`backoff_duration=(.+)`) 52 matches := backoffDurRegexp.FindAll(buf.Bytes(), -1) 53 seenMap := make(map[string]int) 54 for i, match := range matches { 55 if origIndex, seen := seenMap[string(match)]; seen { 56 t.Errorf("match #%d (%q) was seen originally at log entry #%d", i, match, origIndex) 57 } else { 58 seenMap[string(match)] = i 59 } 60 } 61 }