-
Notifications
You must be signed in to change notification settings - Fork 334
imapclient: implement Client.Closed #721
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package imapclient_test | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/emersion/go-imap/v2" | ||
| ) | ||
|
|
||
| // TestClient_Closed tests that the Closed() channel is closed when the | ||
| // connection is explicitly closed via Close(). | ||
| func TestClient_Closed(t *testing.T) { | ||
| client, server := newClientServerPair(t, imap.ConnStateAuthenticated) | ||
| defer server.Close() | ||
|
|
||
| closedCh := client.Closed() | ||
| if closedCh == nil { | ||
| t.Fatal("Closed() returned nil channel") | ||
| } | ||
|
|
||
| select { | ||
| case <-closedCh: | ||
| t.Fatal("Closed() channel closed before calling Close()") | ||
| default: // Expected | ||
| } | ||
|
|
||
| if err := client.Close(); err != nil { | ||
| t.Fatalf("Close() = %v", err) | ||
| } | ||
|
|
||
| select { | ||
| case <-closedCh: | ||
| t.Log("Closed() channel properly closed after Close()") | ||
| case <-time.After(2 * time.Second): | ||
| t.Fatal("Closed() channel not closed after Close()") | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -378,3 +378,34 @@ func ExampleClient_Authenticate_oauth() { | |
| log.Fatalf("authentication failed: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func ExampleClient_Closed() { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example is racy (the main function doesn't typically wait for other goroutines to finish) and doesn't reflect real-world usage. Could we drop this example for now, and wait for NOTIFY to be implemented to add a Notify example which leverages Closed to figure out when the connection has been closed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example redone.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The example still feels a bit artificial - but let's revisit this with NOTIFY. |
||
| c, err := imapclient.DialTLS("mail.example.org:993", nil) | ||
| if err != nil { | ||
| log.Fatalf("failed to dial IMAP server: %v", err) | ||
| } | ||
|
|
||
| selected := false | ||
|
|
||
| go func(c *imapclient.Client) { | ||
| if err := c.Login("root", "asdf").Wait(); err != nil { | ||
| log.Fatalf("failed to login: %v", err) | ||
| } | ||
|
|
||
| if _, err := c.Select("INBOX", nil).Wait(); err != nil { | ||
| log.Fatalf("failed to select INBOX: %v", err) | ||
| } | ||
|
|
||
| selected = true | ||
|
|
||
| c.Close() | ||
| }(c) | ||
|
|
||
| // This channel shall be closed when the connection is closed. | ||
| <-c.Closed() | ||
| log.Println("Connection has been closed") | ||
|
|
||
| if !selected { | ||
| log.Fatalf("Connection was closed before selecting mailbox") | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.