Skip to content

Commit

Permalink
[fix #9] trying to send on closed connection
Browse files Browse the repository at this point in the history
  • Loading branch information
staskobzar committed Apr 10, 2024
1 parent e941cf0 commit b0baed7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func (c *Client) Err() <-chan error {
// network errors if any write away. May block
// until network timeout
func (c *Client) MustSend(msg []byte) error {
if c.conn == nil {
return fmt.Errorf("%w: closed connection: failed to send message", ErrConn)
}
if err := c.setWTimeout(); err != nil {
return fmt.Errorf("%w: failed to set net timeout: %q", ErrConn, err)
}
Expand Down
8 changes: 8 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ func TestClientWriteToConnection(t *testing.T) {
err := cl.MustSend([]byte("must send\n"))
assert.ErrorContains(t, err, "io: read/write on closed")
})

t.Run("MustSend returns error when client is closed", func(t *testing.T) {
conn, _ := net.Pipe()
client := makeClient(conn)
client.Close()
err := client.MustSend([]byte("must send\n"))
assert.ErrorContains(t, err, "closed connection")
})
}

func TestClientReadPartialNetworkInput(t *testing.T) {
Expand Down

0 comments on commit b0baed7

Please sign in to comment.