-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathctx_imap_test.go
61 lines (52 loc) · 1.72 KB
/
ctx_imap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) 2025 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package tests
import (
"fmt"
"time"
"github.com/ProtonMail/proton-bridge/v3/internal/constants"
"github.com/emersion/go-imap/client"
)
func (t *testCtx) newIMAPClient(userID, clientID string) error {
return t.newIMAPClientOnPort(userID, clientID, t.bridge.GetIMAPPort())
}
func (t *testCtx) newIMAPClientOnPort(userID, clientID string, imapPort int) error {
cli, err := eventuallyDial(fmt.Sprintf("%v:%d", constants.Host, imapPort))
if err != nil {
return err
}
t.imapClients[clientID] = &imapClient{
userID: userID,
client: cli,
}
return nil
}
func (t *testCtx) getIMAPClient(clientID string) (string, *client.Client) {
return t.imapClients[clientID].userID, t.imapClients[clientID].client
}
func eventuallyDial(addr string) (cli *client.Client, err error) {
var sleep = 1 * time.Second
for i := 0; i < 5; i++ {
cli, err := client.Dial(addr)
if err == nil {
return cli, nil
}
time.Sleep(sleep)
sleep *= 2
}
return nil, fmt.Errorf("after 5 attempts, last error: %s", err)
}