github.com/matrixorigin/matrixone@v0.7.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/matrixorigin/matrixone/pkg/common/moerr"
    26  	mock_frontend "github.com/matrixorigin/matrixone/pkg/frontend/test"
    27  	"github.com/smartystreets/goconvey/convey"
    28  )
    29  
    30  func Test_protocol(t *testing.T) {
    31  	convey.Convey("test protocol.go succ", t, func() {
    32  		req := &Request{}
    33  		req.SetCmd(1)
    34  		convey.So(req.cmd, convey.ShouldEqual, 1)
    35  
    36  		res := &Response{}
    37  		res.SetStatus(1)
    38  		convey.So(res.GetStatus(), convey.ShouldEqual, 1)
    39  
    40  		res.SetCategory(2)
    41  		convey.So(res.GetCategory(), convey.ShouldEqual, 2)
    42  
    43  		cpi := &ProtocolImpl{}
    44  		io := goetty.NewIOSession(goetty.WithSessionCodec(simple.NewStringCodec()))
    45  		cpi.tcpConn = io
    46  
    47  		str1, str2, _, _ := cpi.Peer()
    48  		convey.So(str1, convey.ShouldEqual, "failed")
    49  		convey.So(str2, convey.ShouldEqual, "0")
    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  
    68  		mp := &MysqlProtocolImpl{}
    69  		mp.io = iopackage
    70  		mp.tcpConn = ioses
    71  		resp := &Response{}
    72  		resp.category = EoFResponse
    73  		err := mp.SendResponse(ctx, resp)
    74  		convey.So(err, convey.ShouldBeNil)
    75  
    76  		resp.SetData(moerr.NewInternalError(context.TODO(), ""))
    77  		resp.category = ErrorResponse
    78  		err = mp.SendResponse(ctx, resp)
    79  		convey.So(err, convey.ShouldBeNil)
    80  
    81  		resp.category = -1
    82  		err = mp.SendResponse(ctx, resp)
    83  		convey.So(err, convey.ShouldNotBeNil)
    84  	})
    85  }