|
| 1 | +package cortexpb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "github.com/weaveworks/common/user" |
| 11 | +) |
| 12 | + |
| 13 | +func TestWriteRequest_Sign(t *testing.T) { |
| 14 | + ctx := context.Background() |
| 15 | + ctx = user.InjectOrgID(ctx, "user-1") |
| 16 | + |
| 17 | + tests := map[string]struct { |
| 18 | + w *WriteRequest |
| 19 | + expectedSign string |
| 20 | + }{ |
| 21 | + "small write with exemplar": { |
| 22 | + w: createWriteRequest(10, true, "family1", "help1", "unit"), |
| 23 | + expectedSign: "v1/9125893422459502203", |
| 24 | + }, |
| 25 | + "small write with exemplar and changed md": { |
| 26 | + w: createWriteRequest(10, true, "family2", "help1", "unit"), |
| 27 | + expectedSign: "v1/18044786562323437562", |
| 28 | + }, |
| 29 | + "small write without exemplar": { |
| 30 | + w: createWriteRequest(10, false, "family1", "help1", "unit"), |
| 31 | + expectedSign: "v1/7697478040597284323", |
| 32 | + }, |
| 33 | + "big write with exemplar": { |
| 34 | + w: createWriteRequest(10000, true, "family1", "help1", "unit"), |
| 35 | + expectedSign: "v1/18402783317092766507", |
| 36 | + }, |
| 37 | + "big write without exemplar": { |
| 38 | + w: createWriteRequest(10000, false, "family1", "help1", "unit"), |
| 39 | + expectedSign: "v1/14973071954515615892", |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + for name, tc := range tests { |
| 44 | + t.Run(name, func(t *testing.T) { |
| 45 | + // running multiple times in parallel to make sure no race |
| 46 | + itNumber := 1000 |
| 47 | + wg := sync.WaitGroup{} |
| 48 | + wg.Add(itNumber) |
| 49 | + for i := 0; i < itNumber; i++ { |
| 50 | + go func() { |
| 51 | + defer wg.Done() |
| 52 | + s, err := tc.w.Sign(ctx) |
| 53 | + require.NoError(t, err) |
| 54 | + // Make sure this sign doesn't change |
| 55 | + require.Equal(t, tc.expectedSign, s) |
| 56 | + }() |
| 57 | + } |
| 58 | + wg.Wait() |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func createWriteRequest(numTs int, exemplar bool, family string, help string, unit string) *WriteRequest { |
| 64 | + w := &WriteRequest{} |
| 65 | + w.Metadata = []*MetricMetadata{ |
| 66 | + { |
| 67 | + MetricFamilyName: family, |
| 68 | + Help: help, |
| 69 | + Unit: unit, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + for i := 0; i < numTs; i++ { |
| 74 | + w.Timeseries = append(w.Timeseries, PreallocTimeseries{ |
| 75 | + TimeSeries: &TimeSeries{ |
| 76 | + Labels: []LabelAdapter{ |
| 77 | + { |
| 78 | + Name: fmt.Sprintf("Name-%v", i), |
| 79 | + Value: fmt.Sprintf("Value-%v", i), |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }) |
| 84 | + |
| 85 | + if exemplar { |
| 86 | + w.Timeseries[i].Exemplars = []Exemplar{ |
| 87 | + { |
| 88 | + Labels: []LabelAdapter{ |
| 89 | + { |
| 90 | + Name: fmt.Sprintf("Ex-Name-%v", i), |
| 91 | + Value: fmt.Sprintf("Ex-Value-%v", i), |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return w |
| 100 | +} |
0 commit comments