github.com/pawelgaczynski/gain@v0.4.0-alpha.0.20230821120126-41f1e60a18da/tcp_conn_test.go (about)

     1  // Copyright (c) 2023 Paweł Gaczyński
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gain_test
    16  
    17  import (
    18  	"sync"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/pawelgaczynski/gain"
    23  	gainNet "github.com/pawelgaczynski/gain/pkg/net"
    24  	. "github.com/stretchr/testify/require"
    25  )
    26  
    27  func testConnectionHandling(t *testing.T, architecture gain.ServerArchitecture) {
    28  	t.Helper()
    29  	testHandler := newConnServerTester(gainNet.TCP, 0, false)
    30  	server, port := newTestConnServer(t, gainNet.TCP, false, architecture, testHandler.testServerHandler)
    31  
    32  	defer func() {
    33  		server.Shutdown()
    34  	}()
    35  
    36  	var connWaitGroup sync.WaitGroup
    37  
    38  	connWaitGroup.Add(10)
    39  	testHandler.onAcceptCallback = func(_ gain.Conn, _ string) {
    40  		connWaitGroup.Done()
    41  	}
    42  
    43  	clientsGroup := newTestConnClientGroup(t, gainNet.TCP, port, 10)
    44  	clientsGroup.Dial()
    45  
    46  	connWaitGroup.Wait()
    47  
    48  	Equal(t, 10, server.ActiveConnections())
    49  
    50  	clientsGroup.Close()
    51  
    52  	time.Sleep(1 * time.Second)
    53  
    54  	Equal(t, 0, server.ActiveConnections())
    55  }