Skip to content

Commit 6b08246

Browse files
committed
GODRIVER-298: support nil context to next method
Change-Id: Ia4005ae06ec7ae7e7525c5745a85e5ad77a058be
1 parent 634eb62 commit 6b08246

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

core/topology/cursor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ func (c *cursor) ID() int64 {
7979
}
8080

8181
func (c *cursor) Next(ctx context.Context) bool {
82+
if ctx == nil {
83+
ctx = context.Background()
84+
}
85+
8286
c.current++
8387
if c.current < c.batch.Len() {
8488
return true

core/topology/cursor_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package topology
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mongodb/mongo-go-driver/bson"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestCursorNextDoesNotPanicIfContextisNil(t *testing.T) {
11+
// all collection/cursor iterators should take contexts, but
12+
// permit passing nils for contexts, which should not
13+
// panic.
14+
//
15+
// While more through testing might be ideal this check
16+
// prevents a regression of GODRIVER-298
17+
18+
c := cursor{batch: bson.NewArray(bson.VC.String("a"), bson.VC.String("b"))}
19+
20+
var iterNext bool
21+
assert.NotPanics(t, func() {
22+
iterNext = c.Next(nil)
23+
})
24+
assert.True(t, iterNext)
25+
}

0 commit comments

Comments
 (0)