1// Copyright 2017 The Gitea Authors. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5//go:build race
6// +build race
7
8package git
9
10import (
11	"context"
12	"testing"
13	"time"
14)
15
16func TestRunInDirTimeoutPipelineNoTimeout(t *testing.T) {
17
18	maxLoops := 1000
19
20	// 'git --version' does not block so it must be finished before the timeout triggered.
21	cmd := NewCommand("--version")
22	for i := 0; i < maxLoops; i++ {
23		if err := cmd.RunInDirTimeoutPipeline(-1, "", nil, nil); err != nil {
24			t.Fatal(err)
25		}
26	}
27}
28
29func TestRunInDirTimeoutPipelineAlwaysTimeout(t *testing.T) {
30
31	maxLoops := 1000
32
33	// 'git hash-object --stdin' blocks on stdin so we can have the timeout triggered.
34	cmd := NewCommand("hash-object", "--stdin")
35	for i := 0; i < maxLoops; i++ {
36		if err := cmd.RunInDirTimeoutPipeline(1*time.Microsecond, "", nil, nil); err != nil {
37			if err != context.DeadlineExceeded {
38				t.Fatalf("Testing %d/%d: %v", i, maxLoops, err)
39			}
40		}
41	}
42}
43