github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/istorage/cas/impl_test.go (about)

     1  /*
     2   * Copyright (c) 2021-present unTill Pro, Ltd.
     3   */
     4  
     5  package cas
     6  
     7  import (
     8  	"os"
     9  	"strconv"
    10  	"sync"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/voedger/voedger/pkg/istorage"
    16  	coreutils "github.com/voedger/voedger/pkg/utils"
    17  )
    18  
    19  const casDefaultPort = 9042
    20  const casDefaultHost = "127.0.0.1"
    21  
    22  func TestBasicUsage(t *testing.T) {
    23  	if !coreutils.IsCassandraStorage() {
    24  		t.Skip()
    25  	}
    26  	casPar := CassandraParamsType{
    27  		Hosts:                   hosts(),
    28  		Port:                    port(),
    29  		NumRetries:              retryAttempt,
    30  		KeyspaceWithReplication: SimpleWithReplication,
    31  	}
    32  	asf, err := Provide(casPar)
    33  	require.NoError(t, err)
    34  	istorage.TechnologyCompatibilityKit(t, asf)
    35  
    36  }
    37  
    38  func TestMultipleApps(t *testing.T) {
    39  	if !coreutils.IsCassandraStorage() {
    40  		t.Skip()
    41  	}
    42  	const appCount = 3
    43  
    44  	casPar := CassandraParamsType{
    45  		Hosts:                   hosts(),
    46  		Port:                    port(),
    47  		KeyspaceWithReplication: SimpleWithReplication,
    48  	}
    49  
    50  	wg := sync.WaitGroup{}
    51  
    52  	asf, err := Provide(casPar)
    53  	require.NoError(t, err)
    54  
    55  	testApp := func() {
    56  		defer wg.Done()
    57  		istorage.TechnologyCompatibilityKit(t, asf)
    58  	}
    59  
    60  	for appNo := 0; appNo < appCount; appNo++ {
    61  		wg.Add(1)
    62  		go testApp()
    63  	}
    64  
    65  	wg.Wait()
    66  }
    67  
    68  func hosts() string {
    69  	value, ok := os.LookupEnv("ISTORAGECAS_HOSTS")
    70  	if !ok {
    71  		return casDefaultHost
    72  	}
    73  	return value
    74  }
    75  
    76  func port() int {
    77  	value, ok := os.LookupEnv("ISTORAGECAS_PORT")
    78  	if !ok {
    79  		return casDefaultPort
    80  	}
    81  	result, err := strconv.Atoi(value)
    82  	if err != nil {
    83  		panic(err)
    84  	}
    85  	return result
    86  }
    87  
    88  func TestCassandraParamsType_cqlVersion(t *testing.T) {
    89  	tests := []struct {
    90  		name           string
    91  		cqlVersion     string
    92  		wantCqlVersion string
    93  	}{
    94  		{
    95  			name:           "Should get default",
    96  			cqlVersion:     "",
    97  			wantCqlVersion: "3.0.0",
    98  		},
    99  		{
   100  			name:           "Should get custom",
   101  			cqlVersion:     "1.2.3",
   102  			wantCqlVersion: "1.2.3",
   103  		},
   104  	}
   105  	for _, test := range tests {
   106  		t.Run(test.name, func(t *testing.T) {
   107  			require.Equal(t, test.wantCqlVersion, CassandraParamsType{CQLVersion: test.cqlVersion}.cqlVersion())
   108  		})
   109  	}
   110  }
   111  
   112  func TestDCAwareRoundRobinPolicy(t *testing.T) {
   113  	if !coreutils.IsCassandraStorage() {
   114  		t.Skip()
   115  	}
   116  	casPar := CassandraParamsType{
   117  		Hosts:                   hosts(),
   118  		Port:                    port(),
   119  		NumRetries:              retryAttempt,
   120  		KeyspaceWithReplication: SimpleWithReplication,
   121  		DC:                      "dc1",
   122  	}
   123  	provider := newStorageProvider(casPar)
   124  	require.NotNil(t, provider.cluster.PoolConfig.HostSelectionPolicy)
   125  }