(t *testing.T)
| 52 | } |
| 53 | |
| 54 | func TestSampleBufferConcurrently(t *testing.T) { |
| 55 | t.Parallel() |
| 56 | |
| 57 | seed := time.Now().UnixNano() |
| 58 | r := rand.New(rand.NewSource(seed)) |
| 59 | t.Logf("Random source seeded with %d\n", seed) |
| 60 | |
| 61 | registry := metrics.NewRegistry() |
| 62 | metric, err := registry.NewMetric("my_metric", metrics.Gauge) |
| 63 | require.NoError(t, err) |
| 64 | |
| 65 | producersCount := 50 + r.Intn(50) |
| 66 | sampleCount := 10 + r.Intn(10) |
| 67 | sleepModifier := 10 + r.Intn(10) |
| 68 | buffer := SampleBuffer{} |
| 69 | |
| 70 | wg := make(chan struct{}) |
| 71 | fillBuffer := func() { |
| 72 | for i := range sampleCount { |
| 73 | buffer.AddMetricSamples([]metrics.SampleContainer{metrics.Sample{ |
| 74 | TimeSeries: metrics.TimeSeries{ |
| 75 | Metric: metric, |
| 76 | Tags: registry.RootTagSet().WithTagsFromMap(map[string]string{"tag1": "val1"}), |
| 77 | }, |
| 78 | Time: time.Unix(1562324644, 0), |
| 79 | Value: float64(i), |
| 80 | }}) |
| 81 | time.Sleep(time.Duration(i*sleepModifier) * time.Microsecond) |
| 82 | } |
| 83 | wg <- struct{}{} |
| 84 | } |
| 85 | for range producersCount { |
| 86 | go fillBuffer() |
| 87 | } |
| 88 | |
| 89 | timer := time.NewTicker(5 * time.Millisecond) |
| 90 | timeout := time.After(5 * time.Second) |
| 91 | defer timer.Stop() |
| 92 | readSamples := make([]metrics.SampleContainer, 0, sampleCount*producersCount) |
| 93 | finishedProducers := 0 |
| 94 | loop: |
| 95 | for { |
| 96 | select { |
| 97 | case <-timer.C: |
| 98 | readSamples = append(readSamples, buffer.GetBufferedSamples()...) |
| 99 | case <-wg: |
| 100 | finishedProducers++ |
| 101 | if finishedProducers == producersCount { |
| 102 | readSamples = append(readSamples, buffer.GetBufferedSamples()...) |
| 103 | break loop |
| 104 | } |
| 105 | case <-timeout: |
| 106 | t.Fatalf("test timed out") |
| 107 | } |
| 108 | } |
| 109 | assert.Equal(t, sampleCount*producersCount, len(readSamples)) |
| 110 | for _, s := range readSamples { |
| 111 | require.NotNil(t, s) |
nothing calls this directly
no test coverage detected
searching dependent graphs…