github.com/matrixorigin/matrixone@v1.2.0/pkg/logservice/option_test.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     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 logservice
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/common/morpc"
    25  )
    26  
    27  func TestWithOption(t *testing.T) {
    28  	ctx := context.Background()
    29  	require.Nil(t, GetBackendOptions(ctx))
    30  	require.Nil(t, GetClientOptions(ctx))
    31  
    32  	// set morpc.BackendOption
    33  	backendOpts := []morpc.BackendOption{
    34  		morpc.WithBackendConnectTimeout(time.Second),
    35  	}
    36  	ctx1 := SetBackendOptions(ctx, backendOpts...)
    37  	backendOptsRet := GetBackendOptions(ctx1)
    38  	require.NotNil(t, backendOptsRet)
    39  	require.Equal(t, len(backendOpts), len(backendOptsRet))
    40  	require.Equal(t, backendOpts, backendOptsRet)
    41  
    42  	// set morpc.BackendOption again
    43  	newBackendOpts := []morpc.BackendOption{}
    44  	ctx2 := SetBackendOptions(ctx1, newBackendOpts...)
    45  	newBackendOptsRet := GetBackendOptions(ctx2)
    46  	require.NotNil(t, backendOptsRet)
    47  	require.Equal(t, len(newBackendOpts), len(newBackendOptsRet))
    48  	require.Equal(t, newBackendOpts, newBackendOptsRet)
    49  
    50  	clientOpts := []morpc.ClientOption{
    51  		morpc.WithClientMaxBackendPerHost(1),
    52  	}
    53  	ctx3 := SetClientOptions(ctx, clientOpts...)
    54  	clientOptsRet := GetClientOptions(ctx3)
    55  	require.NotNil(t, clientOptsRet)
    56  	require.Equal(t, len(clientOpts), len(clientOptsRet))
    57  	require.Equal(t, clientOpts, clientOptsRet)
    58  }