Skip to content

Commit 4e19a4a

Browse files
authored
Merge pull request #221 from jfontan/fix/check-error-newiterator
Catch errors creating new iterators in rowReaders
2 parents 066354b + 3589daf commit 4e19a4a

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

repository_pool.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,18 @@ func (i *rowRepoIter) rowReader(num int) {
276276
defer i.wg.Done()
277277

278278
for repo := range i.repos {
279-
iter, _ := i.iter.NewIterator(repo)
279+
iter, err := i.iter.NewIterator(repo)
280+
if err != nil {
281+
// guard from possible previous error
282+
select {
283+
case <-i.done:
284+
return
285+
default:
286+
i.setError(err)
287+
close(i.done)
288+
continue
289+
}
290+
}
280291

281292
loop:
282293
for {

repository_pool_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package gitbase
22

33
import (
44
"context"
5+
"fmt"
56
"io"
67
"io/ioutil"
78
"os"
89
"path/filepath"
910
"strconv"
1011
"sync"
1112
"testing"
13+
"time"
1214

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

264266
require.ElementsMatch(arrayExpected, arrayID)
265267
}
268+
269+
var errIter = fmt.Errorf("Error iter")
270+
271+
type testErrorIter struct{}
272+
273+
func (d *testErrorIter) NewIterator(
274+
repo *Repository,
275+
) (RowRepoIter, error) {
276+
return nil, errIter
277+
// return &testErrorIter{}, nil
278+
}
279+
280+
func (d *testErrorIter) Next() (sql.Row, error) {
281+
return nil, io.EOF
282+
}
283+
284+
func (d *testErrorIter) Close() error {
285+
return nil
286+
}
287+
288+
func TestRepositoryErrorIter(t *testing.T) {
289+
require := require.New(t)
290+
291+
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root()
292+
pool := NewRepositoryPool()
293+
pool.Add("one", path)
294+
295+
timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
296+
297+
ctx := sql.NewContext(timeout, sql.WithSession(NewSession(&pool)))
298+
eIter := &testErrorIter{}
299+
300+
repoIter, err := NewRowRepoIter(ctx, eIter)
301+
require.NoError(err)
302+
303+
go func() {
304+
repoIter.Next()
305+
}()
306+
307+
select {
308+
case <-repoIter.done:
309+
require.Equal(errIter, repoIter.err)
310+
}
311+
312+
cancel()
313+
}

0 commit comments

Comments
 (0)