github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/server/mucp/rpc_codec_test.go (about)

     1  // Copyright 2020 Asim Aslam
     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  //     https://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  // Original source: github.com/micro/go-micro/v3/server/mucp/rpc_codec_test.go
    16  
    17  package mucp
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"testing"
    23  
    24  	"github.com/tickoalcantara12/micro/v3/service/network/transport"
    25  	"github.com/tickoalcantara12/micro/v3/util/codec"
    26  )
    27  
    28  // testCodec is a dummy codec that only knows how to encode nil bodies
    29  type testCodec struct {
    30  	buf *bytes.Buffer
    31  }
    32  
    33  type testSocket struct {
    34  	local  string
    35  	remote string
    36  }
    37  
    38  // TestCodecWriteError simulates what happens when a codec is unable
    39  // to encode a message (e.g. a missing branch of an "oneof" message in
    40  // protobufs)
    41  //
    42  // We expect an error to be sent to the socket. Previously the socket
    43  // would remain open with no bytes sent, leading to client-side
    44  // timeouts.
    45  func TestCodecWriteError(t *testing.T) {
    46  	socket := testSocket{}
    47  	message := transport.Message{
    48  		Header: map[string]string{},
    49  		Body:   []byte{},
    50  	}
    51  
    52  	rwc := readWriteCloser{
    53  		rbuf: new(bytes.Buffer),
    54  		wbuf: new(bytes.Buffer),
    55  	}
    56  
    57  	c := rpcCodec{
    58  		buf: &rwc,
    59  		codec: &testCodec{
    60  			buf: rwc.wbuf,
    61  		},
    62  		req:    &message,
    63  		socket: socket,
    64  	}
    65  
    66  	err := c.Write(&codec.Message{
    67  		Endpoint: "Service.Endpoint",
    68  		Id:       "0",
    69  		Error:    "",
    70  	}, "body")
    71  
    72  	if err != nil {
    73  		t.Fatalf(`Expected Write to fail; got "%+v" instead`, err)
    74  	}
    75  
    76  	const expectedError = "Unable to encode body: simulating a codec write failure"
    77  	actualError := rwc.wbuf.String()
    78  	if actualError != expectedError {
    79  		t.Fatalf(`Expected error "%+v" in the write buffer, got "%+v" instead`, expectedError, actualError)
    80  	}
    81  }
    82  
    83  func (c *testCodec) ReadHeader(message *codec.Message, typ codec.MessageType) error {
    84  	return nil
    85  }
    86  
    87  func (c *testCodec) ReadBody(dest interface{}) error {
    88  	return nil
    89  }
    90  
    91  func (c *testCodec) Write(message *codec.Message, dest interface{}) error {
    92  	if dest != nil {
    93  		return errors.New("simulating a codec write failure")
    94  	}
    95  	c.buf.Write([]byte(message.Error))
    96  	return nil
    97  }
    98  
    99  func (c *testCodec) Close() error {
   100  	return nil
   101  }
   102  
   103  func (c *testCodec) String() string {
   104  	return "string"
   105  }
   106  
   107  func (s testSocket) Local() string {
   108  	return s.local
   109  }
   110  
   111  func (s testSocket) Remote() string {
   112  	return s.remote
   113  }
   114  
   115  func (s testSocket) Recv(message *transport.Message) error {
   116  	return nil
   117  }
   118  
   119  func (s testSocket) Send(message *transport.Message) error {
   120  	return nil
   121  }
   122  
   123  func (s testSocket) Close() error {
   124  	return nil
   125  }