Skip to content

Commit 65badbc

Browse files
committed
Add LineBufferSize option field
To support configuring the line buffer size a new field LineBufferSize is added to the cmd.Options struct. This allows configuring the line buffer size to the callers needs instead of relying on the DEFAULT_LINE_BUFFER_SIZE.
1 parent 05b0600 commit 65badbc

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

cmd.go

+12
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ type Options struct {
157157
// the real command. These functions can be used to customize the underlying
158158
// os/exec.Cmd. For example, to set SysProcAttr.
159159
BeforeExec []func(cmd *exec.Cmd)
160+
161+
// LineBufferSize sets the size of the OutputStream line buffer. The default
162+
// value DEFAULT_LINE_BUFFER_SIZE is usually sufficient, but if
163+
// ErrLineBufferOverflow errors occur, try increasing the size with this
164+
// field.
165+
LineBufferSize int
160166
}
161167

162168
// NewCmdOptions creates a new Cmd with options. The command is not started
@@ -178,6 +184,10 @@ func NewCmdOptions(options Options, name string, args ...string) *Cmd {
178184
doneChan: make(chan struct{}),
179185
}
180186

187+
if options.LineBufferSize == 0 {
188+
options.LineBufferSize = DEFAULT_LINE_BUFFER_SIZE
189+
}
190+
181191
if options.Buffered {
182192
c.stdoutBuf = NewOutputBuffer()
183193
c.stderrBuf = NewOutputBuffer()
@@ -186,9 +196,11 @@ func NewCmdOptions(options Options, name string, args ...string) *Cmd {
186196
if options.Streaming {
187197
c.Stdout = make(chan string, DEFAULT_STREAM_CHAN_SIZE)
188198
c.stdoutStream = NewOutputStream(c.Stdout)
199+
c.stdoutStream.SetLineBufferSize(options.LineBufferSize)
189200

190201
c.Stderr = make(chan string, DEFAULT_STREAM_CHAN_SIZE)
191202
c.stderrStream = NewOutputStream(c.Stderr)
203+
c.stderrStream.SetLineBufferSize(options.LineBufferSize)
192204
}
193205

194206
if len(options.BeforeExec) > 0 {

cmd_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -1167,3 +1167,63 @@ func TestOptionsBeforeExec(t *testing.T) {
11671167
t.Error("exec cmd option not applied")
11681168
}
11691169
}
1170+
1171+
func TestCmdLineBufferIncrease(t *testing.T) {
1172+
lineContent := cmd.DEFAULT_LINE_BUFFER_SIZE * 2
1173+
longLine := make([]byte, lineContent) // "AAA..."
1174+
for i := 0; i < lineContent; i++ {
1175+
longLine[i] = 'A'
1176+
}
1177+
1178+
tmpfile, err := ioutil.TempFile("", "cmd.TestCmdLineBufferIncrease")
1179+
if err != nil {
1180+
t.Fatal(err)
1181+
}
1182+
t.Cleanup(func() {
1183+
os.Remove(tmpfile.Name())
1184+
})
1185+
if _, err := tmpfile.Write(longLine); err != nil {
1186+
t.Fatal(err)
1187+
}
1188+
if err := tmpfile.Close(); err != nil {
1189+
t.Fatal(err)
1190+
}
1191+
1192+
timeout := time.After(10 * time.Second) // test timeout
1193+
1194+
catStdout := cmd.NewCmdOptions(cmd.Options{
1195+
Streaming: true,
1196+
LineBufferSize: cmd.DEFAULT_LINE_BUFFER_SIZE * 2,
1197+
}, "./test/cat", tmpfile.Name(), "1")
1198+
1199+
catStdoutStatus := catStdout.Start()
1200+
1201+
select {
1202+
case curLine := <-catStdout.Stdout:
1203+
t.Logf("got stdout bytes: %d", len(curLine))
1204+
if len(curLine) <= cmd.DEFAULT_LINE_BUFFER_SIZE {
1205+
t.Error("Unable to read more than default line buffer from stdout")
1206+
}
1207+
case <-timeout:
1208+
t.Fatal("timeout reading streaming output")
1209+
}
1210+
<-catStdoutStatus
1211+
1212+
catStderr := cmd.NewCmdOptions(cmd.Options{
1213+
Streaming: true,
1214+
LineBufferSize: cmd.DEFAULT_LINE_BUFFER_SIZE * 2,
1215+
}, "./test/cat", tmpfile.Name(), "2")
1216+
1217+
catStderrStatus := catStderr.Start()
1218+
1219+
select {
1220+
case curLine := <-catStderr.Stderr:
1221+
t.Logf("got stderr bytes: %d", len(curLine))
1222+
if len(curLine) <= cmd.DEFAULT_LINE_BUFFER_SIZE {
1223+
t.Error("Unable to read more than default line buffer from stderr")
1224+
}
1225+
case <-timeout:
1226+
t.Fatal("timeout reading streaming output")
1227+
}
1228+
<-catStderrStatus
1229+
}

test/cat

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
cat $1 >&$2

0 commit comments

Comments
 (0)