github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/block/meta_test.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package block 22 23 import ( 24 "encoding/json" 25 "testing" 26 "time" 27 28 "github.com/m3db/m3/src/query/models" 29 30 "github.com/stretchr/testify/assert" 31 "github.com/stretchr/testify/require" 32 ) 33 34 func TestMeta(t *testing.T) { 35 bounds, otherBounds := models.Bounds{}, models.Bounds{} 36 badBounds := models.Bounds{Duration: 100} 37 38 assert.True(t, bounds.Equals(otherBounds)) 39 assert.False(t, bounds.Equals(badBounds)) 40 } 41 42 func TestResultMeta(t *testing.T) { 43 r := NewResultMetadata() 44 assert.True(t, r.Exhaustive) 45 assert.True(t, r.LocalOnly) 46 assert.Equal(t, 0, len(r.Warnings)) 47 assert.True(t, r.IsDefault()) 48 49 r.AddWarning("foo", "bar") 50 assert.Equal(t, 1, len(r.Warnings)) 51 assert.Equal(t, "foo", r.Warnings[0].Name) 52 assert.Equal(t, "bar", r.Warnings[0].Message) 53 assert.False(t, r.IsDefault()) 54 55 rTwo := ResultMetadata{ 56 LocalOnly: false, 57 Exhaustive: false, 58 } 59 60 assert.False(t, rTwo.IsDefault()) 61 rTwo.AddWarning("baz", "qux") 62 merge := r.CombineMetadata(rTwo) 63 assert.Equal(t, 1, len(r.Warnings)) 64 assert.Equal(t, 1, len(rTwo.Warnings)) 65 66 assert.False(t, merge.Exhaustive) 67 assert.False(t, merge.LocalOnly) 68 require.Equal(t, 2, len(merge.Warnings)) 69 assert.Equal(t, "foo_bar", merge.Warnings[0].Header()) 70 assert.Equal(t, "baz_qux", merge.Warnings[1].Header()) 71 72 // ensure warnings are deduplicated 73 merge = merge.CombineMetadata(rTwo) 74 require.Equal(t, 2, len(merge.Warnings)) 75 76 assert.Equal(t, "foo_bar", merge.Warnings[0].Header()) 77 assert.Equal(t, "baz_qux", merge.Warnings[1].Header()) 78 79 merge.AddWarning("foo", "bar") 80 require.Equal(t, 2, len(merge.Warnings)) 81 82 assert.Equal(t, "foo_bar", merge.Warnings[0].Header()) 83 assert.Equal(t, "baz_qux", merge.Warnings[1].Header()) 84 } 85 86 func TestMergeEmptyWarnings(t *testing.T) { 87 r := NewResultMetadata() 88 r.AddWarning("foo", "bar") 89 rTwo := NewResultMetadata() 90 merge := r.CombineMetadata(rTwo) 91 assert.Equal(t, 1, len(r.Warnings)) 92 assert.Equal(t, 0, len(rTwo.Warnings)) 93 require.Equal(t, 1, len(merge.Warnings)) 94 assert.Equal(t, "foo_bar", merge.Warnings[0].Header()) 95 } 96 97 func TestMergeResultMetricMetadata(t *testing.T) { 98 r1 := ResultMetricMetadata{ 99 WithSamples: 100, 100 NoSamples: 20, 101 Aggregated: 10, 102 } 103 r2 := ResultMetricMetadata{ 104 WithSamples: 1, 105 Aggregated: 2, 106 Unaggregated: 3, 107 } 108 r1.Merge(r2) 109 assert.Equal(t, 101, r1.WithSamples) 110 assert.Equal(t, 20, r1.NoSamples) 111 assert.Equal(t, 12, r1.Aggregated) 112 assert.Equal(t, 3, r1.Unaggregated) 113 } 114 115 func TestCombineMetadataWithMetricMetadataMaps(t *testing.T) { 116 r1 := NewResultMetadata() 117 r2 := NewResultMetadata() 118 a := []byte("a") 119 b := []byte("b") 120 c := []byte("c") 121 noname := []byte{} 122 123 r1.ByName(a).WithSamples = 5 124 r1.ByName(c).Aggregated = 1 125 r1.ByName(noname).Unaggregated = 19 126 127 r2.ByName(nil).Aggregated = 99 128 r2.ByName(a).WithSamples = 6 129 r2.ByName(a).NoSamples = 1 130 r2.ByName(b).NoSamples = 1 131 r2.ByName(b).Unaggregated = 15 132 r2.ByName(c).Aggregated = 2 133 134 r := r1.CombineMetadata(r2) 135 assert.Equal(t, &ResultMetricMetadata{ 136 NoSamples: 1, 137 WithSamples: 11, 138 }, r.ByName(a)) 139 assert.Equal(t, &ResultMetricMetadata{ 140 NoSamples: 1, 141 Unaggregated: 15, 142 }, r.ByName(b)) 143 assert.Equal(t, &ResultMetricMetadata{ 144 Aggregated: 3, 145 }, r.ByName(c)) 146 assert.Equal(t, &ResultMetricMetadata{ 147 Unaggregated: 19, 148 Aggregated: 99, 149 }, r.ByName(nil)) 150 151 // Sanity check that accessing by nil or by an empty name 152 // yields the same result. 153 assert.Equal(t, r.ByName(nil), r.ByName(noname)) 154 155 // And finally it should marshal to JSON without error. 156 js, err := json.Marshal(r) 157 assert.Nil(t, err) 158 assert.NotEmpty(t, string(js)) 159 } 160 161 func TestMergeIntoEmptyWarnings(t *testing.T) { 162 r := NewResultMetadata() 163 rTwo := NewResultMetadata() 164 rTwo.AddWarning("foo", "bar") 165 merge := r.CombineMetadata(rTwo) 166 assert.Equal(t, 0, len(r.Warnings)) 167 assert.Equal(t, 1, len(rTwo.Warnings)) 168 require.Equal(t, 1, len(merge.Warnings)) 169 assert.Equal(t, "foo_bar", merge.Warnings[0].Header()) 170 } 171 172 func TestMergeResolutions(t *testing.T) { 173 expected := []time.Duration{1, 2, 3} 174 r := ResultMetadata{} 175 rTwo := ResultMetadata{} 176 merge := r.CombineMetadata(rTwo) 177 assert.Nil(t, r.Resolutions) 178 assert.Nil(t, rTwo.Resolutions) 179 assert.Nil(t, merge.Resolutions) 180 181 rTwo.Resolutions = expected 182 merge = r.CombineMetadata(rTwo) 183 assert.Nil(t, r.Resolutions) 184 assert.Equal(t, 3, len(rTwo.Resolutions)) 185 require.Equal(t, 3, len(merge.Resolutions)) 186 assert.Equal(t, expected, merge.Resolutions) 187 188 r = ResultMetadata{Resolutions: expected} 189 rTwo = ResultMetadata{} 190 merge = r.CombineMetadata(rTwo) 191 assert.Equal(t, 3, len(r.Resolutions)) 192 assert.Nil(t, rTwo.Resolutions) 193 require.Equal(t, 3, len(merge.Resolutions)) 194 assert.Equal(t, expected, merge.Resolutions) 195 196 rTwo = ResultMetadata{Resolutions: []time.Duration{4, 5, 6}} 197 merge = r.CombineMetadata(rTwo) 198 assert.Equal(t, 3, len(r.Resolutions)) 199 assert.Equal(t, 3, len(rTwo.Resolutions)) 200 require.Equal(t, 6, len(merge.Resolutions)) 201 assert.Equal(t, []time.Duration{1, 2, 3, 4, 5, 6}, merge.Resolutions) 202 } 203 204 func TestVerifyTemporalRange(t *testing.T) { 205 r := ResultMetadata{ 206 Exhaustive: true, 207 Resolutions: []time.Duration{5, 10}, 208 } 209 210 ex0 := "resolution larger than query range_range: 1ns, resolutions: 10ns, 5ns" 211 ex1 := "resolution larger than query range_range: 6ns, resolutions: 10ns" 212 213 r.VerifyTemporalRange(11) 214 assert.Equal(t, 0, len(r.WarningStrings())) 215 216 r.VerifyTemporalRange(1) 217 require.Equal(t, 1, len(r.WarningStrings())) 218 assert.Equal(t, ex0, r.WarningStrings()[0]) 219 220 r.VerifyTemporalRange(6) 221 require.Equal(t, 2, len(r.WarningStrings())) 222 assert.Equal(t, ex0, r.WarningStrings()[0]) 223 assert.Equal(t, ex1, r.WarningStrings()[1]) 224 225 r.VerifyTemporalRange(11) 226 require.Equal(t, 2, len(r.WarningStrings())) 227 assert.Equal(t, ex0, r.WarningStrings()[0]) 228 assert.Equal(t, ex1, r.WarningStrings()[1]) 229 }