github.com/m3db/m3@v1.5.0/src/x/net/server_test.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package net
    22  
    23  import (
    24  	"fmt"
    25  	"io/ioutil"
    26  	"net"
    27  	"sort"
    28  	"sync"
    29  	"testing"
    30  
    31  	"github.com/m3db/m3/src/x/retry"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  )
    35  
    36  func TestStartAcceptLoop(t *testing.T) {
    37  	var (
    38  		results        []string
    39  		resultLock     sync.Mutex
    40  		wgClient       sync.WaitGroup
    41  		wgServer       sync.WaitGroup
    42  		numConnections = 10
    43  	)
    44  
    45  	l, err := net.Listen("tcp", "127.0.0.1:0")
    46  	assert.Nil(t, err)
    47  	connCh, errCh := StartForeverAcceptLoop(l, retry.NewOptions())
    48  
    49  	wgServer.Add(1)
    50  	go func() {
    51  		defer wgServer.Done()
    52  
    53  		curr := 0
    54  		for conn := range connCh {
    55  			fmt.Fprintf(conn, "%d", curr)
    56  			conn.Close()
    57  			curr++
    58  		}
    59  	}()
    60  
    61  	for i := 0; i < numConnections; i++ {
    62  		wgClient.Add(1)
    63  		go func() {
    64  			defer wgClient.Done()
    65  
    66  			conn, err := net.Dial("tcp", l.Addr().String())
    67  			assert.Nil(t, err)
    68  
    69  			data, err := ioutil.ReadAll(conn)
    70  			assert.Nil(t, err)
    71  
    72  			resultLock.Lock()
    73  			results = append(results, string(data))
    74  			resultLock.Unlock()
    75  		}()
    76  	}
    77  
    78  	wgClient.Wait()
    79  
    80  	// Close the listener to intentionally cause a server-side connection error
    81  	l.Close()
    82  	wgServer.Wait()
    83  
    84  	sort.Strings(results)
    85  
    86  	var expected []string
    87  	for i := 0; i < numConnections; i++ {
    88  		expected = append(expected, fmt.Sprintf("%d", i))
    89  	}
    90  	assert.Equal(t, expected, results)
    91  
    92  	// Expect a server-side connection error
    93  	err = <-errCh
    94  	assert.NotNil(t, err)
    95  }