Skip to content

Commit

Permalink
Fix unselect test deadlock, move Client.Unselect to cmd_selected.go (…
Browse files Browse the repository at this point in the history
…squash me)
  • Loading branch information
foxcpp committed Aug 24, 2020
1 parent bd98973 commit be45b1e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *serverConn) WriteString(s string) (n int, err error) {
}

func newTestClient(t *testing.T) (c *Client, s *serverConn) {
return newTestClientWithGreeting(t, "* OK [CAPABILITY IMAP4rev1 STARTTLS AUTH=PLAIN] Server ready.\r\n")
return newTestClientWithGreeting(t, "* OK [CAPABILITY IMAP4rev1 STARTTLS AUTH=PLAIN UNSELECT] Server ready.\r\n")
}

func newTestClientWithGreeting(t *testing.T, greeting string) (c *Client, s *serverConn) {
Expand Down
1 change: 1 addition & 0 deletions client/cmd_any.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (c *Client) Support(cap string) (bool, error) {
c.locker.Lock()
supported := c.caps[cap]
c.locker.Unlock()

return supported, nil
}

Expand Down
27 changes: 0 additions & 27 deletions client/cmd_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,30 +252,3 @@ func (c *Client) Append(mbox string, flags []string, date time.Time, msg imap.Li
}
return status.Err()
}

// Unselect frees server's resources associated with the selected mailbox and
// returns the server to the authenticated state. This command performs the same
// actions as Close, except that no messages are permanently removed from the
// currently selected mailbox.
//
// If client does not support the UNSELECT extension, ErrExtensionUnsupported
// is returned.
func (c *Client) Unselect() error {
if ok, err := c.Support("UNSELECT"); !ok || err != nil {
return ErrExtensionUnsupported
}

if c.State() != imap.SelectedState {
return ErrNoMailboxSelected
}

cmd := &commands.Unselect{}
if status, err := c.Execute(cmd, nil); err != nil {
return err
} else if err := status.Err(); err != nil {
return err
}

c.SetState(imap.AuthenticatedState, nil)
return nil
}
27 changes: 27 additions & 0 deletions client/cmd_selected.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,30 @@ func (c *Client) Copy(seqset *imap.SeqSet, dest string) error {
func (c *Client) UidCopy(seqset *imap.SeqSet, dest string) error {
return c.copy(true, seqset, dest)
}

// Unselect frees server's resources associated with the selected mailbox and
// returns the server to the authenticated state. This command performs the same
// actions as Close, except that no messages are permanently removed from the
// currently selected mailbox.
//
// If client does not support the UNSELECT extension, ErrExtensionUnsupported
// is returned.
func (c *Client) Unselect() error {
if ok, err := c.Support("UNSELECT"); !ok || err != nil {
return ErrExtensionUnsupported
}

if c.State() != imap.SelectedState {
return ErrNoMailboxSelected
}

cmd := &commands.Unselect{}
if status, err := c.Execute(cmd, nil); err != nil {
return err
} else if err := status.Err(); err != nil {
return err
}

c.SetState(imap.AuthenticatedState, nil)
return nil
}

0 comments on commit be45b1e

Please sign in to comment.