Skip to content

Add CloseRead and closeError test #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2019
Merged

Add CloseRead and closeError test #99

merged 3 commits into from
Jun 23, 2019

Conversation

nhooyr
Copy link
Contributor

@nhooyr nhooyr commented Jun 23, 2019

@scsmithr so I added a test for the issue you reported to me.

It passes. Does your code look any different?

@codecov
Copy link

codecov bot commented Jun 23, 2019

Codecov Report

Merging #99 into master will decrease coverage by 0.62%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #99      +/-   ##
==========================================
- Coverage   76.48%   75.86%   -0.63%     
==========================================
  Files          13       13              
  Lines         978      986       +8     
==========================================
  Hits          748      748              
- Misses        176      184       +8     
  Partials       54       54
Impacted Files Coverage Δ
websocket.go 70.72% <0%> (-1.24%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3149225...6eda9c5. Read the comment docs.

@nhooyr nhooyr merged commit 176b144 into master Jun 23, 2019
@nhooyr nhooyr deleted the closeread branch June 23, 2019 04:20
@nhooyr
Copy link
Contributor Author

nhooyr commented Jun 23, 2019

Whoops meant to notify you. @scsmithr

@scsmithr
Copy link

scsmithr commented Jun 23, 2019

So it turns out I was just misplacing a defer cancel().

If I change your "closeError" test to the following, I get the EOF.

server: func(w http.ResponseWriter, r *http.Request) error {
	c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
	if err != nil {
		return err
	}
	defer c.Close(websocket.StatusInternalError, "")

	ctx, cancel := context.WithCancel(r.Context())
	defer cancel()

	go func() {
		defer cancel()
		c.Reader(ctx)
		c.Close(websocket.StatusPolicyViolation, "")
	}()

	err = wsjson.Write(ctx, c, "hello")
	if err != nil {
		return err
	}

	return nil
},

Removing the extra defer cancel() fixes it.

@nhooyr
Copy link
Contributor Author

nhooyr commented Jun 24, 2019

Makes sense, you're closing the connection early with the defer before the close frame can be written.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants