MCPcopy
hub / github.com/apache/devlake / runWithTimeout

Function runWithTimeout

backend/test/helper/client.go:329–362  ·  view source on GitHub ↗
(timeout time.Duration, f func() (bool, errors.Error))

Source from the content-addressed store, hash-verified

327}
328
329func runWithTimeout(timeout time.Duration, f func() (bool, errors.Error)) errors.Error {
330 if timeout == 0 {
331 timeout = math.MaxInt
332 }
333 type response struct {
334 err errors.Error
335 completed bool
336 }
337 timer := time.After(timeout)
338 resChan := make(chan response)
339 resp := response{}
340 for {
341 go func() {
342 done, err := f()
343 resChan <- response{err, done}
344 }()
345 select {
346 case <-timer:
347 if !resp.completed {
348 return errors.Default.New(fmt.Sprintf("timed out calling function after %d milliseconds", timeout.Milliseconds()))
349 }
350 return nil
351 case resp = <-resChan:
352 if resp.err != nil {
353 return resp.err
354 }
355 if resp.completed {
356 return nil
357 }
358 time.Sleep(1 * time.Second)
359 continue
360 }
361 }
362}
363
364func sendHttpRequest[Res any](t *testing.T, timeout time.Duration, ctx *testContext, httpMethod string, endpoint string, headers map[string]string, body any) Res {
365 t.Helper()

Callers 3

monitorPipelineMethod · 0.85
sendHttpRequestFunction · 0.85

Calls 1

NewMethod · 0.65

Tested by

no test coverage detected