(t *testing.T, appV2 bool)
| 1143 | } |
| 1144 | |
| 1145 | func testScrapeLoopRun(t *testing.T, appV2 bool) { |
| 1146 | t.Parallel() |
| 1147 | var ( |
| 1148 | signal = make(chan struct{}, 1) |
| 1149 | errc = make(chan error) |
| 1150 | ) |
| 1151 | |
| 1152 | ctx, cancel := context.WithCancel(t.Context()) |
| 1153 | sl, scraper := newTestScrapeLoop(t, withCtx(ctx), withAppendable(teststorage.NewAppendable(), appV2)) |
| 1154 | // The loop must terminate during the initial offset if the context |
| 1155 | // is canceled. |
| 1156 | scraper.offsetDur = time.Hour |
| 1157 | |
| 1158 | go func() { |
| 1159 | sl.run(errc) |
| 1160 | signal <- struct{}{} |
| 1161 | }() |
| 1162 | |
| 1163 | // Wait to make sure we are actually waiting on the offset. |
| 1164 | time.Sleep(1 * time.Second) |
| 1165 | |
| 1166 | cancel() |
| 1167 | select { |
| 1168 | case <-signal: |
| 1169 | case <-time.After(5 * time.Second): |
| 1170 | require.FailNow(t, "Cancellation during initial offset failed.") |
| 1171 | case err := <-errc: |
| 1172 | require.FailNow(t, "Unexpected error", "err: %s", err) |
| 1173 | } |
| 1174 | |
| 1175 | ctx, cancel = context.WithCancel(t.Context()) |
| 1176 | sl, scraper = newTestScrapeLoop(t, withAppendable(teststorage.NewAppendable(), appV2), func(sl *scrapeLoop) { |
| 1177 | sl.ctx = ctx |
| 1178 | sl.timeout = 100 * time.Millisecond |
| 1179 | }) |
| 1180 | // The provided timeout must cause cancellation of the context passed down to the |
| 1181 | // scraper. The scraper has to respect the context. |
| 1182 | scraper.offsetDur = 0 |
| 1183 | |
| 1184 | blockCtx, blockCancel := context.WithCancel(t.Context()) |
| 1185 | scraper.scrapeFunc = func(ctx context.Context, _ io.Writer) error { |
| 1186 | select { |
| 1187 | case <-blockCtx.Done(): |
| 1188 | cancel() |
| 1189 | case <-ctx.Done(): |
| 1190 | return ctx.Err() |
| 1191 | } |
| 1192 | return nil |
| 1193 | } |
| 1194 | |
| 1195 | go func() { |
| 1196 | sl.run(errc) |
| 1197 | signal <- struct{}{} |
| 1198 | }() |
| 1199 | |
| 1200 | select { |
| 1201 | case err := <-errc: |
| 1202 | require.ErrorIs(t, err, context.DeadlineExceeded) |
no test coverage detected
searching dependent graphs…