Repository: prometheus/prometheus
File: tsdb/agent/db_test.go (Go)

Code before:

			for r.Next() {
				rec := r.Record()
				switch dec.Type(rec) {
				case record.Series:
					series, err := dec.Series(rec, nil)
					require.NoError(t, err)
					allSeries = append(allSeries, series...)
				case record.Samples:
					samples, err = dec.Samples(rec, samples[:0])
					require.NoError(t, err)
				case record.HistogramSamples:
					histograms, err = dec.HistogramSamples(rec, histograms[:0])
					require.NoError(t, err)
				}
			}

			// Verify the contents
			require.Len(t, allSeries, 1, "Expected 1 series")
			if tc.expectedType == "histogram" {
				require.Len(t, histograms, tc.expectedSampleCount, "Expected %d histograms", tc.expectedSampleCount)
				require.Empty(t, samples, "Expected no float samples")
			} else {
				require.Len(t, samples, tc.expectedSampleCount, "Expected %d samples", tc.expectedSampleCount)
				require.Empty(t, histograms, "Expected no histograms")
			}

			// Verify series
			for _, s := range allSeries {
				lset, ok := seriesMap[storage.SeriesRef(s.Ref)]
				require.True(t, ok, "Series not found in seriesMap")
				require.Equal(t, lset.String(), s.Labels.String(), "Series labels don't match")
			}

			// Verify samples or histograms
			if tc.expectedType == "histogram" {
				for _, h := range histograms {
					expectedHistograms, ok := histogramsMap[storage.SeriesRef(h.Ref)]
					require.True(t, ok, "Histogram refers to unknown series")

					found := false
					for _, eh := range expectedHistograms {
						if eh.T == h.T && eh.H.Equals(h.H) {
							found = true
							break
						}
					}
					require.True(t, found, "Histogram not found in expected histograms")
				}
			} else {
				for _, s := range samples {

Review comment (refactor):
This is awesome! Reading the WAL to verify that we appended the right thing is so much better than checking metrics! I think several tests in this file should be using this approach instead :) 

To make this usable for others as well, what do you think about extracting this logic into a separate function? Something like:

```go
func WalToChunkSample(t *testing.T, wal *wlog.WL) []chuncks.Sample {
 t.Helper()
 ...
}
```

Code after:
			expectedSamples: []*walSample{
				{t: 1, h: &histogram.Histogram{}},
				{t: 100, h: testHistogram},
				{t: 101, h: &histogram.Histogram{CounterResetHint: histogram.NotCounterReset}},
			},
		},
		{
			name: "ct+normal then OOO sample/float",
			inputSamples: []appendableSample{
				{t: 60_000, ct: 40_000, v: 10, lbls: defLbls},
				{t: 120_000, ct: 40_000, v: 10, lbls: defLbls},
				{t: 180_000, ct: 40_000, v: 10, lbls: defLbls},
				{t: 50_000, ct: 40_000, v: 10, lbls: defLbls},
			},
			expectedSamples: []*walSample{
				{t: 40_000, f: 0, lbls: defLbls},
				{t: 50_000, f: 10, lbls: defLbls},
				{t: 60_000, f: 10, lbls: defLbls},
				{t: 120_000, f: 10, lbls: defLbls},
				{t: 180_000, f: 10, lbls: defLbls},
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			reg := prometheus.NewRegistry()
			opts := DefaultOptions()
			opts.OutOfOrderTimeWindow = 360_000
			s := createTestAgentDB(t, reg, opts)
			app := s.Appender(context.TODO())

			for _, sample := range tc.inputSamples {
				// We supposed to write a Histogram to the WAL
				if sample.h != nil {
					_, err := app.AppendHistogramCTZeroSample(0, sample.lbls, sample.t, sample.ct, zeroHistogram, nil)
					if !errors.Is(err, storage.ErrOutOfOrderCT) {
						require.Equal(t, sample.expectsError, err != nil, "expected error: %v, got: %v", sample.expectsError, err)
					}

					_, err = app.AppendHistogram(0, sample.lbls, sample.t, sample.h, nil)
					require.NoError(t, err)
				} else {
					// We supposed to write a float sample to the WAL
					_, err := app.AppendCTZeroSample(0, sample.lbls, sample.t, sample.ct)
					if !errors.Is(err, storage.ErrOutOfOrderCT) {
						require.Equal(t, sample.expectsError, err != nil, "expected error: %v, got: %v", sample.expectsError, err)
					}