code.gitea.io/gitea@v1.19.3/modules/git/command_race_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  //go:build race
     5  
     6  package git
     7  
     8  import (
     9  	"context"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestRunWithContextNoTimeout(t *testing.T) {
    15  	maxLoops := 10
    16  
    17  	// 'git --version' does not block so it must be finished before the timeout triggered.
    18  	cmd := NewCommand(context.Background(), "--version")
    19  	for i := 0; i < maxLoops; i++ {
    20  		if err := cmd.Run(&RunOpts{}); err != nil {
    21  			t.Fatal(err)
    22  		}
    23  	}
    24  }
    25  
    26  func TestRunWithContextTimeout(t *testing.T) {
    27  	maxLoops := 10
    28  
    29  	// 'git hash-object --stdin' blocks on stdin so we can have the timeout triggered.
    30  	cmd := NewCommand(context.Background(), "hash-object", "--stdin")
    31  	for i := 0; i < maxLoops; i++ {
    32  		if err := cmd.Run(&RunOpts{Timeout: 1 * time.Millisecond}); err != nil {
    33  			if err != context.DeadlineExceeded {
    34  				t.Fatalf("Testing %d/%d: %v", i, maxLoops, err)
    35  			}
    36  		}
    37  	}
    38  }