Repository: go-kratos/kratos
Pull request #1477 (2021-09-15 02:22:09)

fix(transport/http): defer leak in test
<!--
🎉 Thanks for sending a pull request to Kratos! Here are some tips for you:

1. If this is your first time contributing to Kratos, please read our contribution guide: https://go-kratos.dev/en/docs/community/contribution/
2. Ensure you have added or ran the appropriate tests and lint for your PR, please use `make lint` and `make test` before filing your PR, use `make clean` to tidy your go mod.
3. If the PR is unfinished, you may need mark it as a WIP(Work In Progress) PR or draft PR
4. Please use a conventional commits format title: `<type>[optional scope]: <description>`
    
    Some suggestion on <type>:

    fix: A bug fix
    feat: A new feature
    test: Adding missing tests or correcting existing tests
    refactor: A code change that neither fixes a bug nor adds a feature
    break: Changes has break change

    docs: Documentation only changes
    deps: Changes external dependencies
    style: Changes that do not affect the meaning of the code (white-space, formatting, etc)
    chore Daily work, examples, etc.
    ci: Changes to our CI configuration files and scripts
-->

#### Description (what this PR does / why we need it):
<!--
* The description should include the motivation for this PR or contrast this with previous behavior
-->


#### Which issue(s) this PR fixes (resolves / be part of):
<!--
* Automatically closes linked issue when PR is merged.
* If you PR is not fully resolved issue, please use `part of #<issue number>` instead.

Usage: `fixes/resolves #<issue number>`, or `fixes/resolves (paste link of issue)`.
-->
fixes #

#### Other special notes for reviewer:
<!--
* Somethings that need extra attention for reviewer
* Some additional notes, TODO list, etc.
-->


codecov-commenter: # [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1477?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) Report
> Merging [#1477](https://codecov.io/gh/go-kratos/kratos/pull/1477?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (3be01e9) into [main](https://codecov.io/gh/go-kratos/kratos/commit/24ec23f0e41676bfe9726ce612005ac5ab85d67c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (24ec23f) will **increase** coverage by `0.06%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/go-kratos/kratos/pull/1477/graphs/tree.svg?width=650&height=150&src=pr&token=XwnmBTz78x&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)](https://codecov.io/gh/go-kratos/kratos/pull/1477?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)

```diff
@@            Coverage Diff             @@
##             main    #1477      +/-   ##
==========================================
+ Coverage   66.65%   66.72%   +0.06%     
==========================================
  Files          66       66              
  Lines        3038     3038              
==========================================
+ Hits         2025     2027       +2     
+ Misses        841      839       -2     
  Partials      172      172              
```


| [Impacted Files](https://codecov.io/gh/go-kratos/kratos/pull/1477?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) | Coverage Δ | |
|---|---|---|
| [internal/context/context.go](https://codecov.io/gh/go-kratos/kratos/pull/1477/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-aW50ZXJuYWwvY29udGV4dC9jb250ZXh0Lmdv) | `100.00% <0.00%> (+2.98%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1477?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1477?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Last update [24ec23f...3be01e9](https://codecov.io/gh/go-kratos/kratos/pull/1477?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).


Patch:
diff --git a/internal/host/host.go b/internal/host/host.go
index 3f1418aa..ac0cf212 100644
--- a/internal/host/host.go
+++ b/internal/host/host.go
@@ -36,7 +36,7 @@ func Port(lis net.Listener) (int, bool) {
 // Extract returns a private addr and port.
 func Extract(hostPort string, lis net.Listener) (string, error) {
 	addr, port, err := net.SplitHostPort(hostPort)
-	if err != nil {
+	if err != nil && lis == nil {
 		return "", err
 	}
 	if lis != nil {


Test patch:
diff --git a/internal/host/host_test.go b/internal/host/host_test.go
index f8f63629..102be651 100644
--- a/internal/host/host_test.go
+++ b/internal/host/host_test.go
@@ -76,6 +76,13 @@ func TestExtract(t *testing.T) {
 			}
 		})
 	}
+	lis, err := net.Listen("tcp", ":12345")
+	assert.NoError(t, err)
+	res, err := Extract("", lis)
+	assert.NoError(t, err)
+	expect, err := Extract(lis.Addr().String(), nil)
+	assert.NoError(t, err)
+	assert.Equal(t, expect, res)
 }
 
 func TestExtract2(t *testing.T) {
diff --git a/transport/http/server_test.go b/transport/http/server_test.go
index 4e102622..69a2ceb0 100644
--- a/transport/http/server_test.go
+++ b/transport/http/server_test.go
@@ -96,8 +96,8 @@ func testClient(t *testing.T, srv *Server) {
 	defer client.Close()
 	for _, test := range tests {
 		var res testData
-		url := fmt.Sprintf(e.String() + test.path)
-		req, err := http.NewRequest(test.method, url, nil)
+		reqURL := fmt.Sprintf(e.String() + test.path)
+		req, err := http.NewRequest(test.method, reqURL, nil)
 		if err != nil {
 			t.Fatal(err)
 		}
@@ -110,11 +110,12 @@ func testClient(t *testing.T, srv *Server) {
 		if err != nil {
 			t.Fatal(err)
 		}
-		defer resp.Body.Close()
 		if resp.StatusCode != 200 {
+			_ = resp.Body.Close()
 			t.Fatalf("http status got %d", resp.StatusCode)
 		}
 		content, err := ioutil.ReadAll(resp.Body)
+		_ = resp.Body.Close()
 		if err != nil {
 			t.Fatalf("read resp error %v", err)
 		}