-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
suite.go
84 lines (73 loc) · 1.92 KB
/
suite.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package e2etest
import (
"context"
"crypto/rand"
"io"
"sync"
"github.com/cenkalti/backoff/v4"
"go.uber.org/zap"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/telegram/internal/tgtest"
)
// TestConfig contains some common test server settings.
type TestConfig struct {
AppID int
AppHash string
DcID int
}
// Suite is struct which contains external E2E test parameters.
type Suite struct {
tgtest.Suite
TestConfig
rand io.Reader
// already used phone numbers
used map[string]struct{}
usedMux sync.Mutex
}
// NewSuite creates new Suite.
func NewSuite(suite tgtest.Suite, config TestConfig, randomSource io.Reader) *Suite {
return &Suite{
Suite: suite,
TestConfig: config,
rand: randomSource,
used: map[string]struct{}{},
}
}
// Client creates new *telegram.Client using this suite.
func (s *Suite) Client(logger *zap.Logger, handler telegram.UpdateHandler) *telegram.Client {
return telegram.NewClient(s.AppID, s.AppHash, telegram.Options{
DC: s.DcID,
DCList: dcs.StagingDCs(),
Logger: logger,
UpdateHandler: handler,
})
}
// Authenticate authenticates client on test server.
func (s *Suite) Authenticate(ctx context.Context, client *telegram.Client) error {
var auth telegram.UserAuthenticator
for {
auth = telegram.TestAuth(rand.Reader, s.DcID)
phone, err := auth.Phone(ctx)
if err != nil {
return err
}
s.usedMux.Lock()
if _, ok := s.used[phone]; !ok {
s.used[phone] = struct{}{}
s.usedMux.Unlock()
break
}
s.usedMux.Unlock()
}
return telegram.NewAuth(
auth,
telegram.SendCodeOptions{},
).Run(ctx, client)
}
// RetryAuthenticate authenticates client on test server.
func (s *Suite) RetryAuthenticate(ctx context.Context, client *telegram.Client) error {
return backoff.Retry(func() error {
return s.Authenticate(ctx, client)
}, backoff.WithContext(backoff.NewExponentialBackOff(), ctx))
}