github.com/pawelgaczynski/gain@v0.4.0-alpha.0.20230821120126-41f1e60a18da/reactor_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  	"testing"
    19  	"time"
    20  
    21  	"github.com/pawelgaczynski/gain"
    22  	gainNet "github.com/pawelgaczynski/gain/pkg/net"
    23  )
    24  
    25  func TestReactorTCPSingleWorkerSingleClient(t *testing.T) {
    26  	testServer(t, testServerConfig{
    27  		protocol:        gainNet.TCP,
    28  		numberOfClients: 1,
    29  		numberOfWorkers: 1,
    30  	}, gain.Reactor)
    31  }
    32  
    33  func TestReactorTCPSingleWorkerManyClients(t *testing.T) {
    34  	testServer(t, testServerConfig{
    35  		protocol:        gainNet.TCP,
    36  		numberOfClients: 8,
    37  		numberOfWorkers: 1,
    38  	}, gain.Reactor)
    39  }
    40  
    41  func TestReactorManyWorkersManyClients(t *testing.T) {
    42  	testServer(t, testServerConfig{
    43  		protocol:        gainNet.TCP,
    44  		numberOfClients: 16,
    45  		numberOfWorkers: 8,
    46  	}, gain.Reactor)
    47  }
    48  
    49  func TestReactorAsyncHandler(t *testing.T) {
    50  	testServer(t, testServerConfig{
    51  		protocol:        gainNet.TCP,
    52  		numberOfClients: 8,
    53  		numberOfWorkers: 2,
    54  		asyncHandler:    true,
    55  	}, gain.Reactor)
    56  }
    57  
    58  func TestReactorAsyncHandlerWithPool(t *testing.T) {
    59  	testServer(t, testServerConfig{
    60  		protocol:        gainNet.TCP,
    61  		numberOfClients: 8,
    62  		numberOfWorkers: 2,
    63  		asyncHandler:    true,
    64  		goroutinePool:   true,
    65  	}, gain.Reactor)
    66  }
    67  
    68  func TestReactorWaitForDialAllClients(t *testing.T) {
    69  	testServer(t, testServerConfig{
    70  		protocol:              gainNet.TCP,
    71  		numberOfClients:       8,
    72  		numberOfWorkers:       8,
    73  		waitForDialAllClients: true,
    74  	}, gain.Reactor)
    75  }
    76  
    77  func TestReactorTCPRingBuffer(t *testing.T) {
    78  	testRingBuffer(t, gainNet.TCP, gain.Reactor)
    79  }
    80  
    81  func TestReactorConnectionHandling(t *testing.T) {
    82  	testConnectionHandling(t, gain.Reactor)
    83  }
    84  
    85  func TestReactorTCPCloseSever(t *testing.T) {
    86  	testCloseServer(t, gainNet.TCP, gain.Reactor, false)
    87  }
    88  
    89  func TestReactorUDPCloseSever(t *testing.T) {
    90  	testCloseServer(t, gainNet.UDP, gain.Reactor, false)
    91  }
    92  
    93  func TestReactorTCPDoubleCloseSever(t *testing.T) {
    94  	testCloseServer(t, gainNet.UDP, gain.Reactor, true)
    95  }
    96  
    97  func TestReactorUDPDoubleCloseSever(t *testing.T) {
    98  	testCloseServer(t, gainNet.UDP, gain.Reactor, true)
    99  }
   100  
   101  func TestReactorCloseSeverWithConnectedClients(t *testing.T) {
   102  	testCloseServerWithConnectedClients(t, gain.Reactor)
   103  }
   104  
   105  func TestReactorTCPCloseConnSync(t *testing.T) {
   106  	testCloseConn(t, false, gain.Reactor, false)
   107  }
   108  
   109  func TestReactorTCPCloseConnAsync(t *testing.T) {
   110  	testCloseConn(t, true, gain.Reactor, false)
   111  }
   112  
   113  func TestReactorTCPOnlyCloseConnSync(t *testing.T) {
   114  	testCloseConn(t, false, gain.Reactor, true)
   115  }
   116  
   117  func TestReactorTCPOnlyCloseConnAsync(t *testing.T) {
   118  	testCloseConn(t, true, gain.Reactor, true)
   119  }
   120  
   121  func TestReactorTCPConnAddress(t *testing.T) {
   122  	testConnAddress(t, gainNet.TCP, gain.Reactor)
   123  }
   124  
   125  func TestReactorTCPLargeRead(t *testing.T) {
   126  	testLargeRead(t, gainNet.TCP, gain.Reactor)
   127  }
   128  
   129  func TestReactorTCPMultipleReads(t *testing.T) {
   130  	testMultipleReads(t, gainNet.TCP, false, gain.Reactor)
   131  }
   132  
   133  func TestReactorTCPAsyncHandlerMultipleReads(t *testing.T) {
   134  	testMultipleReads(t, gainNet.TCP, true, gain.Reactor)
   135  }
   136  
   137  func TestReactorRoundRobinLoadBalancer(t *testing.T) {
   138  	testServer(t, testServerConfig{
   139  		protocol:        gainNet.TCP,
   140  		numberOfClients: 16,
   141  		numberOfWorkers: 8,
   142  		configOptions: []gain.ConfigOption{
   143  			gain.WithLoadBalancing(gain.RoundRobin),
   144  		},
   145  	}, gain.Reactor)
   146  }
   147  
   148  func TestReactorLeastConnectionsLoadBalancer(t *testing.T) {
   149  	testServer(t, testServerConfig{
   150  		protocol:        gainNet.TCP,
   151  		numberOfClients: 16,
   152  		numberOfWorkers: 8,
   153  		configOptions: []gain.ConfigOption{
   154  			gain.WithLoadBalancing(gain.LeastConnections),
   155  		},
   156  	}, gain.Reactor)
   157  }
   158  
   159  func TestReactorSourceIPHashLoadBalancer(t *testing.T) {
   160  	testServer(t, testServerConfig{
   161  		protocol:        gainNet.TCP,
   162  		numberOfClients: 16,
   163  		numberOfWorkers: 8,
   164  		configOptions: []gain.ConfigOption{
   165  			gain.WithLoadBalancing(gain.SourceIPHash),
   166  		},
   167  	}, gain.Reactor)
   168  }
   169  
   170  func TestReactorManyWorkersManyClientsTCPKeepAlive(t *testing.T) {
   171  	testServer(t, testServerConfig{
   172  		protocol:        gainNet.TCP,
   173  		numberOfClients: 16,
   174  		numberOfWorkers: 8,
   175  		configOptions: []gain.ConfigOption{
   176  			gain.WithTCPKeepAlive(time.Minute),
   177  		},
   178  	}, gain.Reactor)
   179  }