mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #14376 from biosvs/fix-expect-package
Fixed infinite loop in ExpectProcess.ExpectFunc
This commit is contained in:
commit
82bf79d4ce
@ -18,6 +18,7 @@ package expect
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -103,7 +104,7 @@ func (ep *ExpectProcess) read() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ExpectFunc returns the first line satisfying the function f.
|
// ExpectFunc returns the first line satisfying the function f.
|
||||||
func (ep *ExpectProcess) ExpectFunc(f func(string) bool) (string, error) {
|
func (ep *ExpectProcess) ExpectFunc(ctx context.Context, f func(string) bool) (string, error) {
|
||||||
i := 0
|
i := 0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -121,7 +122,13 @@ func (ep *ExpectProcess) ExpectFunc(f func(string) bool) (string, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
ep.mu.Unlock()
|
ep.mu.Unlock()
|
||||||
time.Sleep(time.Millisecond * 10)
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return "", fmt.Errorf("failed to find match string: %w", ctx.Err())
|
||||||
|
case <-time.After(time.Millisecond * 10):
|
||||||
|
// continue loop
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ep.mu.Lock()
|
ep.mu.Lock()
|
||||||
lastLinesIndex := len(ep.lines) - DEBUG_LINES_TAIL
|
lastLinesIndex := len(ep.lines) - DEBUG_LINES_TAIL
|
||||||
@ -135,9 +142,15 @@ func (ep *ExpectProcess) ExpectFunc(f func(string) bool) (string, error) {
|
|||||||
ep.err, lastLines)
|
ep.err, lastLines)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExpectWithContext returns the first line containing the given string.
|
||||||
|
func (ep *ExpectProcess) ExpectWithContext(ctx context.Context, s string) (string, error) {
|
||||||
|
return ep.ExpectFunc(ctx, func(txt string) bool { return strings.Contains(txt, s) })
|
||||||
|
}
|
||||||
|
|
||||||
// Expect returns the first line containing the given string.
|
// Expect returns the first line containing the given string.
|
||||||
|
// Deprecated: please use ExpectWithContext instead.
|
||||||
func (ep *ExpectProcess) Expect(s string) (string, error) {
|
func (ep *ExpectProcess) Expect(s string) (string, error) {
|
||||||
return ep.ExpectFunc(func(txt string) bool { return strings.Contains(txt, s) })
|
return ep.ExpectWithContext(context.Background(), s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LineCount returns the number of recorded lines since
|
// LineCount returns the number of recorded lines since
|
||||||
|
@ -17,9 +17,12 @@
|
|||||||
package expect
|
package expect
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExpectFunc(t *testing.T) {
|
func TestExpectFunc(t *testing.T) {
|
||||||
@ -28,7 +31,7 @@ func TestExpectFunc(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
wstr := "hello world\r\n"
|
wstr := "hello world\r\n"
|
||||||
l, eerr := ep.ExpectFunc(func(a string) bool { return len(a) > 10 })
|
l, eerr := ep.ExpectFunc(context.Background(), func(a string) bool { return len(a) > 10 })
|
||||||
if eerr != nil {
|
if eerr != nil {
|
||||||
t.Fatal(eerr)
|
t.Fatal(eerr)
|
||||||
}
|
}
|
||||||
@ -40,6 +43,33 @@ func TestExpectFunc(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExpectFuncTimeout(t *testing.T) {
|
||||||
|
ep, err := NewExpect("tail", "-f", "/dev/null")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
// It's enough to have "talkative" process to stuck in the infinite loop of reading
|
||||||
|
for {
|
||||||
|
err := ep.Send("new line\n")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
_, err = ep.ExpectFunc(ctx, func(a string) bool { return false })
|
||||||
|
|
||||||
|
require.ErrorAs(t, err, &context.DeadlineExceeded)
|
||||||
|
|
||||||
|
if err = ep.Stop(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestEcho(t *testing.T) {
|
func TestEcho(t *testing.T) {
|
||||||
ep, err := NewExpect("echo", "hello world")
|
ep, err := NewExpect("echo", "hello world")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package e2e
|
package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -106,7 +107,7 @@ func ctlV3Elect(cx ctlCtx, name, proposal string) (*expect.ExpectProcess, <-chan
|
|||||||
return proc, outc, err
|
return proc, outc, err
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
s, xerr := proc.ExpectFunc(func(string) bool { return true })
|
s, xerr := proc.ExpectFunc(context.TODO(), func(string) bool { return true })
|
||||||
if xerr != nil {
|
if xerr != nil {
|
||||||
cx.t.Errorf("expect failed (%v)", xerr)
|
cx.t.Errorf("expect failed (%v)", xerr)
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package e2e
|
package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -127,7 +128,7 @@ func ctlV3Lock(cx ctlCtx, name string) (*expect.ExpectProcess, <-chan string, er
|
|||||||
return proc, outc, err
|
return proc, outc, err
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
s, xerr := proc.ExpectFunc(func(string) bool { return true })
|
s, xerr := proc.ExpectFunc(context.TODO(), func(string) bool { return true })
|
||||||
if xerr != nil {
|
if xerr != nil {
|
||||||
cx.t.Errorf("expect failed (%v)", xerr)
|
cx.t.Errorf("expect failed (%v)", xerr)
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package e2e
|
package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -249,7 +250,7 @@ func testV3CurlAuth(cx ctlCtx) {
|
|||||||
testutil.AssertNil(cx.t, err)
|
testutil.AssertNil(cx.t, err)
|
||||||
defer proc.Close()
|
defer proc.Close()
|
||||||
|
|
||||||
cURLRes, err := proc.ExpectFunc(lineFunc)
|
cURLRes, err := proc.ExpectFunc(context.Background(), lineFunc)
|
||||||
testutil.AssertNil(cx.t, err)
|
testutil.AssertNil(cx.t, err)
|
||||||
|
|
||||||
authRes := make(map[string]interface{})
|
authRes := make(map[string]interface{})
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package e2e
|
package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@ -36,7 +37,7 @@ func WaitReadyExpectProc(exproc *expect.ExpectProcess, readyStrs []string) error
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
_, err := exproc.ExpectFunc(matchSet)
|
_, err := exproc.ExpectFunc(context.Background(), matchSet)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user