github.com/thanos-io/thanos@v0.32.5/pkg/store/storepb/inprocess_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package storepb 5 6 import ( 7 "context" 8 "fmt" 9 "io" 10 "testing" 11 12 "github.com/efficientgo/core/testutil" 13 "github.com/pkg/errors" 14 "github.com/thanos-io/thanos/pkg/store/labelpb" 15 ) 16 17 type testStoreServer struct { 18 info *InfoResponse 19 infoLastReq *InfoRequest 20 21 series []*SeriesResponse 22 seriesLastReq *SeriesRequest 23 24 labelNames *LabelNamesResponse 25 labelNamesLastReq *LabelNamesRequest 26 27 labelValues *LabelValuesResponse 28 labelValuesLastReq *LabelValuesRequest 29 30 err error 31 } 32 33 func (t *testStoreServer) Info(_ context.Context, r *InfoRequest) (*InfoResponse, error) { 34 t.infoLastReq = r 35 return t.info, t.err 36 } 37 38 func (t *testStoreServer) Series(r *SeriesRequest, server Store_SeriesServer) error { 39 t.seriesLastReq = r 40 for i, s := range t.series { 41 if t.err != nil && i == len(t.series)/2 { 42 return t.err 43 } 44 if err := server.Send(s); err != nil { 45 return err 46 } 47 } 48 return nil 49 } 50 51 func (t *testStoreServer) LabelNames(_ context.Context, r *LabelNamesRequest) (*LabelNamesResponse, error) { 52 t.labelNamesLastReq = r 53 return t.labelNames, t.err 54 } 55 56 func (t *testStoreServer) LabelValues(_ context.Context, r *LabelValuesRequest) (*LabelValuesResponse, error) { 57 t.labelValuesLastReq = r 58 return t.labelValues, t.err 59 } 60 61 func TestServerAsClient(t *testing.T) { 62 for _, bufferSize := range []int{0, 1, 20, 100} { 63 t.Run(fmt.Sprintf("buffer=%v", bufferSize), func(t *testing.T) { 64 t.Run("Info", func(t *testing.T) { 65 s := &testStoreServer{ 66 info: &InfoResponse{ 67 LabelSets: []labelpb.ZLabelSet{{Labels: []labelpb.ZLabel{{Name: "a", Value: "b"}}}}, 68 MinTime: -1, 69 MaxTime: 10, 70 StoreType: StoreType_DEBUG, 71 }} 72 t.Run("ok", func(t *testing.T) { 73 for i := 0; i < 20; i++ { 74 r := &InfoRequest{} 75 resp, err := ServerAsClient(s, 0).Info(context.TODO(), r) 76 testutil.Ok(t, err) 77 testutil.Equals(t, s.info, resp) 78 testutil.Equals(t, r, s.infoLastReq) 79 s.infoLastReq = nil 80 } 81 }) 82 t.Run("error", func(t *testing.T) { 83 s.err = errors.New("some error") 84 for i := 0; i < 20; i++ { 85 r := &InfoRequest{} 86 _, err := ServerAsClient(s, 0).Info(context.TODO(), r) 87 testutil.NotOk(t, err) 88 testutil.Equals(t, s.err, err) 89 } 90 }) 91 }) 92 t.Run("Series", func(t *testing.T) { 93 s := &testStoreServer{ 94 series: []*SeriesResponse{ 95 NewSeriesResponse(&Series{ 96 Labels: []labelpb.ZLabel{{Name: "a", Value: "b"}}, 97 Chunks: []AggrChunk{{MinTime: 123, MaxTime: 124}, {MinTime: 12455, MaxTime: 14124}}, 98 }), 99 NewSeriesResponse(&Series{ 100 Labels: []labelpb.ZLabel{{Name: "a", Value: "b1"}}, 101 Chunks: []AggrChunk{{MinTime: 1231, MaxTime: 124}, {MinTime: 12455, MaxTime: 14124}}, 102 }), 103 NewWarnSeriesResponse(errors.New("yolo")), 104 NewSeriesResponse(&Series{ 105 Labels: []labelpb.ZLabel{{Name: "a", Value: "b3"}}, 106 Chunks: []AggrChunk{{MinTime: 123, MaxTime: 124}, {MinTime: 124554, MaxTime: 14124}}, 107 }), 108 }} 109 t.Run("ok", func(t *testing.T) { 110 for i := 0; i < 20; i++ { 111 r := &SeriesRequest{ 112 MinTime: -214, 113 MaxTime: 213, 114 Matchers: []LabelMatcher{{Value: "wfsdfs", Name: "__name__", Type: LabelMatcher_EQ}}, 115 PartialResponseStrategy: PartialResponseStrategy_ABORT, 116 } 117 client, err := ServerAsClient(s, 0).Series(context.TODO(), r) 118 testutil.Ok(t, err) 119 var resps []*SeriesResponse 120 for { 121 resp, err := client.Recv() 122 if err == io.EOF { 123 break 124 } 125 testutil.Ok(t, err) 126 resps = append(resps, resp) 127 } 128 testutil.Equals(t, s.series, resps) 129 testutil.Equals(t, r, s.seriesLastReq) 130 s.seriesLastReq = nil 131 } 132 }) 133 t.Run("ok, close send", func(t *testing.T) { 134 s.err = errors.New("some error") 135 for i := 0; i < 20; i++ { 136 r := &SeriesRequest{ 137 MinTime: -214, 138 MaxTime: 213, 139 Matchers: []LabelMatcher{{Value: "wfsdfs", Name: "__name__", Type: LabelMatcher_EQ}}, 140 PartialResponseStrategy: PartialResponseStrategy_ABORT, 141 } 142 client, err := ServerAsClient(s, 0).Series(context.TODO(), r) 143 testutil.Ok(t, err) 144 var resps []*SeriesResponse 145 for { 146 if len(resps) == len(s.series)/2 { 147 testutil.Ok(t, client.CloseSend()) 148 break 149 } 150 resp, err := client.Recv() 151 if err == io.EOF { 152 break 153 } 154 testutil.Ok(t, err) 155 resps = append(resps, resp) 156 } 157 testutil.Equals(t, s.series[:len(s.series)/2], resps) 158 testutil.Equals(t, r, s.seriesLastReq) 159 s.seriesLastReq = nil 160 } 161 }) 162 t.Run("error", func(t *testing.T) { 163 for i := 0; i < 20; i++ { 164 r := &SeriesRequest{ 165 MinTime: -214, 166 MaxTime: 213, 167 Matchers: []LabelMatcher{{Value: "wfsdfs", Name: "__name__", Type: LabelMatcher_EQ}}, 168 PartialResponseStrategy: PartialResponseStrategy_ABORT, 169 } 170 client, err := ServerAsClient(s, 0).Series(context.TODO(), r) 171 testutil.Ok(t, err) 172 var resps []*SeriesResponse 173 for { 174 resp, err := client.Recv() 175 if err == io.EOF { 176 break 177 } 178 if err == s.err { 179 break 180 } 181 testutil.Ok(t, err) 182 resps = append(resps, resp) 183 } 184 testutil.Equals(t, s.series[:len(s.series)/2], resps) 185 testutil.Equals(t, r, s.seriesLastReq) 186 s.seriesLastReq = nil 187 } 188 }) 189 }) 190 t.Run("LabelNames", func(t *testing.T) { 191 s := &testStoreServer{ 192 info: &InfoResponse{ 193 LabelSets: []labelpb.ZLabelSet{{Labels: []labelpb.ZLabel{{Name: "a", Value: "b"}}}}, 194 MinTime: -1, 195 MaxTime: 10, 196 StoreType: StoreType_DEBUG, 197 }} 198 t.Run("ok", func(t *testing.T) { 199 for i := 0; i < 20; i++ { 200 r := &LabelNamesRequest{ 201 Start: -1, 202 End: 234, 203 PartialResponseStrategy: PartialResponseStrategy_ABORT, 204 } 205 resp, err := ServerAsClient(s, 0).LabelNames(context.TODO(), r) 206 testutil.Ok(t, err) 207 testutil.Equals(t, s.labelNames, resp) 208 testutil.Equals(t, r, s.labelNamesLastReq) 209 s.labelNamesLastReq = nil 210 } 211 }) 212 t.Run("error", func(t *testing.T) { 213 s.err = errors.New("some error") 214 for i := 0; i < 20; i++ { 215 r := &LabelNamesRequest{ 216 Start: -1, 217 End: 234, 218 PartialResponseStrategy: PartialResponseStrategy_ABORT, 219 } 220 _, err := ServerAsClient(s, 0).LabelNames(context.TODO(), r) 221 testutil.NotOk(t, err) 222 testutil.Equals(t, s.err, err) 223 } 224 }) 225 }) 226 t.Run("LabelValues", func(t *testing.T) { 227 s := &testStoreServer{ 228 labelValues: &LabelValuesResponse{ 229 Warnings: []string{"1", "a"}, 230 Values: []string{"abc1", "go_goroutines"}, 231 }, 232 } 233 t.Run("ok", func(t *testing.T) { 234 for i := 0; i < 20; i++ { 235 r := &LabelValuesRequest{ 236 Label: "__name__", 237 Start: -1, 238 End: 234, 239 PartialResponseStrategy: PartialResponseStrategy_ABORT, 240 } 241 resp, err := ServerAsClient(s, 0).LabelValues(context.TODO(), r) 242 testutil.Ok(t, err) 243 testutil.Equals(t, s.labelValues, resp) 244 testutil.Equals(t, r, s.labelValuesLastReq) 245 s.labelValuesLastReq = nil 246 } 247 }) 248 t.Run("error", func(t *testing.T) { 249 s.err = errors.New("some error") 250 for i := 0; i < 20; i++ { 251 r := &LabelValuesRequest{ 252 Label: "__name__", 253 Start: -1, 254 End: 234, 255 PartialResponseStrategy: PartialResponseStrategy_ABORT, 256 } 257 _, err := ServerAsClient(s, 0).LabelValues(context.TODO(), r) 258 testutil.NotOk(t, err) 259 testutil.Equals(t, s.err, err) 260 } 261 }) 262 }) 263 }) 264 } 265 }