File: sidekiq_metrics_test.go

package info (click to toggle)
golang-gitlab-gitlab-org-api-client-go 0.123.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,356 kB
  • sloc: makefile: 17
file content (117 lines) | stat: -rw-r--r-- 3,523 bytes parent folder | download | duplicates (3)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package gitlab

import (
	"fmt"
	"net/http"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestSidekiqService_GetQueueMetrics(t *testing.T) {
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/sidekiq/queue_metrics", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprintf(w, `{"queues": {"default": {"backlog": 0,"latency": 0}}}`)
	})

	qm, _, err := client.Sidekiq.GetQueueMetrics()
	require.NoError(t, err)

	want := &QueueMetrics{Queues: map[string]struct {
		Backlog int `json:"backlog"`
		Latency int `json:"latency"`
	}{"default": {Backlog: 0, Latency: 0}}}
	require.Equal(t, want, qm)
}

func TestSidekiqService_GetProcessMetrics(t *testing.T) {
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/sidekiq/process_metrics", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprint(w, `{"processes": [{"hostname": "gitlab.example.com","pid": 5649,"tag": "gitlab","concurrency": 25,"busy": 0}]}`)
	})

	pm, _, err := client.Sidekiq.GetProcessMetrics()
	require.NoError(t, err)

	want := &ProcessMetrics{[]struct {
		Hostname    string     `json:"hostname"`
		Pid         int        `json:"pid"`
		Tag         string     `json:"tag"`
		StartedAt   *time.Time `json:"started_at"`
		Queues      []string   `json:"queues"`
		Labels      []string   `json:"labels"`
		Concurrency int        `json:"concurrency"`
		Busy        int        `json:"busy"`
	}{{Hostname: "gitlab.example.com", Pid: 5649, Tag: "gitlab", Concurrency: 25, Busy: 0}}}
	require.Equal(t, want, pm)
}

func TestSidekiqService_GetJobStats(t *testing.T) {
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/sidekiq/job_stats", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprint(w, `{"jobs": {"processed": 2,"failed": 0,"enqueued": 0}}`)
	})

	js, _, err := client.Sidekiq.GetJobStats()
	require.NoError(t, err)

	want := &JobStats{struct {
		Processed int `json:"processed"`
		Failed    int `json:"failed"`
		Enqueued  int `json:"enqueued"`
	}(struct {
		Processed int
		Failed    int
		Enqueued  int
	}{Processed: 2, Failed: 0, Enqueued: 0})}
	require.Equal(t, want, js)
}

func TestSidekiqService_GetCompoundMetrics(t *testing.T) {
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/sidekiq/compound_metrics", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		mustWriteHTTPResponse(t, w, "testdata/get_compound_metrics.json")
	})

	cm, _, err := client.Sidekiq.GetCompoundMetrics()
	require.NoError(t, err)

	want := &CompoundMetrics{
		QueueMetrics: QueueMetrics{Queues: map[string]struct {
			Backlog int `json:"backlog"`
			Latency int `json:"latency"`
		}{"default": {
			Backlog: 0,
			Latency: 0,
		}}},
		ProcessMetrics: ProcessMetrics{Processes: []struct {
			Hostname    string     `json:"hostname"`
			Pid         int        `json:"pid"`
			Tag         string     `json:"tag"`
			StartedAt   *time.Time `json:"started_at"`
			Queues      []string   `json:"queues"`
			Labels      []string   `json:"labels"`
			Concurrency int        `json:"concurrency"`
			Busy        int        `json:"busy"`
		}{{Hostname: "gitlab.example.com", Pid: 5649, Tag: "gitlab", Concurrency: 25, Busy: 0}}},
		JobStats: JobStats{Jobs: struct {
			Processed int `json:"processed"`
			Failed    int `json:"failed"`
			Enqueued  int `json:"enqueued"`
		}(struct {
			Processed int
			Failed    int
			Enqueued  int
		}{Processed: 2, Failed: 0, Enqueued: 0})},
	}
	require.Equal(t, want, cm)
}