libquadmath: Fix up libquadmath/math/sqrtq.c compilation in some powerpc* configurati...
[official-gcc.git] / libgo / go / net / rpc / client_test.go
blobffc12faedae9c9656f85e59e8a940df9d3082f04
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.
5 package rpc
7 import (
8 "errors"
9 "fmt"
10 "net"
11 "strings"
12 "testing"
15 type shutdownCodec struct {
16 responded chan int
17 closed bool
20 func (c *shutdownCodec) WriteRequest(*Request, any) error { return nil }
21 func (c *shutdownCodec) ReadResponseBody(any) error { return nil }
22 func (c *shutdownCodec) ReadResponseHeader(*Response) error {
23 c.responded <- 1
24 return errors.New("shutdownCodec ReadResponseHeader")
26 func (c *shutdownCodec) Close() error {
27 c.closed = true
28 return nil
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")
41 // Test that errors in gob shut down the connection. Issue 7689.
43 type R struct {
44 msg []byte // Not exported, so R does not work with gob.
47 type S struct{}
49 func (s *S) Recv(nul *struct{}, reply *R) error {
50 *reply = R{[]byte("foo")}
51 return nil
54 func TestGobError(t *testing.T) {
55 defer func() {
56 err := recover()
57 if err == nil {
58 t.Fatal("no error")
60 if !strings.Contains(err.(error).Error(), "reading body unexpected EOF") {
61 t.Fatal("expected `reading body unexpected EOF', got", err)
63 }()
64 Register(new(S))
66 listen, err := net.Listen("tcp", "127.0.0.1:0")
67 if err != nil {
68 panic(err)
70 go Accept(listen)
72 client, err := Dial("tcp", listen.Addr().String())
73 if err != nil {
74 panic(err)
77 var reply Reply
78 err = client.Call("S.Recv", &struct{}{}, &reply)
79 if err != nil {
80 panic(err)
83 fmt.Printf("%#v\n", reply)
84 client.Close()
86 listen.Close()