Repository: grpc/grpc-go
File: internal/transport/transport_test.go (Go)

Code before:
	for st := range server.conns {
		serverTransport = st.(*http2Server)
		break
	}
	server.mu.Unlock()

	if serverTransport == nil {
		t.Fatal("Server transport not found")
	}

	// Test case 1: StreamsSucceeded metric (eosReceived = true)
	testStreamSucceeded := func(t *testing.T) {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		stream := &ServerStream{
			Stream: &Stream{
				id:     123,
				ctx:    ctx,
				method: "/test.service/testmethod",
			},
		}

		serverTransport.mu.Lock()
		serverTransport.activeStreams[stream.id] = stream
		serverTransport.mu.Unlock()

		initialSucceeded := serverTransport.channelz.SocketMetrics.StreamsSucceeded.Load()
		initialFailed := serverTransport.channelz.SocketMetrics.StreamsFailed.Load()

		serverTransport.deleteStream(stream, true)
		serverTransport.deleteStream(stream, true)
		serverTransport.deleteStream(stream, true)

		finalSucceeded := serverTransport.channelz.SocketMetrics.StreamsSucceeded.Load()
		finalFailed := serverTransport.channelz.SocketMetrics.StreamsFailed.Load()

		if finalSucceeded != initialSucceeded+1 {
			t.Errorf("StreamsSucceeded: got %d, want %d (incremented by 1)", finalSucceeded, initialSucceeded+1)
		}
		if finalFailed != initialFailed {
			t.Errorf("StreamsFailed: got %d, want %d (unchanged)", finalFailed, initialFailed)
		}

		serverTransport.mu.Lock()
		_, exists := serverTransport.activeStreams[stream.id]
		serverTransport.mu.Unlock()
		if exists {
			t.Error("Stream should have been removed from activeStreams")
		}
	}

Review comment (bug):
Adding a stream into the active stream map without creating an actual stream will make the test brittle. The test may start failing due to a possible internal refactoring, even when the change is correct. Instead of doing this, we should use the client to create a real stream. See the following test as an example:
https://github.com/grpc/grpc-go/blob/7235bb7dcd13cc2c219f8e680ba65e31199d4791/internal/transport/transport_test.go#L1136-L1166

After this, we can use `serverTransport.maxStreamID` to get the stream ID of the new stream and call `serverTransport.deleteStream`.

I haven't tried this, so please feel free to reach out if there are issues in this approach.

Code after:

			// Setup server configuration with channelz support
			serverConfig := &ServerConfig{
				ChannelzParent: channelz.RegisterServer(t.Name()),
			}
			defer channelz.RemoveEntry(serverConfig.ChannelzParent.ID)

			// Create server and client with normal handler (not notifyCall)
			server, client, cancel := setUpWithOptions(t, 0, serverConfig, normal, ConnectOptions{})
			defer func() {
				client.Close(fmt.Errorf("test cleanup"))
				server.stop()
				cancel()
			}()

			// Wait for connection to be established
			waitWhileTrue(t, func() (bool, error) {
				server.mu.Lock()
				defer server.mu.Unlock()
				if len(server.conns) == 0 {
					return true, fmt.Errorf("timed-out while waiting for connection")
				}
				return false, nil
			})

			// Get the server transport
			server.mu.Lock()
			var serverTransport *http2Server
			for st := range server.conns {
				serverTransport = st.(*http2Server)
				break
			}
			server.mu.Unlock()

			if serverTransport == nil {
				t.Fatal("Server transport not found")
			}

			clientStream, err := client.NewStream(ctx, &CallHdr{})
			if err != nil {
				t.Fatalf("Failed to create stream: %v", err)
			}

			// Wait for the stream to be created on the server side
			var serverStream *ServerStream
			waitWhileTrue(t, func() (bool, error) {
				serverTransport.mu.Lock()
				defer serverTransport.mu.Unlock()
				for _, v := range serverTransport.activeStreams {
					if v.id == clientStream.id {
						serverStream = v