github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/utils/appwsid_test.go (about)

     1  /*
     2   * Copyright (c) 2020-present unTill Pro, Ltd.
     3   * @author Denis Gribanov
     4   */
     5  
     6  package coreutils
     7  
     8  import (
     9  	"testing"
    10  
    11  	fuzz "github.com/google/gofuzz"
    12  	"github.com/stretchr/testify/require"
    13  	"github.com/voedger/voedger/pkg/istructs"
    14  )
    15  
    16  func TestBasicUsage_GetAppWSID(t *testing.T) {
    17  	cases := []struct {
    18  		wsid             istructs.WSID
    19  		numAppWorkspaces istructs.NumAppWorkspaces
    20  		expectedAppWSID  istructs.WSID
    21  	}{
    22  		{1, 1, istructs.NewWSID(istructs.MainClusterID, istructs.MaxPseudoBaseWSID+1)},
    23  		{2, 1, istructs.NewWSID(istructs.MainClusterID, istructs.MaxPseudoBaseWSID+1)},
    24  		{3, 1, istructs.NewWSID(istructs.MainClusterID, istructs.MaxPseudoBaseWSID+1)},
    25  		{1, 10, istructs.NewWSID(istructs.MainClusterID, istructs.MaxPseudoBaseWSID+2)},
    26  		{8, 10, istructs.NewWSID(istructs.MainClusterID, istructs.MaxPseudoBaseWSID+9)},
    27  		{10, 10, istructs.NewWSID(istructs.MainClusterID, istructs.MaxPseudoBaseWSID+1)},
    28  		{11, 10, istructs.NewWSID(istructs.MainClusterID, istructs.MaxPseudoBaseWSID+2)},
    29  	}
    30  
    31  	for _, c := range cases {
    32  		require.Equal(t, c.expectedAppWSID, GetAppWSID(c.wsid, c.numAppWorkspaces), c)
    33  	}
    34  }
    35  
    36  func TestGetPseudoWSID(t *testing.T) {
    37  	fuzz := fuzz.New()
    38  	type src struct {
    39  		entity    string
    40  		clusterID istructs.ClusterID
    41  	}
    42  	const mask = uint64(0xFFFFFFFFFFFC0000)
    43  	var srcInstance src
    44  	for i := 0; i < 10000; i++ {
    45  		fuzz.Fuzz(&srcInstance)
    46  		require.Zero(t, uint64(GetPseudoWSID(istructs.NullWSID, srcInstance.entity, srcInstance.clusterID))&mask)
    47  		require.Zero(t, uint64(GetPseudoWSID(istructs.NullWSID+1, srcInstance.entity, srcInstance.clusterID))&mask)
    48  	}
    49  }
    50  
    51  func TestAppWSIDToPseudoWSID(t *testing.T) {
    52  	numAppWorkspaces := istructs.NumAppWorkspaces(10)
    53  	pseudoWSIDInitial := GetPseudoWSID(istructs.NullWSID, "test", istructs.MainClusterID)
    54  	appWSIDExpected := GetAppWSID(pseudoWSIDInitial, numAppWorkspaces)
    55  
    56  	// could be any but must lead to the initial appWSIDExpected
    57  	psuedoWSID_someNew := AppWSIDToPseudoWSID(appWSIDExpected)
    58  
    59  	appWSIDActual := GetAppWSID(psuedoWSID_someNew, numAppWorkspaces)
    60  	require.Equal(t, appWSIDExpected, appWSIDActual)
    61  }