MCPcopy
hub / github.com/harness/harness / TestCommandContextTimeout

Function TestCommandContextTimeout

git/command/command_test.go:58–114  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

56}
57
58func TestCommandContextTimeout(t *testing.T) {
59 cmd := New("init", WithFlag("--bare"), WithArg("samplerepo"))
60
61 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
62 defer cancel()
63
64 err := cmd.Run(ctx)
65 defer os.RemoveAll("samplerepo")
66 if err != nil {
67 t.Errorf("expected: %v error, got: %v", nil, err)
68 }
69
70 inbuff := &bytes.Buffer{}
71 inbuff.WriteString("some content")
72 outbuffer := &bytes.Buffer{}
73
74 cmd = New("hash-object", WithFlag("--stdin"))
75 err = cmd.Run(ctx,
76 WithDir("./samplerepo"),
77 WithStdin(inbuff),
78 WithStdout(outbuffer),
79 )
80 if err != nil {
81 t.Errorf("hashing object failed: %v", err)
82 return
83 }
84
85 log.Info().Msgf("outbuffer %s", outbuffer.String())
86
87 cmd = New("cat-file", WithFlag("--batch"))
88
89 pr, pw := io.Pipe()
90 defer pr.Close()
91
92 outbuffer.Reset()
93
94 go func() {
95 defer pw.Close()
96 for i := 0; i < 3; i++ {
97 _, _ = pw.Write(outbuffer.Bytes())
98 time.Sleep(1 * time.Second)
99 }
100 }()
101
102 runCtx, runCancel := context.WithTimeout(context.Background(), 1*time.Second)
103 defer runCancel()
104
105 err = cmd.Run(runCtx,
106 WithDir("./samplerepo"),
107 WithStdin(pr),
108 WithStdout(outbuffer),
109 )
110
111 if !errors.Is(err, context.DeadlineExceeded) {
112 t.Errorf("expected: %v error, got: %v", context.DeadlineExceeded, err)
113 }
114}

Callers

nothing calls this directly

Calls 15

IsFunction · 0.92
WithFlagFunction · 0.85
WithArgFunction · 0.85
WithDirFunction · 0.85
WithStdinFunction · 0.85
WithStdoutFunction · 0.85
MsgfMethod · 0.80
NewFunction · 0.70
RunMethod · 0.65
InfoMethod · 0.65
CloseMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…