Home | History | Annotate | Download | only in rpc
      1 // Copyright 2014 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package rpc
      6 
      7 import (
      8 	"errors"
      9 	"fmt"
     10 	"net"
     11 	"strings"
     12 	"testing"
     13 )
     14 
     15 type shutdownCodec struct {
     16 	responded chan int
     17 	closed    bool
     18 }
     19 
     20 func (c *shutdownCodec) WriteRequest(*Request, interface{}) error { return nil }
     21 func (c *shutdownCodec) ReadResponseBody(interface{}) error       { return nil }
     22 func (c *shutdownCodec) ReadResponseHeader(*Response) error {
     23 	c.responded <- 1
     24 	return errors.New("shutdownCodec ReadResponseHeader")
     25 }
     26 func (c *shutdownCodec) Close() error {
     27 	c.closed = true
     28 	return nil
     29 }
     30 
     31 func TestCloseCodec(t *testing.T) {
     32 	codec := &shutdownCodec{responded: make(chan int)}
     33 	client := NewClientWithCodec(codec)
     34 	<-codec.responded
     35 	client.Close()
     36 	if !codec.closed {
     37 		t.Error("client.Close did not close codec")
     38 	}
     39 }
     40 
     41 // Test that errors in gob shut down the connection. Issue 7689.
     42 
     43 type R struct {
     44 	msg []byte // Not exported, so R does not work with gob.
     45 }
     46 
     47 type S struct{}
     48 
     49 func (s *S) Recv(nul *struct{}, reply *R) error {
     50 	*reply = R{[]byte("foo")}
     51 	return nil
     52 }
     53 
     54 func TestGobError(t *testing.T) {
     55 	defer func() {
     56 		err := recover()
     57 		if err == nil {
     58 			t.Fatal("no error")
     59 		}
     60 		if !strings.Contains("reading body EOF", err.(error).Error()) {
     61 			t.Fatal("expected `reading body EOF', got", err)
     62 		}
     63 	}()
     64 	Register(new(S))
     65 
     66 	listen, err := net.Listen("tcp", "127.0.0.1:0")
     67 	if err != nil {
     68 		panic(err)
     69 	}
     70 	go Accept(listen)
     71 
     72 	client, err := Dial("tcp", listen.Addr().String())
     73 	if err != nil {
     74 		panic(err)
     75 	}
     76 
     77 	var reply Reply
     78 	err = client.Call("S.Recv", &struct{}{}, &reply)
     79 	if err != nil {
     80 		panic(err)
     81 	}
     82 
     83 	fmt.Printf("%#v\n", reply)
     84 	client.Close()
     85 
     86 	listen.Close()
     87 }
     88