| 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 jsonrpc2_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 9 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 11 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 12 | "runtime/debugscip-go gomod github.com/golang/go/src go1.25.0 `runtime/debug`/" |
| 13 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 14 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 15 | |
| 16 | jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ "golang.org/x/tools/internal/jsonrpc2_v2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/" |
| 17 | ) |
| 18 | |
| 19 | // needsLocalhostNet skips t if networking does not work for ports opened |
| 20 | // with "localhost". |
| 21 | // forked from golang.org/x/tools/internal/testenv. |
| 22 | func needsLocalhostNetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/needsLocalhostNet().(t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#) { |
| 23 | switch runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. { |
| 24 | case "js", "wasip1": |
| 25 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Skipf().(`Listening on "localhost" fails on %s; see https://go.dev/issue/59718`, runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS.) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestIdleTimeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/TestIdleTimeout().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 30 | needsLocalhostNetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/needsLocalhostNet().(t) |
| 31 | |
| 32 | // Use a panicking time.AfterFunc instead of context.WithTimeout so that we |
| 33 | // get a goroutine dump on failure. We expect the test to take on the order of |
| 34 | // a few tens of milliseconds at most, so 10s should be several orders of |
| 35 | // magnitude of headroom. |
| 36 | timer := timescip-go gomod github.com/golang/go/src go1.25.0 time/.AfterFuncscip-go gomod github.com/golang/go/src go1.25.0 time/AfterFunc().(10*timescip-go gomod github.com/golang/go/src go1.25.0 time/.Secondscip-go gomod github.com/golang/go/src go1.25.0 time/Second., func() { |
| 37 | debugscip-go gomod github.com/golang/go/src go1.25.0 `runtime/debug`/.SetTracebackscip-go gomod github.com/golang/go/src go1.25.0 `runtime/debug`/SetTraceback().("all") |
| 38 | panic("TestIdleTimeout deadlocked") |
| 39 | }) |
| 40 | defer timer.Stopscip-go gomod github.com/golang/go/src go1.25.0 time/Timer#Stop().() |
| 41 | |
| 42 | ctx := contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().() |
| 43 | |
| 44 | try := func(d timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#) (longEnough bool) { |
| 45 | listener, err := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NetListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NetListener().(ctx, "tcp", "localhost:0", jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NetListenOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NetListenOptions#{}) |
| 46 | if err != nil { |
| 47 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 48 | } |
| 49 | |
| 50 | idleStart := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().() |
| 51 | listener = jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NewIdleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NewIdleListener().(d, listener) |
| 52 | defer listener.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Close.() |
| 53 | |
| 54 | server := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NewServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NewServer().(ctx, listener, jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#{}) |
| 55 | |
| 56 | // Exercise some connection/disconnection patterns, and then assert that when |
| 57 | // our timer fires, the server exits. |
| 58 | conn1, err := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Dialscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dial().(ctx, listener.Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Dialer.(), jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#{}, nil) |
| 59 | if err != nil { |
| 60 | if since := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(idleStart); since < d { |
| 61 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("conn1 failed to connect after %v: %v", since, err) |
| 62 | } |
| 63 | t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Log().("jsonrpc2.Dial:", err) |
| 64 | return false // Took to long to dial, so the failure could have been due to the idle timeout. |
| 65 | } |
| 66 | // On the server side, Accept can race with the connection timing out. |
| 67 | // Send a call and wait for the response to ensure that the connection was |
| 68 | // actually fully accepted. |
| 69 | ac := conn1.Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Call().(ctx, "ping", nil) |
| 70 | if err := ac.Awaitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#Await().(ctx, nil); !errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Isscip-go gomod github.com/golang/go/src go1.25.0 errors/Is().(err, jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ErrMethodNotFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrMethodNotFound.) { |
| 71 | if since := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(idleStart); since < d { |
| 72 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("conn1 broken after %v: %v", since, err) |
| 73 | } |
| 74 | t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Log().(`conn1.Call(ctx, "ping", nil):`, err) |
| 75 | conn1.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Close().() |
| 76 | return false |
| 77 | } |
| 78 | |
| 79 | // Since conn1 was successfully accepted and remains open, the server is |
| 80 | // definitely non-idle. Dialing another simultaneous connection should |
| 81 | // succeed. |
| 82 | conn2, err := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Dialscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dial().(ctx, listener.Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Dialer.(), jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#{}, nil) |
| 83 | if err != nil { |
| 84 | conn1.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Close().() |
| 85 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("conn2 failed to connect while non-idle after %v: %v", timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(idleStart), err) |
| 86 | return false |
| 87 | } |
| 88 | // Ensure that conn2 is also accepted on the server side before we close |
| 89 | // conn1. Otherwise, the connection can appear idle if the server processes |
| 90 | // the closure of conn1 and the idle timeout before it finally notices conn2 |
| 91 | // in the accept queue. |
| 92 | // (That failure mode may explain the failure noted in |
| 93 | // https://go.dev/issue/49387#issuecomment-1303979877.) |
| 94 | ac = conn2.Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Call().(ctx, "ping", nil) |
| 95 | if err := ac.Awaitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#Await().(ctx, nil); !errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Isscip-go gomod github.com/golang/go/src go1.25.0 errors/Is().(err, jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ErrMethodNotFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrMethodNotFound.) { |
| 96 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("conn2 broken while non-idle after %v: %v", timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(idleStart), err) |
| 97 | } |
| 98 | |
| 99 | if err := conn1.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Close().(); err != nil { |
| 100 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("conn1.Close failed with error: %v", err) |
| 101 | } |
| 102 | idleStart = timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().() |
| 103 | if err := conn2.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Close().(); err != nil { |
| 104 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("conn2.Close failed with error: %v", err) |
| 105 | } |
| 106 | |
| 107 | conn3, err := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Dialscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dial().(ctx, listener.Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Dialer.(), jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#{}, nil) |
| 108 | if err != nil { |
| 109 | if since := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(idleStart); since < d { |
| 110 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("conn3 failed to connect after %v: %v", since, err) |
| 111 | } |
| 112 | t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Log().("jsonrpc2.Dial:", err) |
| 113 | return false // Took to long to dial, so the failure could have been due to the idle timeout. |
| 114 | } |
| 115 | |
| 116 | ac = conn3.Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Call().(ctx, "ping", nil) |
| 117 | if err := ac.Awaitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#Await().(ctx, nil); !errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Isscip-go gomod github.com/golang/go/src go1.25.0 errors/Is().(err, jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ErrMethodNotFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrMethodNotFound.) { |
| 118 | if since := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(idleStart); since < d { |
| 119 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("conn3 broken after %v: %v", since, err) |
| 120 | } |
| 121 | t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Log().(`conn3.Call(ctx, "ping", nil):`, err) |
| 122 | conn3.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Close().() |
| 123 | return false |
| 124 | } |
| 125 | |
| 126 | idleStart = timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().() |
| 127 | if err := conn3.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Close().(); err != nil { |
| 128 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("conn3.Close failed with error: %v", err) |
| 129 | } |
| 130 | |
| 131 | serverError := server.Waitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#Wait().() |
| 132 | |
| 133 | if !errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Isscip-go gomod github.com/golang/go/src go1.25.0 errors/Is().(serverError, jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ErrIdleTimeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrIdleTimeout.) { |
| 134 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("run() returned error %v, want %v", serverError, jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ErrIdleTimeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrIdleTimeout.) |
| 135 | } |
| 136 | if since := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(idleStart); since < d { |
| 137 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("server shut down after %v idle; want at least %v", since, d) |
| 138 | } |
| 139 | return true |
| 140 | } |
| 141 | |
| 142 | d := 1 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond. |
| 143 | for { |
| 144 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("testing with idle timeout %v", d) |
| 145 | if !try(d) { |
| 146 | d *= 2 |
| 147 | continue |
| 148 | } |
| 149 | break |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | type msgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/msg# struct { |
| 154 | Msgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/msg#Msg. string |
| 155 | } |
| 156 | |
| 157 | type fakeHandlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/fakeHandler# struct{} |
| 158 | |
| 159 | func (fakeHandlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/fakeHandler#) Handlescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/fakeHandler#Handle().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, req *jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#) (any, error) { |
| 160 | switch req.Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#Method. { |
| 161 | case "ping": |
| 162 | return &msgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/msg#{"pong"}, nil |
| 163 | default: |
| 164 | return nil, jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ErrNotHandledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrNotHandled. |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestServescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/TestServe().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 169 | ctx := contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().() |
| 170 | |
| 171 | tests := []struct { |
| 172 | name string |
| 173 | factory func(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#) (jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#, error) |
| 174 | }{ |
| 175 | {"tcp", func(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#) (jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#, error) { |
| 176 | needsLocalhostNetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/needsLocalhostNet().(t) |
| 177 | return jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NetListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NetListener().(ctx, "tcp", "localhost:0", jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NetListenOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NetListenOptions#{}) |
| 178 | }}, |
| 179 | {"pipe", func(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#) (jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#, error) { |
| 180 | return jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NetPipeListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NetPipeListener().(ctx) |
| 181 | }}, |
| 182 | } |
| 183 | |
| 184 | for _, test := range tests { |
| 185 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(test.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 186 | fake, err := test.factory(ctx, t) |
| 187 | if err != nil { |
| 188 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 189 | } |
| 190 | conn, shutdown, err := newFakescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/newFake().(t, ctx, fake) |
| 191 | if err != nil { |
| 192 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 193 | } |
| 194 | defer shutdown() |
| 195 | var got msgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/msg# |
| 196 | if err := conn.Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Call().(ctx, "ping", &msgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/msg#{"ting"}).Awaitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#Await().(ctx, &got); err != nil { |
| 197 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 198 | } |
| 199 | if want := "pong"; got.Msgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/msg#Msg. != want { |
| 200 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("conn.Call(...): returned %q, want %q", got, want) |
| 201 | } |
| 202 | }) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | func newFakescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/newFake().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, l jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#) (*jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#, func(), error) { |
| 207 | server := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NewServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NewServer().(ctx, l, jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#{ |
| 208 | Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#Handler.: fakeHandlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/fakeHandler#{}, |
| 209 | }) |
| 210 | |
| 211 | client, err := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Dialscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dial().(ctx, |
| 212 | l.Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Dialer.(), |
| 213 | jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#{ |
| 214 | Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#Handler.: fakeHandlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/fakeHandler#{}, |
| 215 | }, nil) |
| 216 | if err != nil { |
| 217 | return nil, nil, err |
| 218 | } |
| 219 | return client, func() { |
| 220 | if err := l.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Close.(); err != nil { |
| 221 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 222 | } |
| 223 | if err := client.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Close().(); err != nil { |
| 224 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 225 | } |
| 226 | server.Waitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#Wait().() |
| 227 | }, nil |
| 228 | } |
| 229 | |
| 230 | // TestIdleListenerAcceptCloseRace checks for the Accept/Close race fixed in CL 388597. |
| 231 | // |
| 232 | // (A bug in the idleListener implementation caused a successful Accept to block |
| 233 | // on sending to a background goroutine that could have already exited.) |
| 234 | func TestIdleListenerAcceptCloseRacescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/TestIdleListenerAcceptCloseRace().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 235 | ctx := contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().() |
| 236 | |
| 237 | n := 10 |
| 238 | |
| 239 | // Each iteration of the loop appears to take around a millisecond, so to |
| 240 | // avoid spurious failures we'll set the watchdog for three orders of |
| 241 | // magnitude longer. When the bug was present, this reproduced the deadlock |
| 242 | // reliably on a Linux workstation when run with -count=100, which should be |
| 243 | // frequent enough to show up on the Go build dashboard if it regresses. |
| 244 | watchdog := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#(n) * 1000 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond. |
| 245 | timer := timescip-go gomod github.com/golang/go/src go1.25.0 time/.AfterFuncscip-go gomod github.com/golang/go/src go1.25.0 time/AfterFunc().(watchdog, func() { |
| 246 | debugscip-go gomod github.com/golang/go/src go1.25.0 `runtime/debug`/.SetTracebackscip-go gomod github.com/golang/go/src go1.25.0 `runtime/debug`/SetTraceback().("all") |
| 247 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s deadlocked after %v", t.Namescip-go gomod github.com/golang/go/src go1.25.0 testing/common#Name().(), watchdog)) |
| 248 | }) |
| 249 | defer timer.Stopscip-go gomod github.com/golang/go/src go1.25.0 time/Timer#Stop().() |
| 250 | |
| 251 | for ; n > 0; n-- { |
| 252 | listener, err := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NetPipeListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NetPipeListener().(ctx) |
| 253 | if err != nil { |
| 254 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 255 | } |
| 256 | listener = jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NewIdleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NewIdleListener().(24*timescip-go gomod github.com/golang/go/src go1.25.0 time/.Hourscip-go gomod github.com/golang/go/src go1.25.0 time/Hour., listener) |
| 257 | |
| 258 | done := make(chan struct{}) |
| 259 | go func() { |
| 260 | conn, err := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Dialscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dial().(ctx, listener.Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Dialer.(), jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#{}, nil) |
| 261 | listener.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Close.() |
| 262 | if err == nil { |
| 263 | conn.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Close().() |
| 264 | } |
| 265 | close(done) |
| 266 | }() |
| 267 | |
| 268 | // Accept may return a non-nil error if Close closes the underlying network |
| 269 | // connection before the wrapped Accept call unblocks. However, it must not |
| 270 | // deadlock! |
| 271 | c, err := listener.Acceptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Accept.(ctx) |
| 272 | if err == nil { |
| 273 | c.Closescip-go gomod github.com/golang/go/src go1.25.0 io/Closer#Close().() |
| 274 | } |
| 275 | <-done |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // TestCloseCallRace checks for a race resulting in a deadlock when a Call on |
| 280 | // one side of the connection races with a Close (or otherwise broken |
| 281 | // connection) initiated from the other side. |
| 282 | // |
| 283 | // (The Call method was waiting for a result from the Read goroutine to |
| 284 | // determine which error value to return, but the Read goroutine was waiting for |
| 285 | // in-flight calls to complete before reporting that result.) |
| 286 | func TestCloseCallRacescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/TestCloseCallRace().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 287 | ctx := contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().() |
| 288 | n := 10 |
| 289 | |
| 290 | watchdog := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#(n) * 1000 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond. |
| 291 | timer := timescip-go gomod github.com/golang/go/src go1.25.0 time/.AfterFuncscip-go gomod github.com/golang/go/src go1.25.0 time/AfterFunc().(watchdog, func() { |
| 292 | debugscip-go gomod github.com/golang/go/src go1.25.0 `runtime/debug`/.SetTracebackscip-go gomod github.com/golang/go/src go1.25.0 `runtime/debug`/SetTraceback().("all") |
| 293 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s deadlocked after %v", t.Namescip-go gomod github.com/golang/go/src go1.25.0 testing/common#Name().(), watchdog)) |
| 294 | }) |
| 295 | defer timer.Stopscip-go gomod github.com/golang/go/src go1.25.0 time/Timer#Stop().() |
| 296 | |
| 297 | for ; n > 0; n-- { |
| 298 | listener, err := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NetPipeListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NetPipeListener().(ctx) |
| 299 | if err != nil { |
| 300 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 301 | } |
| 302 | |
| 303 | pokec := make(chan *jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.AsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#, 1) |
| 304 | |
| 305 | s := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.NewServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NewServer().(ctx, listener, jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.BinderFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/BinderFunc#(func(_ contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, srvConn *jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions# { |
| 306 | h := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.HandlerFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/HandlerFunc#(func(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, _ *jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#) (any, error) { |
| 307 | // Start a concurrent call from the server to the client. |
| 308 | // The point of this test is to ensure this doesn't deadlock |
| 309 | // if the client shuts down the connection concurrently. |
| 310 | // |
| 311 | // The racing Call may or may not receive a response: it should get a |
| 312 | // response if it is sent before the client closes the connection, and |
| 313 | // it should fail with some kind of "connection closed" error otherwise. |
| 314 | go func() { |
| 315 | pokec <- srvConn.Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Call().(ctx, "poke", nil) |
| 316 | }() |
| 317 | |
| 318 | return &msgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2_test`/msg#{"pong"}, nil |
| 319 | }) |
| 320 | return jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#{Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#Handler.: h} |
| 321 | })) |
| 322 | |
| 323 | dialConn, err := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.Dialscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dial().(ctx, listener.Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Dialer.(), jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#{}, nil) |
| 324 | if err != nil { |
| 325 | listener.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Close.() |
| 326 | s.Waitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#Wait().() |
| 327 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 328 | } |
| 329 | |
| 330 | // Calling any method on the server should provoke it to asynchronously call |
| 331 | // us back. While it is starting that call, we will close the connection. |
| 332 | if err := dialConn.Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Call().(ctx, "ping", nil).Awaitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#Await().(ctx, nil); err != nil { |
| 333 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err) |
| 334 | } |
| 335 | if err := dialConn.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Close().(); err != nil { |
| 336 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err) |
| 337 | } |
| 338 | |
| 339 | // Ensure that the Call on the server side did not block forever when the |
| 340 | // connection closed. |
| 341 | pokeCall := <-pokec |
| 342 | if err := pokeCall.Awaitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#Await().(ctx, nil); err == nil { |
| 343 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected nil error from server-initited call") |
| 344 | } else if errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Isscip-go gomod github.com/golang/go/src go1.25.0 errors/Is().(err, jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/.ErrMethodNotFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrMethodNotFound.) { |
| 345 | // The call completed before the Close reached the handler. |
| 346 | } else { |
| 347 | // The error was something else. |
| 348 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("server-initiated call completed with expected error: %v", err) |
| 349 | } |
| 350 | |
| 351 | listener.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Close.() |
| 352 | s.Waitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#Wait().() |
| 353 | } |
| 354 | } |