...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/drivertest/channel_netconn.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/drivertest

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package drivertest
     8  
     9  import (
    10  	"errors"
    11  	"net"
    12  	"time"
    13  )
    14  
    15  // ChannelNetConn implements the net.Conn interface by reading and writing wire messages to a channel.
    16  type ChannelNetConn struct {
    17  	WriteErr error
    18  	Written  chan []byte
    19  	ReadResp chan []byte
    20  	ReadErr  chan error
    21  }
    22  
    23  // Read reads data from the connection
    24  func (c *ChannelNetConn) Read(b []byte) (int, error) {
    25  	var wm []byte
    26  	var err error
    27  	select {
    28  	case wm = <-c.ReadResp:
    29  	case err = <-c.ReadErr:
    30  	}
    31  	return copy(b, wm), err
    32  }
    33  
    34  // Write writes data to the connection.
    35  func (c *ChannelNetConn) Write(b []byte) (int, error) {
    36  	copyBuf := make([]byte, len(b))
    37  	copy(copyBuf, b)
    38  
    39  	select {
    40  	case c.Written <- copyBuf:
    41  	default:
    42  		c.WriteErr = errors.New("could not write wm to Written channel")
    43  	}
    44  	return len(b), c.WriteErr
    45  }
    46  
    47  // Close closes the connection.
    48  func (c *ChannelNetConn) Close() error {
    49  	return nil
    50  }
    51  
    52  // LocalAddr returns the local network address.
    53  func (c *ChannelNetConn) LocalAddr() net.Addr {
    54  	return nil
    55  }
    56  
    57  // RemoteAddr returns the remote network address.
    58  func (c *ChannelNetConn) RemoteAddr() net.Addr {
    59  	return nil
    60  }
    61  
    62  // SetDeadline sets the read and write deadlines associated with the connection.
    63  func (c *ChannelNetConn) SetDeadline(_ time.Time) error {
    64  	return nil
    65  }
    66  
    67  // SetReadDeadline sets the read and write deadlines associated with the connection.
    68  func (c *ChannelNetConn) SetReadDeadline(_ time.Time) error {
    69  	return nil
    70  }
    71  
    72  // SetWriteDeadline sets the read and write deadlines associated with the connection.
    73  func (c *ChannelNetConn) SetWriteDeadline(_ time.Time) error {
    74  	return nil
    75  }
    76  
    77  // GetWrittenMessage gets the last wire message written to the connection
    78  func (c *ChannelNetConn) GetWrittenMessage() []byte {
    79  	select {
    80  	case wm := <-c.Written:
    81  		return wm
    82  	default:
    83  		return nil
    84  	}
    85  }
    86  
    87  // AddResponse adds a response to the connection.
    88  func (c *ChannelNetConn) AddResponse(resp []byte) error {
    89  	select {
    90  	case c.ReadResp <- resp[:4]:
    91  	default:
    92  		return errors.New("could not write length bytes")
    93  	}
    94  
    95  	select {
    96  	case c.ReadResp <- resp[4:]:
    97  	default:
    98  		return errors.New("could not write response bytes")
    99  	}
   100  
   101  	return nil
   102  }
   103  

View as plain text