internal/jsonrpc2_v2: eliminate a potential Accept/Dial race in TestIdleTimeout

The only explanation I can think of for the failure in
https://go.dev/issue/49387#issuecomment-1303979877 is that maybe the
idle timeout started as soon as conn1 was closed, without waiting for
conn2 to be closed. That might be possible if the connection returned
by Dial was still in the server's accept queue, but never actually
accepted.

To eliminate that possibility, we can send an RPC on that connection
and wait for a response, as we already do with conn1. Since the conn1
RPC succeeded, we know that the connection is non-idle, conn2 should
be accepted, and the request on conn2 should succeed unconditionally.

Fixes golang/go#49387 (hopefully for real this time).

Change-Id: Ie3e74f91d322223d82c000fdf1f3a0ed08afd20d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/448096
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
diff --git a/internal/jsonrpc2_v2/serve_test.go b/internal/jsonrpc2_v2/serve_test.go
index 21bf0bb..88ac66b 100644
--- a/internal/jsonrpc2_v2/serve_test.go
+++ b/internal/jsonrpc2_v2/serve_test.go
@@ -66,15 +66,25 @@
 			return false
 		}
 
+		// Since conn1 was successfully accepted and remains open, the server is
+		// definitely non-idle. Dialing another simultaneous connection should
+		// succeed.
 		conn2, err := jsonrpc2.Dial(ctx, listener.Dialer(), jsonrpc2.ConnectionOptions{})
 		if err != nil {
 			conn1.Close()
-			if since := time.Since(idleStart); since < d {
-				t.Fatalf("conn2 failed to connect while non-idle: %v", err)
-			}
-			t.Log("jsonrpc2.Dial:", err)
+			t.Fatalf("conn2 failed to connect while non-idle after %v: %v", time.Since(idleStart), err)
 			return false
 		}
+		// Ensure that conn2 is also accepted on the server side before we close
+		// conn1. Otherwise, the connection can appear idle if the server processes
+		// the closure of conn1 and the idle timeout before it finally notices conn2
+		// in the accept queue.
+		// (That failure mode may explain the failure noted in
+		// https://go.dev/issue/49387#issuecomment-1303979877.)
+		ac = conn2.Call(ctx, "ping", nil)
+		if err := ac.Await(ctx, nil); !errors.Is(err, jsonrpc2.ErrMethodNotFound) {
+			t.Fatalf("conn2 broken while non-idle after %v: %v", time.Since(idleStart), err)
+		}
 
 		if err := conn1.Close(); err != nil {
 			t.Fatalf("conn1.Close failed with error: %v", err)