github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/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/matrixorigin/matrixone/pkg/vm/process"
    22  
    23  	"github.com/fagongzi/util/protoc"
    24  	pb "github.com/matrixorigin/matrixone/pkg/pb/ctl"
    25  	"github.com/matrixorigin/matrixone/pkg/pb/logservice"
    26  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestCmdPingDNWithEmptyDN(t *testing.T) {
    32  	ctx := context.Background()
    33  	clusterDetails := func() (logservice.ClusterDetails, error) {
    34  		return logservice.ClusterDetails{}, nil
    35  	}
    36  	proc := process.New(ctx, nil, nil, nil, nil, clusterDetails)
    37  	result, err := handlePing()(proc,
    38  		dn,
    39  		"",
    40  		func(ctx context.Context, cr []txn.CNOpRequest) ([]txn.CNOpResponse, error) {
    41  			return nil, nil
    42  		})
    43  	require.NoError(t, err)
    44  	assert.Equal(t, pb.CtlResult{Method: pb.CmdMethod_Ping.String(), Data: make([]interface{}, 0)},
    45  		result)
    46  }
    47  
    48  func TestCmdPingDNWithSingleDN(t *testing.T) {
    49  	shardID := uint64(1)
    50  	ctx := context.Background()
    51  	clusterDetails := func() (logservice.ClusterDetails, error) {
    52  		return logservice.ClusterDetails{
    53  			DNStores: []logservice.DNStore{
    54  				{
    55  					Shards: []logservice.DNShardInfo{
    56  						{
    57  							ShardID: 1,
    58  						},
    59  					},
    60  				},
    61  			},
    62  		}, nil
    63  	}
    64  	proc := process.New(ctx, nil, nil, nil, nil, clusterDetails)
    65  	result, err := handlePing()(proc,
    66  		dn,
    67  		"",
    68  		func(ctx context.Context, cr []txn.CNOpRequest) ([]txn.CNOpResponse, error) {
    69  			return []txn.CNOpResponse{
    70  				{
    71  					Payload: protoc.MustMarshal(&pb.DNPingResponse{ShardID: shardID}),
    72  				},
    73  			}, nil
    74  		})
    75  	require.NoError(t, err)
    76  	assert.Equal(t, pb.CtlResult{
    77  		Method: pb.CmdMethod_Ping.String(),
    78  		Data:   []interface{}{pb.DNPingResponse{ShardID: shardID}},
    79  	}, result)
    80  }
    81  
    82  func TestCmdPingDNWithMultiDN(t *testing.T) {
    83  	ctx := context.Background()
    84  	clusterDetails := func() (logservice.ClusterDetails, error) {
    85  		return logservice.ClusterDetails{
    86  			DNStores: []logservice.DNStore{
    87  				{
    88  					Shards: []logservice.DNShardInfo{
    89  						{
    90  							ShardID: 1,
    91  						},
    92  						{
    93  							ShardID: 2,
    94  						},
    95  					},
    96  				},
    97  			},
    98  		}, nil
    99  	}
   100  	proc := process.New(ctx, nil, nil, nil, nil, clusterDetails)
   101  	result, err := handlePing()(proc,
   102  		dn,
   103  		"",
   104  		func(ctx context.Context, cr []txn.CNOpRequest) ([]txn.CNOpResponse, error) {
   105  			return []txn.CNOpResponse{
   106  				{
   107  					Payload: protoc.MustMarshal(&pb.DNPingResponse{ShardID: 1}),
   108  				},
   109  				{
   110  					Payload: protoc.MustMarshal(&pb.DNPingResponse{ShardID: 2}),
   111  				},
   112  			}, nil
   113  		})
   114  	require.NoError(t, err)
   115  	assert.Equal(t, pb.CtlResult{
   116  		Method: pb.CmdMethod_Ping.String(),
   117  		Data:   []interface{}{pb.DNPingResponse{ShardID: 1}, pb.DNPingResponse{ShardID: 2}},
   118  	}, result)
   119  }
   120  
   121  func TestCmdPingDNWithParameter(t *testing.T) {
   122  	ctx := context.Background()
   123  	clusterDetails := func() (logservice.ClusterDetails, error) {
   124  		return logservice.ClusterDetails{
   125  			DNStores: []logservice.DNStore{
   126  				{
   127  					Shards: []logservice.DNShardInfo{
   128  						{
   129  							ShardID: 1,
   130  						},
   131  						{
   132  							ShardID: 2,
   133  						},
   134  					},
   135  				},
   136  			},
   137  		}, nil
   138  	}
   139  	proc := process.New(ctx, nil, nil, nil, nil, clusterDetails)
   140  	result, err := handlePing()(proc,
   141  		dn,
   142  		"1",
   143  		func(ctx context.Context, cr []txn.CNOpRequest) ([]txn.CNOpResponse, error) {
   144  			return []txn.CNOpResponse{
   145  				{
   146  					Payload: protoc.MustMarshal(&pb.DNPingResponse{ShardID: 1}),
   147  				},
   148  			}, nil
   149  		})
   150  	require.NoError(t, err)
   151  	assert.Equal(t, pb.CtlResult{
   152  		Method: pb.CmdMethod_Ping.String(),
   153  		Data:   []interface{}{pb.DNPingResponse{ShardID: 1}},
   154  	}, result)
   155  }