github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/command_race_test.go (about)

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