gitee.com/liu-zhao234568/cntest@v1.0.0/rpc/server_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rpc 18 19 import ( 20 "bufio" 21 "bytes" 22 "io" 23 "io/ioutil" 24 "net" 25 "path/filepath" 26 "strings" 27 "testing" 28 "time" 29 ) 30 31 func TestServerRegisterName(t *testing.T) { 32 server := NewServer() 33 service := new(testService) 34 35 if err := server.RegisterName("test", service); err != nil { 36 t.Fatalf("%v", err) 37 } 38 39 if len(server.services.services) != 2 { 40 t.Fatalf("Expected 2 service entries, got %d", len(server.services.services)) 41 } 42 43 svc, ok := server.services.services["test"] 44 if !ok { 45 t.Fatalf("Expected service calc to be registered") 46 } 47 48 wantCallbacks := 9 49 if len(svc.callbacks) != wantCallbacks { 50 t.Errorf("Expected %d callbacks for service 'service', got %d", wantCallbacks, len(svc.callbacks)) 51 } 52 } 53 54 func TestServer(t *testing.T) { 55 files, err := ioutil.ReadDir("testdata") 56 if err != nil { 57 t.Fatal("where'd my testdata go?") 58 } 59 for _, f := range files { 60 if f.IsDir() || strings.HasPrefix(f.Name(), ".") { 61 continue 62 } 63 path := filepath.Join("testdata", f.Name()) 64 name := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name())) 65 t.Run(name, func(t *testing.T) { 66 runTestScript(t, path) 67 }) 68 } 69 } 70 71 func runTestScript(t *testing.T, file string) { 72 server := newTestServer() 73 content, err := ioutil.ReadFile(file) 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 clientConn, serverConn := net.Pipe() 79 defer clientConn.Close() 80 go server.ServeCodec(NewCodec(serverConn), 0) 81 readbuf := bufio.NewReader(clientConn) 82 for _, line := range strings.Split(string(content), "\n") { 83 line = strings.TrimSpace(line) 84 switch { 85 case len(line) == 0 || strings.HasPrefix(line, "//"): 86 // skip comments, blank lines 87 continue 88 case strings.HasPrefix(line, "--> "): 89 t.Log(line) 90 // write to connection 91 clientConn.SetWriteDeadline(time.Now().Add(5 * time.Second)) 92 if _, err := io.WriteString(clientConn, line[4:]+"\n"); err != nil { 93 t.Fatalf("write error: %v", err) 94 } 95 case strings.HasPrefix(line, "<-- "): 96 t.Log(line) 97 want := line[4:] 98 // read line from connection and compare text 99 clientConn.SetReadDeadline(time.Now().Add(5 * time.Second)) 100 sent, err := readbuf.ReadString('\n') 101 if err != nil { 102 t.Fatalf("read error: %v", err) 103 } 104 sent = strings.TrimRight(sent, "\r\n") 105 if sent != want { 106 t.Errorf("wrong line from server\ngot: %s\nwant: %s", sent, want) 107 } 108 default: 109 panic("invalid line in test script: " + line) 110 } 111 } 112 } 113 114 // This test checks that responses are delivered for very short-lived connections that 115 // only carry a single request. 116 func TestServerShortLivedConn(t *testing.T) { 117 server := newTestServer() 118 defer server.Stop() 119 120 listener, err := net.Listen("tcp", "127.0.0.1:0") 121 if err != nil { 122 t.Fatal("can't listen:", err) 123 } 124 defer listener.Close() 125 go server.ServeListener(listener) 126 127 var ( 128 request = `{"jsonrpc":"2.0","id":1,"method":"rpc_modules"}` + "\n" 129 wantResp = `{"jsonrpc":"2.0","id":1,"result":{"nftest":"1.0","rpc":"1.0","test":"1.0"}}` + "\n" 130 deadline = time.Now().Add(10 * time.Second) 131 ) 132 for i := 0; i < 20; i++ { 133 conn, err := net.Dial("tcp", listener.Addr().String()) 134 if err != nil { 135 t.Fatal("can't dial:", err) 136 } 137 defer conn.Close() 138 conn.SetDeadline(deadline) 139 // Write the request, then half-close the connection so the server stops reading. 140 conn.Write([]byte(request)) 141 conn.(*net.TCPConn).CloseWrite() 142 // Now try to get the response. 143 buf := make([]byte, 2000) 144 n, err := conn.Read(buf) 145 if err != nil { 146 t.Fatal("read error:", err) 147 } 148 if !bytes.Equal(buf[:n], []byte(wantResp)) { 149 t.Fatalf("wrong response: %s", buf[:n]) 150 } 151 } 152 }