github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/client/server_test.go (about)

     1  // Copyright 2020 DataStax
     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 client_test
    16  
    17  import (
    18  	"context"
    19  	"github.com/datastax/go-cassandra-native-protocol/client"
    20  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  	"testing"
    24  	"time"
    25  )
    26  
    27  func TestCqlServer_Accept(t *testing.T) {
    28  
    29  	server := client.NewCqlServer("127.0.0.1:9043", nil)
    30  
    31  	clt1 := client.NewCqlClient("127.0.0.1:9043", nil)
    32  	clt2 := client.NewCqlClient("127.0.0.1:9043", nil)
    33  
    34  	ctx, cancelFn := context.WithCancel(context.Background())
    35  
    36  	err := server.Start(ctx)
    37  	require.NoError(t, err)
    38  
    39  	clientConn1, err := clt1.Connect(ctx)
    40  	require.NoError(t, err)
    41  	require.NotNil(t, clientConn1)
    42  
    43  	clientConn2, err := clt2.Connect(ctx)
    44  	require.NoError(t, err)
    45  	require.NotNil(t, clientConn2)
    46  
    47  	serverConn1, err := server.Accept(clientConn1)
    48  	require.NoError(t, err)
    49  	require.NotNil(t, serverConn1)
    50  
    51  	serverConn2, err := server.Accept(clientConn2)
    52  	require.NoError(t, err)
    53  	require.NotNil(t, serverConn2)
    54  
    55  	require.Equal(t, serverConn1.RemoteAddr(), clientConn1.LocalAddr())
    56  	require.Equal(t, serverConn2.RemoteAddr(), clientConn2.LocalAddr())
    57  	require.Equal(t, serverConn1.LocalAddr(), clientConn1.RemoteAddr())
    58  	require.Equal(t, serverConn2.LocalAddr(), clientConn2.RemoteAddr())
    59  
    60  	cancelFn()
    61  
    62  	assert.Eventually(t, clientConn1.IsClosed, time.Second*10, time.Millisecond*10)
    63  	assert.Eventually(t, clientConn2.IsClosed, time.Second*10, time.Millisecond*10)
    64  	assert.Eventually(t, serverConn1.IsClosed, time.Second*10, time.Millisecond*10)
    65  	assert.Eventually(t, serverConn2.IsClosed, time.Second*10, time.Millisecond*10)
    66  	assert.Eventually(t, server.IsClosed, time.Second*10, time.Millisecond*10)
    67  }
    68  
    69  func TestCqlServer_AcceptAny(t *testing.T) {
    70  
    71  	server := client.NewCqlServer("127.0.0.1:9043", nil)
    72  
    73  	clt1 := client.NewCqlClient("127.0.0.1:9043", nil)
    74  	clt2 := client.NewCqlClient("127.0.0.1:9043", nil)
    75  
    76  	ctx, cancelFn := context.WithCancel(context.Background())
    77  
    78  	err := server.Start(ctx)
    79  	require.NoError(t, err)
    80  
    81  	clientConn1, err := clt1.Connect(ctx)
    82  	require.NoError(t, err)
    83  	require.NotNil(t, clientConn1)
    84  
    85  	serverConn1, err := server.AcceptAny()
    86  	require.NoError(t, err)
    87  	require.NotNil(t, serverConn1)
    88  
    89  	clientConn2, err := clt2.Connect(ctx)
    90  	require.NoError(t, err)
    91  	require.NotNil(t, clientConn2)
    92  
    93  	serverConn2, err := server.AcceptAny()
    94  	require.NoError(t, err)
    95  	require.NotNil(t, serverConn2)
    96  
    97  	require.Equal(t, serverConn1.RemoteAddr(), clientConn1.LocalAddr())
    98  	require.Equal(t, serverConn2.RemoteAddr(), clientConn2.LocalAddr())
    99  	require.Equal(t, serverConn1.LocalAddr(), clientConn1.RemoteAddr())
   100  	require.Equal(t, serverConn2.LocalAddr(), clientConn2.RemoteAddr())
   101  
   102  	cancelFn()
   103  
   104  	assert.Eventually(t, clientConn1.IsClosed, time.Second*10, time.Millisecond*10)
   105  	assert.Eventually(t, clientConn2.IsClosed, time.Second*10, time.Millisecond*10)
   106  	assert.Eventually(t, serverConn1.IsClosed, time.Second*10, time.Millisecond*10)
   107  	assert.Eventually(t, serverConn2.IsClosed, time.Second*10, time.Millisecond*10)
   108  	assert.Eventually(t, server.IsClosed, time.Second*10, time.Millisecond*10)
   109  }
   110  
   111  func TestCqlServer_AllAcceptedClients(t *testing.T) {
   112  
   113  	server := client.NewCqlServer("127.0.0.1:9043", nil)
   114  
   115  	clt1 := client.NewCqlClient("127.0.0.1:9043", nil)
   116  	clt2 := client.NewCqlClient("127.0.0.1:9043", nil)
   117  
   118  	ctx, cancelFn := context.WithCancel(context.Background())
   119  
   120  	err := server.Start(ctx)
   121  	require.NoError(t, err)
   122  
   123  	clientConn1, err := clt1.Connect(ctx)
   124  	require.NoError(t, err)
   125  	require.NotNil(t, clientConn1)
   126  
   127  	clientConn2, err := clt2.Connect(ctx)
   128  	require.NoError(t, err)
   129  	require.NotNil(t, clientConn2)
   130  
   131  	serverConn1, err := server.Accept(clientConn1)
   132  	require.NoError(t, err)
   133  	require.NotNil(t, serverConn1)
   134  
   135  	serverConn2, err := server.Accept(clientConn2)
   136  	require.NoError(t, err)
   137  	require.NotNil(t, serverConn2)
   138  
   139  	clients, err := server.AllAcceptedClients()
   140  	require.NoError(t, err)
   141  	require.NotNil(t, clients)
   142  	require.Len(t, clients, 2)
   143  
   144  	cancelFn()
   145  
   146  	assert.Eventually(t, clientConn1.IsClosed, time.Second*10, time.Millisecond*10)
   147  	assert.Eventually(t, clientConn2.IsClosed, time.Second*10, time.Millisecond*10)
   148  	assert.Eventually(t, clients[0].IsClosed, time.Second*10, time.Millisecond*10)
   149  	assert.Eventually(t, clients[1].IsClosed, time.Second*10, time.Millisecond*10)
   150  	assert.Eventually(t, server.IsClosed, time.Second*10, time.Millisecond*10)
   151  }
   152  
   153  func TestCqlServer_Bind(t *testing.T) {
   154  
   155  	server := client.NewCqlServer("127.0.0.1:9043", nil)
   156  
   157  	clt1 := client.NewCqlClient("127.0.0.1:9043", nil)
   158  	clt2 := client.NewCqlClient("127.0.0.1:9043", nil)
   159  
   160  	ctx, cancelFn := context.WithCancel(context.Background())
   161  
   162  	err := server.Start(ctx)
   163  	require.NoError(t, err)
   164  
   165  	clientConn1, serverConn1, err := server.Bind(clt1, ctx)
   166  	require.NoError(t, err)
   167  	require.NotNil(t, clientConn1)
   168  	require.NotNil(t, serverConn1)
   169  
   170  	clientConn2, serverConn2, err := server.Bind(clt2, ctx)
   171  	require.NoError(t, err)
   172  	require.NotNil(t, clientConn2)
   173  	require.NotNil(t, serverConn2)
   174  
   175  	require.Equal(t, serverConn1.RemoteAddr(), clientConn1.LocalAddr())
   176  	require.Equal(t, serverConn2.RemoteAddr(), clientConn2.LocalAddr())
   177  	require.Equal(t, serverConn1.LocalAddr(), clientConn1.RemoteAddr())
   178  	require.Equal(t, serverConn2.LocalAddr(), clientConn2.RemoteAddr())
   179  
   180  	cancelFn()
   181  
   182  	assert.Eventually(t, clientConn1.IsClosed, time.Second*10, time.Millisecond*10)
   183  	assert.Eventually(t, clientConn2.IsClosed, time.Second*10, time.Millisecond*10)
   184  	assert.Eventually(t, serverConn1.IsClosed, time.Second*10, time.Millisecond*10)
   185  	assert.Eventually(t, serverConn2.IsClosed, time.Second*10, time.Millisecond*10)
   186  	assert.Eventually(t, server.IsClosed, time.Second*10, time.Millisecond*10)
   187  }
   188  
   189  func TestCqlServer_BindAndInit(t *testing.T) {
   190  
   191  	server := client.NewCqlServer("127.0.0.1:9043", nil)
   192  
   193  	clt1 := client.NewCqlClient("127.0.0.1:9043", nil)
   194  	clt2 := client.NewCqlClient("127.0.0.1:9043", nil)
   195  
   196  	ctx, cancelFn := context.WithCancel(context.Background())
   197  
   198  	err := server.Start(ctx)
   199  	require.NoError(t, err)
   200  
   201  	clientConn1, serverConn1, err := server.BindAndInit(clt1, ctx, primitive.ProtocolVersion4, client.ManagedStreamId)
   202  	require.NoError(t, err)
   203  	require.NotNil(t, clientConn1)
   204  	require.NotNil(t, serverConn1)
   205  
   206  	clientConn2, serverConn2, err := server.Bind(clt2, ctx)
   207  	require.NoError(t, err)
   208  	require.NotNil(t, clientConn2)
   209  	require.NotNil(t, serverConn2)
   210  
   211  	require.Equal(t, serverConn1.RemoteAddr(), clientConn1.LocalAddr())
   212  	require.Equal(t, serverConn2.RemoteAddr(), clientConn2.LocalAddr())
   213  	require.Equal(t, serverConn1.LocalAddr(), clientConn1.RemoteAddr())
   214  	require.Equal(t, serverConn2.LocalAddr(), clientConn2.RemoteAddr())
   215  
   216  	cancelFn()
   217  
   218  	assert.Eventually(t, clientConn1.IsClosed, time.Second*10, time.Millisecond*10)
   219  	assert.Eventually(t, clientConn2.IsClosed, time.Second*10, time.Millisecond*10)
   220  	assert.Eventually(t, serverConn1.IsClosed, time.Second*10, time.Millisecond*10)
   221  	assert.Eventually(t, serverConn2.IsClosed, time.Second*10, time.Millisecond*10)
   222  	assert.Eventually(t, server.IsClosed, time.Second*10, time.Millisecond*10)
   223  }