Skip to content

Commit ac407ec

Browse files
authored
Merge pull request #407 from smacker/wait_for_index
integration tests: wait for index creation
2 parents d926da1 + 09e8b97 commit ac407ec

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

cmdtests/gitbase_back_comp_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717
"github.com/src-d/engine/cmdtests"
1818
"gotest.tools/icmd"
1919

20-
"gopkg.in/src-d/regression-core.v0"
20+
regression "gopkg.in/src-d/regression-core.v0"
2121

22-
"github.com/mitchellh/go-homedir"
22+
homedir "github.com/mitchellh/go-homedir"
2323
"github.com/stretchr/testify/suite"
2424
)
2525

@@ -187,8 +187,9 @@ func (s *GitbaseBackCompTestSuite) TestRetroCompatibleIndexes() {
187187
// [previous version] srcd sql "CREATE INDEXES"
188188
r = s.runSQL(s.PrevCmd, "CREATE INDEX repo_idx ON repositories USING pilosa (repository_id)")
189189
require.NoError(r.Error, r.Combined())
190-
// wait a bit for index to be ready
191-
time.Sleep(1 * time.Second)
190+
191+
// wait for index to be visible, full output assert below
192+
IndexIsVisible(s, "repositories", "repo_idx")
192193

193194
// [previous version] srcd sql "SHOW INDEXES"
194195
r = s.runSQL(s.PrevCmd, "SHOW INDEX FROM repositories")

cmdtests/sql_index_util.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// +build integration regression
2+
3+
package cmdtests
4+
5+
import (
6+
"strings"
7+
"time"
8+
9+
"github.com/stretchr/testify/require"
10+
"gotest.tools/icmd"
11+
)
12+
13+
// building/loading index takes some time, wait maximum 15s
14+
func IndexIsVisible(s commandSuite, table, name string) bool {
15+
for i := 0; i < 15; i++ {
16+
if hasIndex(s, table, name) {
17+
return true
18+
}
19+
time.Sleep(time.Second)
20+
}
21+
22+
return hasIndex(s, table, name)
23+
}
24+
25+
func hasIndex(s commandSuite, table, name string) bool {
26+
r := s.RunCommand("sql", "SHOW INDEX FROM "+table)
27+
s.Require().NoError(r.Error, r.Combined())
28+
29+
// parse result and check that correct index was built and it is visiable
30+
// see example output here: https://dev.mysql.com/doc/refman/8.0/en/show-index.html
31+
lines := strings.Split(r.Stdout(), "\n")
32+
for _, line := range lines {
33+
cols := strings.Split(line, "|")
34+
if len(cols) < 15 {
35+
continue
36+
}
37+
if strings.TrimSpace(cols[1]) != table {
38+
continue
39+
}
40+
if strings.TrimSpace(cols[3]) != name {
41+
continue
42+
}
43+
44+
if strings.TrimSpace(cols[14]) == "YES" {
45+
return true
46+
}
47+
return false
48+
}
49+
50+
return false
51+
}
52+
53+
type commandSuite interface {
54+
RunCommand(cmd string, args ...string) *icmd.Result
55+
Require() *require.Assertions
56+
}

cmdtests/sql_test.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,6 @@ func (s *SQLTestSuite) TestIndexesWorkdirChange() {
335335
r = s.RunCommand("sql", "CREATE INDEX repo_idx ON repositories USING pilosa (repository_id)")
336336
require.NoError(err, r.Stdout())
337337

338-
time.Sleep(1 * time.Second) // wait for index to be built
339-
340338
s.testQueryWithIndex(require, "repos", true)
341339

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

366362
s.testQueryWithIndex(require, "repos", true)
367363
}
368364

369365
func (s *SQLTestSuite) testQueryWithIndex(require *require.Assertions, repo string, hasIndex bool) {
370-
r := s.RunCommand("sql", "SHOW INDEX FROM repositories")
371-
require.NoError(r.Error, r.Combined())
372-
373366
if hasIndex {
374-
// parse result and check that correct index was built and it is visiable
375-
indexLine := strings.Split(r.Stdout(), "\n")[3]
376-
expected := `repositories.repository_id`
377-
require.Contains(indexLine, expected)
378-
visibleValue := strings.TrimSpace(strings.Split(indexLine, "|")[14])
379-
require.Equal("YES", visibleValue)
367+
require.True(cmdtests.IndexIsVisible(s, "repositories", "repo_idx"))
380368
}
381369

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

0 commit comments

Comments
 (0)