github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/ctl/cmd_ping_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 ctl
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/fagongzi/util/protoc"
    22  	"github.com/matrixorigin/matrixone/pkg/clusterservice"
    23  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    24  	"github.com/matrixorigin/matrixone/pkg/pb/api"
    25  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    26  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestCmdPingTNWithEmptyTN(t *testing.T) {
    33  	ctx := context.Background()
    34  	initTestRuntime()
    35  	proc := process.New(ctx, nil, nil, nil, nil, nil, nil, nil, nil, nil)
    36  	result, err := handlePing()(proc,
    37  		tn,
    38  		"",
    39  		func(ctx context.Context, proc *process.Process, cr []txn.CNOpRequest) ([]txn.CNOpResponse, error) {
    40  			return nil, nil
    41  		})
    42  	require.NoError(t, err)
    43  	assert.Equal(t,
    44  		Result{
    45  			Method: api.OpMethodName[api.OpCode_OpPing],
    46  			Data:   make([]any, 0),
    47  		},
    48  		result)
    49  }
    50  
    51  func TestCmdPingTNWithSingleTN(t *testing.T) {
    52  	initTestRuntime(1)
    53  
    54  	shardID := uint64(1)
    55  	ctx := context.Background()
    56  	proc := process.New(ctx, nil, nil, nil, nil, nil, nil, nil, nil, nil)
    57  	result, err := handlePing()(proc,
    58  		tn,
    59  		"",
    60  		func(ctx context.Context, proc *process.Process, cr []txn.CNOpRequest) ([]txn.CNOpResponse, error) {
    61  			return []txn.CNOpResponse{
    62  				{Payload: protoc.MustMarshal(&api.TNPingResponse{ShardID: shardID})},
    63  			}, nil
    64  		})
    65  	require.NoError(t, err)
    66  	assert.Equal(t, Result{
    67  		Method: api.OpMethodName[api.OpCode_OpPing],
    68  		Data:   []any{api.TNPingResponse{ShardID: shardID}},
    69  	}, result)
    70  }
    71  
    72  func TestCmdPingTNWithMultiTN(t *testing.T) {
    73  	ctx := context.Background()
    74  	initTestRuntime(1, 2)
    75  	proc := process.New(ctx, nil, nil, nil, nil, nil, nil, nil, nil, nil)
    76  	result, err := handlePing()(proc,
    77  		tn,
    78  		"",
    79  		func(ctx context.Context, proc *process.Process, cr []txn.CNOpRequest) ([]txn.CNOpResponse, error) {
    80  			return []txn.CNOpResponse{
    81  				{Payload: protoc.MustMarshal(&api.TNPingResponse{ShardID: 1})},
    82  				{Payload: protoc.MustMarshal(&api.TNPingResponse{ShardID: 2})},
    83  			}, nil
    84  		})
    85  	require.NoError(t, err)
    86  	assert.Equal(t, Result{
    87  		Method: api.OpMethodName[api.OpCode_OpPing],
    88  		Data:   []any{api.TNPingResponse{ShardID: 1}, api.TNPingResponse{ShardID: 2}},
    89  	}, result)
    90  }
    91  
    92  func TestCmdPingTNWithParameter(t *testing.T) {
    93  	ctx := context.Background()
    94  	initTestRuntime(1, 2)
    95  	proc := process.New(ctx, nil, nil, nil, nil, nil, nil, nil, nil, nil)
    96  	result, err := handlePing()(proc,
    97  		tn,
    98  		"1",
    99  		func(ctx context.Context, proc *process.Process, cr []txn.CNOpRequest) ([]txn.CNOpResponse, error) {
   100  			return []txn.CNOpResponse{
   101  				{Payload: protoc.MustMarshal(&api.TNPingResponse{ShardID: 1})},
   102  			}, nil
   103  		})
   104  	require.NoError(t, err)
   105  	assert.Equal(t, Result{
   106  		Method: api.OpMethodName[api.OpCode_OpPing],
   107  		Data:   []any{api.TNPingResponse{ShardID: 1}},
   108  	}, result)
   109  }
   110  
   111  func initTestRuntime(shardIDs ...uint64) {
   112  	runtime.SetupProcessLevelRuntime(runtime.DefaultRuntime())
   113  	var shards = make([]metadata.TNShard, 0, len(shardIDs))
   114  	for _, id := range shardIDs {
   115  		shards = append(shards, metadata.TNShard{
   116  			TNShardRecord: metadata.TNShardRecord{ShardID: id},
   117  		})
   118  	}
   119  
   120  	cluster := clusterservice.NewMOCluster(
   121  		nil,
   122  		0,
   123  		clusterservice.WithDisableRefresh(),
   124  		clusterservice.WithServices(nil, []metadata.TNService{
   125  			{
   126  				Shards: shards,
   127  			},
   128  		}))
   129  	runtime.ProcessLevelRuntime().SetGlobalVariables(runtime.ClusterService, cluster)
   130  }