Skip to content

Commit

Permalink
various fixes according to src-d/go-mysql-server#801
Browse files Browse the repository at this point in the history
Signed-off-by: lwsanty <lwsanty@gmail.com>
  • Loading branch information
lwsanty committed Aug 15, 2019
1 parent 8716d62 commit 77c764c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 26 deletions.
40 changes: 40 additions & 0 deletions capture-output/capture_output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package capture_output

import (
"fmt"
"io/ioutil"
"os"
"time"
)

func Capture(f func(), delay time.Duration) string {
file, err := ioutil.TempFile("", "prefix")
if err != nil {
panic(err)
}

stdout := os.Stdout
stderr := os.Stderr
os.Stdout = file
os.Stderr = file

defer func() {
os.Stdout = stdout
os.Stderr = stderr
os.RemoveAll(file.Name())
}()

f()
time.Sleep(delay)
if err := file.Close(); err != nil {
panic(err)
}

data, err := ioutil.ReadFile(file.Name())
if err != nil {
panic(err)
}
fmt.Println("data:", string(data))

return string(data)
}
68 changes: 48 additions & 20 deletions cmd/regression-bblfsh-mockups/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ import (
"github.com/bblfsh/sdk/v3/uast/nodes"
"github.com/bblfsh/sdk/v3/uast/nodes/nodesproto"
"github.com/jessevdk/go-flags"
"github.com/kami-zh/go-capturer"
"github.com/src-d/regression-core"
gitbase "github.com/src-d/regression-gitbase"
mockup "github.com/src-d/regression-gitbase/bblfsh-mockups"
capture_output "github.com/src-d/regression-gitbase/capture-output"
"gopkg.in/src-d/go-errors.v1"
"gopkg.in/src-d/go-log.v1"
)

const limit = 10
const (
limit = 10
queryTimeoutTotal = 15 * time.Second

var (
errFailedToCreateUASTData = errors.NewKind("failed to create UAST data")
captureOutputDelay = 500 * time.Millisecond
)

var (
testErrParseErrorOnly = []*v2.ParseError{{"only one parse error"}}
testErrParseErrorSeveral = []*v2.ParseError{
{"first parse error"},
Expand Down Expand Up @@ -68,7 +71,7 @@ func main() {
os.Exit(1)
}

log.Infof("PASS")
log.Infof("ALL TESTS PASSED")
}

// TestQueryExecBeforeTimeout
Expand Down Expand Up @@ -105,20 +108,27 @@ func TestQueryExecBeforeTimeout() error {
// 4) check that warning(or error?) related to timeout appeared?
func TestQueryExecAfterTimeout() error {
const (
errText = "parse error"
errText = "parse error"
errTimeoutOnRowRead = "row read wait bigger than connection timeout"

responseLagSeconds = 2
responseLag = responseLagSeconds * time.Second
connectionTimeoutSeconds = responseLagSeconds - 1
)
var errAssert = errors.NewKind("expected error containing %q, got: %v")

return testWarnings(context.Background(), mockup.OptsV2{
err := testWarnings(context.Background(), mockup.OptsV2{
ParseResponse: nil,
ParseResponseLag: responseLag,
ParseResponseErr: errors.NewKind(errText).New(),
}, map[string]string{
"GITBASE_CONNECTION_TIMEOUT": strconv.Itoa(connectionTimeoutSeconds),
}, "timeout", limit)
if err == nil || !strings.Contains(err.Error(), errTimeoutOnRowRead) {
return errAssert.New(errTimeoutOnRowRead, err)
}

return nil
}

// TestResponseErrorWarnings
Expand Down Expand Up @@ -158,9 +168,8 @@ func TestBrokenUASTInResponseWarning() error {
// 3) check that no error was returned during the query execution
// 4) check that related warnings appeared
func TestParseErrorWarnings() error {
node := nodes.String("a")
buf := bytes.NewBuffer(nil)
if err := nodesproto.WriteTo(buf, node); err != nil {
bufData, err := getUASTBytes("a")
if err != nil {
return err
}

Expand All @@ -181,7 +190,7 @@ func TestParseErrorWarnings() error {
}

return testWarnings(context.Background(), mockup.OptsV2{
ParseResponse: &v2.ParseResponse{Language: "go", Uast: buf.Bytes(), Errors: parseErrs},
ParseResponse: &v2.ParseResponse{Language: "go", Uast: bufData, Errors: parseErrs},
}, nil, text, repeats)
}

Expand All @@ -198,6 +207,11 @@ func TestParseErrorWarnings() error {
return nil
}

type queryResult struct {
out string
err error
}

func testWarnings(ctx context.Context, o mockup.OptsV2, gitbaseEnvs map[string]string, expWarning string, repeats int) error {
closer, err := mockup.PrepareGRPCServer(mockup.Options{OptsV2: o})
defer closer()
Expand All @@ -210,17 +224,31 @@ func testWarnings(ctx context.Context, o mockup.OptsV2, gitbaseEnvs map[string]s
return err
}

out := capturer.CaptureStderr(func() {
err = test.RunQueryCtx(ctx, gitbaseEnvs, gitbase.Query{
Statements: []string{uastQuery},
})
})
if err != nil {
return err
resc := make(chan queryResult)
go func() {
var tmpErr error
tmpOut := capture_output.Capture(func() {
tmpErr = test.RunQueryCtx(ctx, gitbaseEnvs, gitbase.Query{
Statements: []string{uastQuery},
})
}, captureOutputDelay)

resc <- queryResult{out: tmpOut, err: tmpErr}
}()

var outPut string
select {
case res := <-resc:
if res.err != nil {
return res.err
}
outPut = res.out
case <-time.After(queryTimeoutTotal):
return errors.NewKind("query is being executed longer than expected").New()
}

log.Infof("out: %v", out)
actRepeats := strings.Count(out, expWarning)
log.Infof("out: %v", outPut)
actRepeats := strings.Count(outPut, expWarning)
if repeats != actRepeats {
return errors.NewKind("repeats of %q: exp: %v act: %v").New(expWarning, repeats, actRepeats)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/go-sql-driver/mysql v1.4.0
github.com/google/go-cmp v0.3.1 // indirect
github.com/jessevdk/go-flags v1.4.0
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d h1:cVtBfNW5XTHiKQe7jDaDBSh/EVM4XLPutLAGboIXuM0=
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d/go.mod h1:P2viExyCEfeWGU259JnaQ34Inuec4R38JCyBx2edgD0=
github.com/kevinburke/go-bindata v3.13.0+incompatible/go.mod h1:/pEEZ72flUW2p0yi30bslSp9YqD9pysLxunQDdb2CPM=
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e h1:RgQk53JHp/Cjunrr1WlsXSZpqXn+uREuHvUVcK82CV8=
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
Expand Down
8 changes: 5 additions & 3 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,16 @@ func (t *Test) runQueryCtx(
return err
}

defer func() {
queries.Disconnect()
server.Stop()
}()

_, err = queries.ExecuteCtx(ctx)
if err != nil {
return err
}

queries.Disconnect()
server.Stop()

return nil
}

Expand Down

0 comments on commit 77c764c

Please sign in to comment.