github.com/cloudwego/kitex@v0.9.0/internal/mocks/conn.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  	"net"
    21  	"time"
    22  )
    23  
    24  var _ net.Conn = &Conn{}
    25  
    26  // Conn implements the net.Conn interface.
    27  type Conn struct {
    28  	ReadFunc             func(b []byte) (n int, err error)
    29  	WriteFunc            func(b []byte) (n int, err error)
    30  	CloseFunc            func() (e error)
    31  	LocalAddrFunc        func() (r net.Addr)
    32  	RemoteAddrFunc       func() (r net.Addr)
    33  	SetDeadlineFunc      func(t time.Time) (e error)
    34  	SetReadDeadlineFunc  func(t time.Time) (e error)
    35  	SetWriteDeadlineFunc func(t time.Time) (e error)
    36  }
    37  
    38  // Read implements the net.Conn interface.
    39  func (m Conn) Read(b []byte) (n int, err error) {
    40  	if m.ReadFunc != nil {
    41  		return m.ReadFunc(b)
    42  	}
    43  	return
    44  }
    45  
    46  // Write implements the net.Conn interface.
    47  func (m Conn) Write(b []byte) (n int, err error) {
    48  	if m.WriteFunc != nil {
    49  		return m.WriteFunc(b)
    50  	}
    51  	return
    52  }
    53  
    54  // Close implements the net.Conn interface.
    55  func (m Conn) Close() (e error) {
    56  	if m.CloseFunc != nil {
    57  		return m.CloseFunc()
    58  	}
    59  	return
    60  }
    61  
    62  // LocalAddr implements the net.Conn interface.
    63  func (m Conn) LocalAddr() (r net.Addr) {
    64  	if m.LocalAddrFunc != nil {
    65  		return m.LocalAddrFunc()
    66  	}
    67  	return
    68  }
    69  
    70  // RemoteAddr implements the net.Conn interface.
    71  func (m Conn) RemoteAddr() (r net.Addr) {
    72  	if m.RemoteAddrFunc != nil {
    73  		return m.RemoteAddrFunc()
    74  	}
    75  	return
    76  }
    77  
    78  // SetDeadline implements the net.Conn interface.
    79  func (m Conn) SetDeadline(t time.Time) (e error) {
    80  	if m.SetDeadlineFunc != nil {
    81  		return m.SetDeadlineFunc(t)
    82  	}
    83  	return
    84  }
    85  
    86  // SetReadDeadline implements the net.Conn interface.
    87  func (m Conn) SetReadDeadline(t time.Time) (e error) {
    88  	if m.SetReadDeadlineFunc != nil {
    89  		return m.SetReadDeadlineFunc(t)
    90  	}
    91  	return
    92  }
    93  
    94  // SetWriteDeadline implements the net.Conn interface.
    95  func (m Conn) SetWriteDeadline(t time.Time) (e error) {
    96  	if m.SetWriteDeadlineFunc != nil {
    97  		return m.SetWriteDeadlineFunc(t)
    98  	}
    99  	return
   100  }