github.com/pawelgaczynski/gain@v0.4.0-alpha.0.20230821120126-41f1e60a18da/sharding_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  	"crypto/rand"
    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 TestShardingSingleWorkerSingleClient(t *testing.T) {
    28  	testServer(t, testServerConfig{
    29  		protocol:        gainNet.TCP,
    30  		numberOfClients: 1,
    31  		numberOfWorkers: 1,
    32  	}, gain.SocketSharding)
    33  }
    34  
    35  func TestShardingSingleWorkerManyClients(t *testing.T) {
    36  	testServer(t, testServerConfig{
    37  		protocol:        gainNet.TCP,
    38  		numberOfClients: 8,
    39  		numberOfWorkers: 1,
    40  	}, gain.SocketSharding)
    41  }
    42  
    43  func TestShardingManyWorkersManyClients(t *testing.T) {
    44  	testServer(t, testServerConfig{
    45  		protocol:        gainNet.TCP,
    46  		numberOfClients: 16,
    47  		numberOfWorkers: 8,
    48  	}, gain.SocketSharding)
    49  }
    50  
    51  func TestShardingAsyncHandler(t *testing.T) {
    52  	testServer(t, testServerConfig{
    53  		protocol:        gainNet.TCP,
    54  		numberOfClients: 8,
    55  		numberOfWorkers: 8,
    56  		asyncHandler:    true,
    57  	}, gain.SocketSharding)
    58  }
    59  
    60  func TestShardingAsyncHandlerWithPool(t *testing.T) {
    61  	testServer(t, testServerConfig{
    62  		protocol:        gainNet.TCP,
    63  		numberOfClients: 8,
    64  		numberOfWorkers: 8,
    65  		asyncHandler:    true,
    66  		goroutinePool:   true,
    67  	}, gain.SocketSharding)
    68  }
    69  
    70  func TestShardingWaitForDialAllClients(t *testing.T) {
    71  	testServer(t, testServerConfig{
    72  		protocol:              gainNet.TCP,
    73  		numberOfClients:       8,
    74  		numberOfWorkers:       8,
    75  		waitForDialAllClients: true,
    76  	}, gain.SocketSharding)
    77  }
    78  
    79  func TestShardingUDPSingleWorkerSingleClient(t *testing.T) {
    80  	testServer(t, testServerConfig{
    81  		protocol:        gainNet.UDP,
    82  		numberOfClients: 1,
    83  		numberOfWorkers: 1,
    84  		writesCount:     2,
    85  	}, gain.SocketSharding)
    86  }
    87  
    88  func TestShardingUDPSingleWorkerManyClients(t *testing.T) {
    89  	testServer(t, testServerConfig{
    90  		protocol:        gainNet.UDP,
    91  		numberOfClients: 8,
    92  		numberOfWorkers: 1,
    93  	}, gain.SocketSharding)
    94  }
    95  
    96  func TestShardingUDPManyWorkersManyClients(t *testing.T) {
    97  	testServer(t, testServerConfig{
    98  		protocol:        gainNet.UDP,
    99  		numberOfClients: 16,
   100  		numberOfWorkers: 8,
   101  	}, gain.SocketSharding)
   102  }
   103  
   104  func TestShardingUDPAsyncHandler(t *testing.T) {
   105  	testServer(t, testServerConfig{
   106  		protocol:        gainNet.UDP,
   107  		numberOfClients: 8,
   108  		numberOfWorkers: 8,
   109  		asyncHandler:    true,
   110  	}, gain.SocketSharding)
   111  }
   112  
   113  func TestShardingUDPAsyncHandlerWithPool(t *testing.T) {
   114  	testServer(t, testServerConfig{
   115  		protocol:        gainNet.UDP,
   116  		numberOfClients: 8,
   117  		numberOfWorkers: 8,
   118  		asyncHandler:    true,
   119  		goroutinePool:   true,
   120  	}, gain.SocketSharding)
   121  }
   122  
   123  func TestShardingTCPRingBuffer(t *testing.T) {
   124  	testRingBuffer(t, gainNet.TCP, gain.SocketSharding)
   125  }
   126  
   127  func TestShardingUDPRingBuffer(t *testing.T) {
   128  	testRingBuffer(t, gainNet.UDP, gain.SocketSharding)
   129  }
   130  
   131  func TestShardingConnectionHandling(t *testing.T) {
   132  	testConnectionHandling(t, gain.SocketSharding)
   133  }
   134  
   135  func TestShardingTCPCloseSever(t *testing.T) {
   136  	testCloseServer(t, gainNet.TCP, gain.SocketSharding, false)
   137  }
   138  
   139  func TestShardingUDPCloseSever(t *testing.T) {
   140  	testCloseServer(t, gainNet.UDP, gain.SocketSharding, false)
   141  }
   142  
   143  func TestShardingTCPDoubleCloseSever(t *testing.T) {
   144  	testCloseServer(t, gainNet.TCP, gain.SocketSharding, true)
   145  }
   146  
   147  func TestShardingUDPDoubleCloseSever(t *testing.T) {
   148  	testCloseServer(t, gainNet.UDP, gain.SocketSharding, true)
   149  }
   150  
   151  func TestShardingCloseSeverWithConnectedClients(t *testing.T) {
   152  	testCloseServerWithConnectedClients(t, gain.SocketSharding)
   153  }
   154  
   155  func TestShardingReleasingUDPConns(t *testing.T) {
   156  	testHandler := newConnServerTester(gainNet.UDP, 10, true)
   157  	server, port := newTestConnServer(t, gainNet.UDP, false, gain.SocketSharding, testHandler.testServerHandler)
   158  
   159  	clientsGroup := newTestConnClientGroup(t, gainNet.UDP, port, 10)
   160  	clientsGroup.Dial()
   161  
   162  	go func() {
   163  		for {
   164  			data := make([]byte, 1024)
   165  			_, err := rand.Read(data)
   166  			Nil(t, err)
   167  			clientsGroup.Write(data)
   168  			buffer := make([]byte, 1024)
   169  			clientsGroup.Read(buffer)
   170  		}
   171  	}()
   172  
   173  	Equal(t, 0, server.ActiveConnections())
   174  	wg := testHandler.writeWG
   175  	wg.Wait()
   176  	server.Shutdown()
   177  }
   178  
   179  func TestShardingTCPCloseConnSync(t *testing.T) {
   180  	testCloseConn(t, false, gain.SocketSharding, false)
   181  }
   182  
   183  func TestShardingTCPCloseConnAsync(t *testing.T) {
   184  	testCloseConn(t, true, gain.SocketSharding, false)
   185  }
   186  
   187  func TestShardingTCPOnlyCloseConnSync(t *testing.T) {
   188  	testCloseConn(t, false, gain.SocketSharding, true)
   189  }
   190  
   191  func TestShardingTCPOnlyCloseConnAsync(t *testing.T) {
   192  	testCloseConn(t, true, gain.SocketSharding, true)
   193  }
   194  
   195  func TestShardingTCPConnAddress(t *testing.T) {
   196  	testConnAddress(t, gainNet.TCP, gain.SocketSharding)
   197  }
   198  
   199  func TestShardingUDPConnAddress(t *testing.T) {
   200  	testConnAddress(t, gainNet.UDP, gain.SocketSharding)
   201  }
   202  
   203  func TestShardingTCPLargeRead(t *testing.T) {
   204  	testLargeRead(t, gainNet.TCP, gain.SocketSharding)
   205  }
   206  
   207  func TestShardingTCPMultipleReads(t *testing.T) {
   208  	testMultipleReads(t, gainNet.TCP, false, gain.SocketSharding)
   209  }
   210  
   211  func TestShardingTCPAsyncHandlerMultipleReads(t *testing.T) {
   212  	testMultipleReads(t, gainNet.TCP, true, gain.SocketSharding)
   213  }
   214  
   215  func TestShardingManyWorkersManyClientsWithCBPF(t *testing.T) {
   216  	testServer(t, testServerConfig{
   217  		protocol:        gainNet.TCP,
   218  		numberOfClients: 16,
   219  		numberOfWorkers: 8,
   220  		configOptions: []gain.ConfigOption{
   221  			gain.WithCBPF(true),
   222  		},
   223  	}, gain.SocketSharding)
   224  }
   225  
   226  func TestShardingManyWorkersManyClientsSocketBuffers(t *testing.T) {
   227  	testServer(t, testServerConfig{
   228  		protocol:        gainNet.TCP,
   229  		numberOfClients: 16,
   230  		numberOfWorkers: 8,
   231  		configOptions: []gain.ConfigOption{
   232  			gain.WithSocketRecvBufferSize(2048),
   233  			gain.WithSocketSendBufferSize(2048),
   234  		},
   235  	}, gain.SocketSharding)
   236  }
   237  
   238  func TestShardingManyWorkersManyClientsTCPKeepAlive(t *testing.T) {
   239  	testServer(t, testServerConfig{
   240  		protocol:        gainNet.TCP,
   241  		numberOfClients: 16,
   242  		numberOfWorkers: 8,
   243  		configOptions: []gain.ConfigOption{
   244  			gain.WithTCPKeepAlive(time.Minute),
   245  		},
   246  	}, gain.SocketSharding)
   247  }
   248  
   249  func TestShardingManyWorkersManyClientsCPUAffinity(t *testing.T) {
   250  	testServer(t, testServerConfig{
   251  		protocol:        gainNet.TCP,
   252  		numberOfClients: 16,
   253  		numberOfWorkers: 8,
   254  		configOptions: []gain.ConfigOption{
   255  			gain.WithCPUAffinity(true),
   256  		},
   257  	}, gain.SocketSharding)
   258  }