gitea源码

command_race_test.go 890B

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