newTestScrapeLoop is the initial scrape loop for all tests. It returns scrapeLoop and mock scraper you can customize. It's recommended to use withXYZ functions for simple option customizations, e.g: sl, _ := newTestScrapeLoop(t, withCtx(customCtx)) However, when changing more than one scrapeLoop
(t testing.TB, opts ...func(sl *scrapeLoop))
| 87 | // newTestScrapeLoop-like constructors. It should be flexible enough with scrapeLoop |
| 88 | // used for initial options. |
| 89 | func newTestScrapeLoop(t testing.TB, opts ...func(sl *scrapeLoop)) (_ *scrapeLoop, scraper *testScraper) { |
| 90 | metrics := newTestScrapeMetrics(t) |
| 91 | sl := &scrapeLoop{ |
| 92 | stopped: make(chan struct{}), |
| 93 | |
| 94 | l: promslog.NewNopLogger(), |
| 95 | cache: newScrapeCache(metrics), |
| 96 | |
| 97 | interval: 10 * time.Millisecond, |
| 98 | timeout: 1 * time.Hour, |
| 99 | sampleMutator: nopMutator, |
| 100 | reportSampleMutator: nopMutator, |
| 101 | buffers: pool.New(1e3, 1e6, 3, func(sz int) any { return make([]byte, 0, sz) }), |
| 102 | metrics: metrics, |
| 103 | maxSchema: histogram.ExponentialSchemaMax, |
| 104 | honorTimestamps: true, |
| 105 | enableCompression: true, |
| 106 | validationScheme: model.UTF8Validation, |
| 107 | symbolTable: labels.NewSymbolTable(), |
| 108 | // Tests assume those features are enabled, unless explicitly turned off. |
| 109 | appendMetadataToWAL: true, |
| 110 | parseST: true, |
| 111 | } |
| 112 | for _, o := range opts { |
| 113 | o(sl) |
| 114 | } |
| 115 | |
| 116 | if sl.appendable != nil && sl.appendableV2 != nil { |
| 117 | t.Fatal("select the appendable to use, both were passed, likely a bug") |
| 118 | } |
| 119 | |
| 120 | // Validate user opts for convenience. |
| 121 | require.Nil(t, sl.parentCtx, "newTestScrapeLoop does not support injecting non-nil parent context") |
| 122 | require.Nil(t, sl.appenderCtx, "newTestScrapeLoop does not support injecting non-nil appender context") |
| 123 | require.Nil(t, sl.cancel, "newTestScrapeLoop does not support injecting custom cancel function") |
| 124 | require.Nil(t, sl.scraper, "newTestScrapeLoop does not support injecting scraper, it's mocked, use the returned scraper") |
| 125 | |
| 126 | rootCtx := t.Context() |
| 127 | // Use sl.ctx for context injection. |
| 128 | // True contexts (sl.appenderCtx, sl.parentCtx, sl.ctx) are populated from it |
| 129 | if sl.ctx != nil { |
| 130 | rootCtx = sl.ctx |
| 131 | } |
| 132 | ctx, cancel := context.WithCancel(rootCtx) |
| 133 | sl.ctx = ctx |
| 134 | sl.cancel = cancel |
| 135 | sl.appenderCtx = rootCtx |
| 136 | sl.parentCtx = rootCtx |
| 137 | |
| 138 | scraper = &testScraper{} |
| 139 | sl.scraper = scraper |
| 140 | return sl, scraper |
| 141 | } |
| 142 | |
| 143 | func newTestScrapePool(t *testing.T, app compatAppendable, appV2 bool, injectNewLoop func(options scrapeLoopOptions) loop) *scrapePool { |
| 144 | sa := selectAppendable(app, appV2) |
no test coverage detected
searching dependent graphs…