Skip to content
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

Fix TestTelegramConfirmedRequest #215

Merged
merged 4 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions provider/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,37 @@ func TestTelegramUnconfirmedRequest(t *testing.T) {

func TestTelegramConfirmedRequest(t *testing.T) {
var servedToken string
var mu sync.Mutex

// is set when token becomes used,
// no sync is required because only a single goroutine in TelegramHandler.Run() reads and writes it
var tokenAlreadyUsed bool

var wgToken sync.WaitGroup
wgToken.Add(1)
defer func() {
if t.Failed() && servedToken == "" {
wgToken.Done() // for the case when test fails before token is generated
}
}()

m := &TelegramAPIMock{
GetUpdatesFunc: func(ctx context.Context) (*telegramUpdate, error) {
var upd telegramUpdate
wgToken.Wait()

mu.Lock()
defer mu.Unlock()
if servedToken != "" {
resp := fmt.Sprintf(getUpdatesResp, servedToken)
if tokenAlreadyUsed || t.Failed() {
return nil, fmt.Errorf("token %s has been already used", servedToken)
}

err := json.Unmarshal([]byte(resp), &upd)
if err != nil {
t.Fatal(err)
}
var upd telegramUpdate
resp := fmt.Sprintf(getUpdatesResp, servedToken)
err := json.Unmarshal([]byte(resp), &upd)
if err != nil {
t.Fatal(err)
}

// token is served only once
tokenAlreadyUsed = true

return &upd, nil
},
AvatarFunc: func(ctx context.Context, userID int) (string, error) {
Expand Down Expand Up @@ -147,10 +162,10 @@ func TestTelegramConfirmedRequest(t *testing.T) {
err := json.Unmarshal(w.Body.Bytes(), &resp)
assert.NoError(t, err)
assert.Equal(t, "my_auth_bot", resp.Bot)
assert.NotEmpty(t, resp.Token)

mu.Lock()
servedToken = resp.Token
mu.Unlock()
wgToken.Done()

// Check the token confirmation
assert.Eventually(t, func() bool {
Expand Down
38 changes: 26 additions & 12 deletions v2/provider/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,36 @@ func TestTelegramUnconfirmedRequest(t *testing.T) {

func TestTelegramConfirmedRequest(t *testing.T) {
var servedToken string
var mu sync.Mutex
// is set when token becomes used,
// no sync is required because only a single goroutine in TelegramHandler.Run() reads and writes it
var tokenAlreadyUsed bool

var wgToken sync.WaitGroup
wgToken.Add(1)
defer func() {
if t.Failed() && servedToken == "" {
wgToken.Done() // for the case when test fails before token is generated
}
}()

m := &TelegramAPIMock{
GetUpdatesFunc: func(ctx context.Context) (*telegramUpdate, error) {
var upd telegramUpdate
wgToken.Wait()

mu.Lock()
defer mu.Unlock()
if servedToken != "" {
resp := fmt.Sprintf(getUpdatesResp, servedToken)
if tokenAlreadyUsed || t.Failed() {
return nil, fmt.Errorf("token %s has been already used", servedToken)
}

err := json.Unmarshal([]byte(resp), &upd)
if err != nil {
t.Fatal(err)
}
var upd telegramUpdate
resp := fmt.Sprintf(getUpdatesResp, servedToken)
err := json.Unmarshal([]byte(resp), &upd)
if err != nil {
t.Fatal(err)
}

// token is served only once
tokenAlreadyUsed = true

return &upd, nil
},
AvatarFunc: func(ctx context.Context, userID int) (string, error) {
Expand Down Expand Up @@ -147,10 +161,10 @@ func TestTelegramConfirmedRequest(t *testing.T) {
err := json.Unmarshal(w.Body.Bytes(), &resp)
assert.NoError(t, err)
assert.Equal(t, "my_auth_bot", resp.Bot)
assert.NotEmpty(t, resp.Token)

mu.Lock()
servedToken = resp.Token
mu.Unlock()
wgToken.Done()

// Check the token confirmation
assert.Eventually(t, func() bool {
Expand Down
Loading