github.com/matrixorigin/matrixone@v1.2.0/pkg/frontend/protocol_test.go (about)

     1  // Copyright 2021 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 frontend
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/fagongzi/goetty/v2"
    22  	"github.com/fagongzi/goetty/v2/buf"
    23  	"github.com/fagongzi/goetty/v2/codec/simple"
    24  	"github.com/golang/mock/gomock"
    25  	"github.com/smartystreets/goconvey/convey"
    26  
    27  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    28  	mock_frontend "github.com/matrixorigin/matrixone/pkg/frontend/test"
    29  )
    30  
    31  func Test_protocol(t *testing.T) {
    32  	convey.Convey("test protocol.go succ", t, func() {
    33  		req := &Request{}
    34  		req.SetCmd(1)
    35  		convey.So(req.cmd, convey.ShouldEqual, 1)
    36  
    37  		res := &Response{}
    38  		res.SetStatus(1)
    39  		convey.So(res.GetStatus(), convey.ShouldEqual, 1)
    40  
    41  		res.SetCategory(2)
    42  		convey.So(res.GetCategory(), convey.ShouldEqual, 2)
    43  
    44  		cpi := &ProtocolImpl{}
    45  		io := goetty.NewIOSession(goetty.WithSessionCodec(simple.NewStringCodec()))
    46  		cpi.tcpConn = io
    47  
    48  		str1 := cpi.Peer()
    49  		convey.So(str1, convey.ShouldEqual, "")
    50  	})
    51  }
    52  
    53  func Test_SendResponse(t *testing.T) {
    54  	ctx := context.TODO()
    55  	convey.Convey("SendResponse succ", t, func() {
    56  		ctrl := gomock.NewController(t)
    57  		defer ctrl.Finish()
    58  
    59  		iopackage := mock_frontend.NewMockIOPackage(ctrl)
    60  		iopackage.EXPECT().WriteUint8(gomock.Any(), gomock.Any(), gomock.Any()).Return(0).AnyTimes()
    61  		iopackage.EXPECT().WriteUint16(gomock.Any(), gomock.Any(), gomock.Any()).Return(0).AnyTimes()
    62  		iopackage.EXPECT().WriteUint32(gomock.Any(), gomock.Any(), gomock.Any()).Return(0).AnyTimes()
    63  
    64  		ioses := mock_frontend.NewMockIOSession(ctrl)
    65  		ioses.EXPECT().OutBuf().Return(buf.NewByteBuf(1024)).AnyTimes()
    66  		ioses.EXPECT().Write(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
    67  		ioses.EXPECT().Flush(gomock.Any()).AnyTimes()
    68  
    69  		mp := &MysqlProtocolImpl{}
    70  		mp.io = iopackage
    71  		mp.tcpConn = ioses
    72  		resp := &Response{}
    73  		resp.category = EoFResponse
    74  		err := mp.SendResponse(ctx, resp)
    75  		convey.So(err, convey.ShouldBeNil)
    76  
    77  		resp.SetData(moerr.NewInternalError(context.TODO(), ""))
    78  		resp.category = ErrorResponse
    79  		err = mp.SendResponse(ctx, resp)
    80  		convey.So(err, convey.ShouldBeNil)
    81  
    82  		resp.category = -1
    83  		err = mp.SendResponse(ctx, resp)
    84  		convey.So(err, convey.ShouldNotBeNil)
    85  	})
    86  }