github.com/cloudwego/kitex@v0.9.0/internal/mocks/transhandlerclient.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package mocks
    18  
    19  import (
    20  	"context"
    21  	"net"
    22  
    23  	"github.com/cloudwego/kitex/pkg/kerrors"
    24  	"github.com/cloudwego/kitex/pkg/klog"
    25  	"github.com/cloudwego/kitex/pkg/remote"
    26  )
    27  
    28  type mockCliTransHandlerFactory struct {
    29  	hdlr *MockCliTransHandler
    30  }
    31  
    32  // NewMockCliTransHandlerFactory .
    33  func NewMockCliTransHandlerFactory(hdrl *MockCliTransHandler) remote.ClientTransHandlerFactory {
    34  	return &mockCliTransHandlerFactory{hdrl}
    35  }
    36  
    37  func (f *mockCliTransHandlerFactory) NewTransHandler(opt *remote.ClientOption) (remote.ClientTransHandler, error) {
    38  	f.hdlr.opt = opt
    39  	return f.hdlr, nil
    40  }
    41  
    42  // MockCliTransHandler .
    43  type MockCliTransHandler struct {
    44  	opt       *remote.ClientOption
    45  	transPipe *remote.TransPipeline
    46  
    47  	WriteFunc func(ctx context.Context, conn net.Conn, send remote.Message) (nctx context.Context, err error)
    48  
    49  	ReadFunc func(ctx context.Context, conn net.Conn, msg remote.Message) (nctx context.Context, err error)
    50  
    51  	OnMessageFunc func(ctx context.Context, args, result remote.Message) (context.Context, error)
    52  }
    53  
    54  // Write implements the remote.TransHandler interface.
    55  func (t *MockCliTransHandler) Write(ctx context.Context, conn net.Conn, send remote.Message) (nctx context.Context, err error) {
    56  	if t.WriteFunc != nil {
    57  		return t.WriteFunc(ctx, conn, send)
    58  	}
    59  	return ctx, nil
    60  }
    61  
    62  // Read implements the remote.TransHandler interface.
    63  func (t *MockCliTransHandler) Read(ctx context.Context, conn net.Conn, msg remote.Message) (nctx context.Context, err error) {
    64  	if t.ReadFunc != nil {
    65  		return t.ReadFunc(ctx, conn, msg)
    66  	}
    67  	return ctx, nil
    68  }
    69  
    70  // OnMessage implements the remote.TransHandler interface.
    71  func (t *MockCliTransHandler) OnMessage(ctx context.Context, args, result remote.Message) (context.Context, error) {
    72  	if t.OnMessageFunc != nil {
    73  		return t.OnMessageFunc(ctx, args, result)
    74  	}
    75  	return ctx, nil
    76  }
    77  
    78  // OnActive implements the remote.TransHandler interface.
    79  func (t *MockCliTransHandler) OnActive(ctx context.Context, conn net.Conn) (context.Context, error) {
    80  	// ineffective now and do nothing
    81  	return ctx, nil
    82  }
    83  
    84  // OnInactive implements the remote.TransHandler interface.
    85  func (t *MockCliTransHandler) OnInactive(ctx context.Context, conn net.Conn) {
    86  	// ineffective now and do nothing
    87  }
    88  
    89  // OnError implements the remote.TransHandler interface.
    90  func (t *MockCliTransHandler) OnError(ctx context.Context, err error, conn net.Conn) {
    91  	if pe, ok := err.(*kerrors.DetailedError); ok {
    92  		klog.CtxErrorf(ctx, "KITEX: send request error, remote=%s, error=%s\nstack=%s", conn.RemoteAddr(), err.Error(), pe.Stack())
    93  	} else {
    94  		klog.CtxErrorf(ctx, "KITEX: send request error, remote=%s, error=%s", conn.RemoteAddr(), err.Error())
    95  	}
    96  }
    97  
    98  // SetPipeline implements the remote.TransHandler interface.
    99  func (t *MockCliTransHandler) SetPipeline(p *remote.TransPipeline) {
   100  	t.transPipe = p
   101  }