github.com/nilium/gitlab-runner@v12.5.0+incompatible/helpers/prometheus/log_hook_test.go (about) 1 package prometheus 2 3 import ( 4 "testing" 5 6 "github.com/prometheus/client_golang/prometheus" 7 "github.com/sirupsen/logrus" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func callFireConcurrent(lh *LogHook, repeats int, finish chan bool) { 12 for i := 0; i < repeats; i++ { 13 lh.Fire(&logrus.Entry{ 14 Level: logrus.ErrorLevel, 15 }) 16 finish <- true 17 } 18 } 19 20 func TestConcurrentFireCall(t *testing.T) { 21 lh := NewLogHook() 22 finish := make(chan bool) 23 24 times := 5 25 repeats := 100 26 total := times * repeats 27 28 for i := 0; i < times; i++ { 29 go callFireConcurrent(&lh, repeats, finish) 30 } 31 32 finished := 0 33 for { 34 if finished >= total { 35 break 36 } 37 38 <-finish 39 finished++ 40 } 41 42 assert.Equal(t, int64(total), *lh.errorsNumber[logrus.ErrorLevel], "Should fire log_hook N times") 43 } 44 45 func callCollectConcurrent(lh *LogHook, repeats int, ch chan<- prometheus.Metric, finish chan bool) { 46 for i := 0; i < repeats; i++ { 47 lh.Collect(ch) 48 finish <- true 49 } 50 } 51 52 func TestCouncurrentFireCallWithCollect(t *testing.T) { 53 lh := NewLogHook() 54 finish := make(chan bool) 55 ch := make(chan prometheus.Metric) 56 57 times := 5 58 repeats := 100 59 total := times * repeats * 2 60 61 go func() { 62 for { 63 <-ch 64 } 65 }() 66 67 for i := 0; i < times; i++ { 68 go callFireConcurrent(&lh, repeats, finish) 69 go callCollectConcurrent(&lh, repeats, ch, finish) 70 } 71 72 finished := 0 73 for { 74 if finished >= total { 75 break 76 } 77 78 <-finish 79 finished++ 80 } 81 82 assert.Equal(t, int64(total/2), *lh.errorsNumber[logrus.ErrorLevel], "Should fire log_hook N times") 83 }