golang.org/x/tools@v0.21.1-0.20240520172518-788d39e776b1/internal/jsonrpc2/servertest/servertest_test.go (about) 1 // Copyright 2020 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 servertest 6 7 import ( 8 "context" 9 "testing" 10 "time" 11 12 "golang.org/x/tools/internal/jsonrpc2" 13 ) 14 15 type msg struct { 16 Msg string 17 } 18 19 func fakeHandler(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error { 20 return reply(ctx, &msg{"pong"}, nil) 21 } 22 23 func TestTestServer(t *testing.T) { 24 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 25 defer cancel() 26 server := jsonrpc2.HandlerServer(fakeHandler) 27 tcpTS := NewTCPServer(ctx, server, nil) 28 defer tcpTS.Close() 29 pipeTS := NewPipeServer(server, nil) 30 defer pipeTS.Close() 31 32 tests := []struct { 33 name string 34 connector Connector 35 }{ 36 {"tcp", tcpTS}, 37 {"pipe", pipeTS}, 38 } 39 40 for _, test := range tests { 41 t.Run(test.name, func(t *testing.T) { 42 conn := test.connector.Connect(ctx) 43 conn.Go(ctx, jsonrpc2.MethodNotFound) 44 var got msg 45 if _, err := conn.Call(ctx, "ping", &msg{"ping"}, &got); err != nil { 46 t.Fatal(err) 47 } 48 if want := "pong"; got.Msg != want { 49 t.Errorf("conn.Call(...): returned %q, want %q", got, want) 50 } 51 }) 52 } 53 }