Skip to content

Catch errors creating new iteratos in rowReaders #221

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

Merged
merged 2 commits into from
Apr 13, 2018
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
13 changes: 12 additions & 1 deletion repository_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,18 @@ func (i *rowRepoIter) rowReader(num int) {
defer i.wg.Done()

for repo := range i.repos {
iter, _ := i.iter.NewIterator(repo)
iter, err := i.iter.NewIterator(repo)
if err != nil {
// guard from possible previous error
select {
case <-i.done:
return
default:
i.setError(err)
close(i.done)
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to continue to the next loop iteration. Otherwise, we'll still get a panic because of the iter == nil and never get the error.

continue
}
}

loop:
for {
Expand Down
48 changes: 48 additions & 0 deletions repository_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package gitbase

import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"
"gopkg.in/src-d/go-git-fixtures.v3"
Expand Down Expand Up @@ -263,3 +265,49 @@ func TestRepositoryPoolAddDir(t *testing.T) {

require.ElementsMatch(arrayExpected, arrayID)
}

var errIter = fmt.Errorf("Error iter")

type testErrorIter struct{}

func (d *testErrorIter) NewIterator(
repo *Repository,
) (RowRepoIter, error) {
return nil, errIter
// return &testErrorIter{}, nil
}

func (d *testErrorIter) Next() (sql.Row, error) {
return nil, io.EOF
}

func (d *testErrorIter) Close() error {
return nil
}

func TestRepositoryErrorIter(t *testing.T) {
require := require.New(t)

path := fixtures.Basic().ByTag("worktree").One().Worktree().Root()
pool := NewRepositoryPool()
pool.Add("one", path)

timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)

ctx := sql.NewContext(timeout, sql.WithSession(NewSession(&pool)))
eIter := &testErrorIter{}

repoIter, err := NewRowRepoIter(ctx, eIter)
require.NoError(err)

go func() {
repoIter.Next()
}()

select {
case <-repoIter.done:
require.Equal(errIter, repoIter.err)
}

cancel()
}