File: log_hook_test.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (86 lines) | stat: -rw-r--r-- 1,555 bytes parent folder | download
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
//go:build !integration
// +build !integration

package prometheus

import (
	"testing"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
)

func callFireConcurrent(lh *LogHook, repeats int, finish chan bool) {
	for i := 0; i < repeats; i++ {
		_ = lh.Fire(&logrus.Entry{
			Level: logrus.ErrorLevel,
		})
		finish <- true
	}
}

func TestConcurrentFireCall(t *testing.T) {
	lh := NewLogHook()
	finish := make(chan bool)

	times := 5
	repeats := 100
	total := times * repeats

	for i := 0; i < times; i++ {
		go callFireConcurrent(&lh, repeats, finish)
	}

	finished := 0
	for {
		if finished >= total {
			break
		}

		<-finish
		finished++
	}

	assert.Equal(t, int64(total), *lh.errorsNumber[logrus.ErrorLevel], "Should fire log_hook N times")
}

func callCollectConcurrent(lh *LogHook, repeats int, ch chan<- prometheus.Metric, finish chan bool) {
	for i := 0; i < repeats; i++ {
		lh.Collect(ch)
		finish <- true
	}
}

func TestCouncurrentFireCallWithCollect(t *testing.T) {
	lh := NewLogHook()
	finish := make(chan bool)
	ch := make(chan prometheus.Metric)

	times := 5
	repeats := 100
	total := times * repeats * 2

	go func() {
		for {
			<-ch
		}
	}()

	for i := 0; i < times; i++ {
		go callFireConcurrent(&lh, repeats, finish)
		go callCollectConcurrent(&lh, repeats, ch, finish)
	}

	finished := 0
	for {
		if finished >= total {
			break
		}

		<-finish
		finished++
	}

	assert.Equal(t, int64(total/2), *lh.errorsNumber[logrus.ErrorLevel], "Should fire log_hook N times")
}