Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integration tests: wait for index creation #407

Merged
merged 3 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmdtests/gitbase_back_comp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
"github.com/src-d/engine/cmdtests"
"gotest.tools/icmd"

"gopkg.in/src-d/regression-core.v0"
regression "gopkg.in/src-d/regression-core.v0"

"github.com/mitchellh/go-homedir"
homedir "github.com/mitchellh/go-homedir"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -187,8 +187,9 @@ func (s *GitbaseBackCompTestSuite) TestRetroCompatibleIndexes() {
// [previous version] srcd sql "CREATE INDEXES"
r = s.runSQL(s.PrevCmd, "CREATE INDEX repo_idx ON repositories USING pilosa (repository_id)")
require.NoError(r.Error, r.Combined())
// wait a bit for index to be ready
time.Sleep(1 * time.Second)

// wait for index to be visible, full output assert below
IndexIsVisible(s, "repositories", "repo_idx")

// [previous version] srcd sql "SHOW INDEXES"
r = s.runSQL(s.PrevCmd, "SHOW INDEX FROM repositories")
Expand Down
56 changes: 56 additions & 0 deletions cmdtests/sql_index_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// +build integration regression

package cmdtests

import (
"strings"
"time"

"github.com/stretchr/testify/require"
"gotest.tools/icmd"
)

// building/loading index takes some time, wait maximum 15s
func IndexIsVisible(s commandSuite, table, name string) bool {
for i := 0; i < 15; i++ {
if hasIndex(s, table, name) {
return true
}
time.Sleep(time.Second)
}

return hasIndex(s, table, name)
}

func hasIndex(s commandSuite, table, name string) bool {
r := s.RunCommand("sql", "SHOW INDEX FROM "+table)
s.Require().NoError(r.Error, r.Combined())

// parse result and check that correct index was built and it is visiable
// see example output here: https://dev.mysql.com/doc/refman/8.0/en/show-index.html
lines := strings.Split(r.Stdout(), "\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add an example output. This way if the format changes in the future we can compare it and fix the code easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a link instead of output itself (it's huge). The link contains version of mysql so it won't change.

for _, line := range lines {
cols := strings.Split(line, "|")
if len(cols) < 15 {
continue
}
if strings.TrimSpace(cols[1]) != table {
continue
}
if strings.TrimSpace(cols[3]) != name {
continue
}

if strings.TrimSpace(cols[14]) == "YES" {
return true
}
return false
}

return false
}

type commandSuite interface {
RunCommand(cmd string, args ...string) *icmd.Result
Require() *require.Assertions
}
16 changes: 2 additions & 14 deletions cmdtests/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,6 @@ func (s *SQLTestSuite) TestIndexesWorkdirChange() {
r = s.RunCommand("sql", "CREATE INDEX repo_idx ON repositories USING pilosa (repository_id)")
require.NoError(err, r.Stdout())

time.Sleep(1 * time.Second) // wait for index to be built

s.testQueryWithIndex(require, "repos", true)

// workdir 2
Expand All @@ -360,26 +358,16 @@ func (s *SQLTestSuite) TestIndexesWorkdirChange() {
// wait for gitbase to be ready
r = s.RunCommand("sql", "select 1")
require.NoError(r.Error, r.Combined())
// wait for gitbase to load index
time.Sleep(1 * time.Second)

s.testQueryWithIndex(require, "repos", true)
}

func (s *SQLTestSuite) testQueryWithIndex(require *require.Assertions, repo string, hasIndex bool) {
r := s.RunCommand("sql", "SHOW INDEX FROM repositories")
require.NoError(r.Error, r.Combined())

if hasIndex {
// parse result and check that correct index was built and it is visiable
indexLine := strings.Split(r.Stdout(), "\n")[3]
expected := `repositories.repository_id`
require.Contains(indexLine, expected)
visibleValue := strings.TrimSpace(strings.Split(indexLine, "|")[14])
require.Equal("YES", visibleValue)
require.True(cmdtests.IndexIsVisible(s, "repositories", "repo_idx"))
}

r = s.RunCommand("sql", "EXPLAIN FORMAT=TREE select * from repositories WHERE repository_id='"+repo+"'")
r := s.RunCommand("sql", "EXPLAIN FORMAT=TREE select * from repositories WHERE repository_id='"+repo+"'")
require.NoError(r.Error, r.Combined())
if hasIndex {
require.Contains(r.Stdout(), "Indexes")
Expand Down