github.com/thanos-io/thanos@v0.32.5/internal/cortex/util/merger_test.go (about) 1 // Copyright (c) The Cortex Authors. 2 // Licensed under the Apache License 2.0. 3 4 package util 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/prometheus/common/model" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestMergeSampleSets(t *testing.T) { 15 now := model.Now() 16 sample1 := model.SamplePair{Timestamp: now, Value: 1} 17 sample2 := model.SamplePair{Timestamp: now.Add(1 * time.Second), Value: 2} 18 sample3 := model.SamplePair{Timestamp: now.Add(4 * time.Second), Value: 3} 19 sample4 := model.SamplePair{Timestamp: now.Add(8 * time.Second), Value: 7} 20 21 for _, c := range []struct { 22 samplesA []model.SamplePair 23 samplesB []model.SamplePair 24 expected []model.SamplePair 25 }{ 26 { 27 samplesA: []model.SamplePair{}, 28 samplesB: []model.SamplePair{}, 29 expected: []model.SamplePair{}, 30 }, 31 { 32 samplesA: []model.SamplePair{sample1}, 33 samplesB: []model.SamplePair{}, 34 expected: []model.SamplePair{sample1}, 35 }, 36 { 37 samplesA: []model.SamplePair{}, 38 samplesB: []model.SamplePair{sample1}, 39 expected: []model.SamplePair{sample1}, 40 }, 41 { 42 samplesA: []model.SamplePair{sample1}, 43 samplesB: []model.SamplePair{sample1}, 44 expected: []model.SamplePair{sample1}, 45 }, 46 { 47 samplesA: []model.SamplePair{sample1, sample2, sample3}, 48 samplesB: []model.SamplePair{sample1, sample3, sample4}, 49 expected: []model.SamplePair{sample1, sample2, sample3, sample4}, 50 }, 51 } { 52 samples := MergeSampleSets(c.samplesA, c.samplesB) 53 require.Equal(t, c.expected, samples) 54 } 55 } 56 57 func TestMergeNSampleSets(t *testing.T) { 58 now := model.Now() 59 sample1 := model.SamplePair{Timestamp: now, Value: 1} 60 sample2 := model.SamplePair{Timestamp: now.Add(1 * time.Second), Value: 2} 61 sample3 := model.SamplePair{Timestamp: now.Add(4 * time.Second), Value: 3} 62 sample4 := model.SamplePair{Timestamp: now.Add(8 * time.Second), Value: 7} 63 64 for _, c := range []struct { 65 sampleSets [][]model.SamplePair 66 expected []model.SamplePair 67 }{ 68 { 69 sampleSets: [][]model.SamplePair{{}, {}, {}}, 70 expected: []model.SamplePair{}, 71 }, 72 { 73 sampleSets: [][]model.SamplePair{ 74 {sample1, sample2}, 75 {sample2}, 76 {sample1, sample3, sample4}, 77 }, 78 expected: []model.SamplePair{sample1, sample2, sample3, sample4}, 79 }, 80 } { 81 samples := MergeNSampleSets(c.sampleSets...) 82 require.Equal(t, c.expected, samples) 83 } 84 }