Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab_poll_test.go « gitlab « source « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9a5590154da42ff6a2d33354692f2d1366473728 (plain)
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
package gitlab

import (
	"fmt"
	"sync"
	"testing"
	"time"

	"github.com/sirupsen/logrus/hooks/test"
	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/client"
)

func TestClient_Poll(t *testing.T) {
	hook := test.NewGlobal()
	tests := []struct {
		name         string
		retries      int
		maxTime      time.Duration
		expectedFail bool
	}{
		{
			name:         "success_with_no_retry",
			retries:      0,
			maxTime:      10 * time.Millisecond,
			expectedFail: false,
		},
		{
			name:         "success_after_N_retries",
			retries:      3,
			maxTime:      30 * time.Millisecond,
			expectedFail: false,
		},
		{
			name:         "fail_with_no_retries",
			retries:      0,
			maxTime:      10 * time.Millisecond,
			expectedFail: true,
		},
		{
			name:         "fail_after_N_retries",
			retries:      3,
			maxTime:      30 * time.Millisecond,
			expectedFail: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			defer hook.Reset()
			var counter int
			client := client.StubClient{StatusErr: func() error {
				if tt.expectedFail {
					return fmt.Errorf(client.ConnectionErrorMsg)
				}

				if counter < tt.retries {
					counter++
					return fmt.Errorf(client.ConnectionErrorMsg)
				}

				return nil
			}}

			glClient := Gitlab{client: client, mu: &sync.RWMutex{}}

			glClient.poll(3*time.Millisecond, tt.maxTime)
			if tt.expectedFail {
				require.False(t, glClient.isReady)

				s := fmt.Sprintf("\"Failed to connect to the internal GitLab API after %.2fs", tt.maxTime.Seconds())
				require.Equal(t, s, hook.LastEntry().Message)
				return
			}

			require.True(t, glClient.isReady)
			require.Equal(t, "GitLab internal pages status API connected successfully", hook.LastEntry().Message)
		})
	}
}