github.com/okex/exchain@v1.8.0/libs/tendermint/rpc/jsonrpc/server/http_server_test.go (about)

     1  package server
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"net"
     9  	"net/http"
    10  	"sync"
    11  	"sync/atomic"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  
    18  	"github.com/okex/exchain/libs/tendermint/libs/log"
    19  )
    20  
    21  func TestMaxOpenConnections(t *testing.T) {
    22  	const max = 5 // max simultaneous connections
    23  
    24  	// Start the server.
    25  	var open int32
    26  	mux := http.NewServeMux()
    27  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    28  		if n := atomic.AddInt32(&open, 1); n > int32(max) {
    29  			t.Errorf("%d open connections, want <= %d", n, max)
    30  		}
    31  		defer atomic.AddInt32(&open, -1)
    32  		time.Sleep(10 * time.Millisecond)
    33  		fmt.Fprint(w, "some body")
    34  	})
    35  	config := DefaultConfig()
    36  	config.MaxOpenConnections = max
    37  	l, err := Listen("tcp://127.0.0.1:0", config)
    38  	require.NoError(t, err)
    39  	defer l.Close()
    40  	go Serve(l, mux, log.TestingLogger(), config)
    41  
    42  	// Make N GET calls to the server.
    43  	attempts := max * 2
    44  	var wg sync.WaitGroup
    45  	var failed int32
    46  	for i := 0; i < attempts; i++ {
    47  		wg.Add(1)
    48  		go func() {
    49  			defer wg.Done()
    50  			c := http.Client{Timeout: 3 * time.Second}
    51  			r, err := c.Get("http://" + l.Addr().String())
    52  			if err != nil {
    53  				t.Log(err)
    54  				atomic.AddInt32(&failed, 1)
    55  				return
    56  			}
    57  			defer r.Body.Close()
    58  			io.Copy(ioutil.Discard, r.Body)
    59  		}()
    60  	}
    61  	wg.Wait()
    62  
    63  	// We expect some Gets to fail as the server's accept queue is filled,
    64  	// but most should succeed.
    65  	if int(failed) >= attempts/2 {
    66  		t.Errorf("%d requests failed within %d attempts", failed, attempts)
    67  	}
    68  }
    69  
    70  func TestServeTLS(t *testing.T) {
    71  	ln, err := net.Listen("tcp", "localhost:0")
    72  	require.NoError(t, err)
    73  	defer ln.Close()
    74  
    75  	mux := http.NewServeMux()
    76  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    77  		fmt.Fprint(w, "some body")
    78  	})
    79  
    80  	go ServeTLS(ln, mux, "test.crt", "test.key", log.TestingLogger(), DefaultConfig())
    81  
    82  	tr := &http.Transport{
    83  		TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // nolint: gosec
    84  	}
    85  	c := &http.Client{Transport: tr}
    86  	res, err := c.Get("https://" + ln.Addr().String())
    87  	require.NoError(t, err)
    88  	defer res.Body.Close()
    89  	assert.Equal(t, http.StatusOK, res.StatusCode)
    90  
    91  	body, err := ioutil.ReadAll(res.Body)
    92  	require.NoError(t, err)
    93  	assert.Equal(t, []byte("some body"), body)
    94  }