-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* hide channels manipulations from pool, make api synced * suppress linter * add cursor tests * add comments
- Loading branch information
Showing
6 changed files
with
287 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package pool | ||
|
||
import "context" | ||
|
||
// Cursor provides synchronous access to async data from pool's response channel | ||
type Cursor struct { | ||
ch chan response | ||
err error | ||
} | ||
|
||
// Next returns next result from the cursor, ok = false on completion. | ||
// Any error saved internally and can be returned by Err call | ||
func (c *Cursor) Next(ctx context.Context, v *interface{}) bool { | ||
for { | ||
select { | ||
case resp, ok := <-c.ch: | ||
if !ok { | ||
return false | ||
} | ||
if resp.err != nil { | ||
c.err = resp.err | ||
continue | ||
} | ||
*v = resp.value | ||
return ok | ||
case <-ctx.Done(): | ||
c.err = ctx.Err() | ||
return false | ||
} | ||
} | ||
} | ||
|
||
// All gets all data from the cursor | ||
func (c *Cursor) All(ctx context.Context) (res []interface{}, err error) { | ||
var v interface{} | ||
for c.Next(ctx, &v) { | ||
res = append(res, v) | ||
} | ||
return res, c.err | ||
} | ||
|
||
// Err returns error collected by Next | ||
func (c *Cursor) Err() error { return c.err } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package pool | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCursor_Next(t *testing.T) { | ||
c := Cursor{ch: make(chan response, 3)} | ||
|
||
c.ch <- response{value: "12345"} | ||
c.ch <- response{value: "abc"} | ||
c.ch <- response{value: "xyz 0987"} | ||
close(c.ch) | ||
|
||
var v interface{} | ||
next := c.Next(context.Background(), &v) | ||
assert.True(t, next) | ||
assert.Equal(t, "12345", v.(string)) | ||
|
||
next = c.Next(context.Background(), &v) | ||
assert.True(t, next) | ||
assert.Equal(t, "abc", v.(string)) | ||
|
||
next = c.Next(context.Background(), &v) | ||
assert.True(t, next) | ||
assert.Equal(t, "xyz 0987", v.(string)) | ||
|
||
next = c.Next(context.Background(), &v) | ||
assert.False(t, next) | ||
} | ||
|
||
func TestCursor_All(t *testing.T) { | ||
c := Cursor{ch: make(chan response, 3)} | ||
|
||
c.ch <- response{value: "12345"} | ||
c.ch <- response{value: "abc"} | ||
c.ch <- response{value: "xyz 0987"} | ||
close(c.ch) | ||
|
||
res, err := c.All(context.Background()) | ||
require.NoError(t, err) | ||
assert.Len(t, res, 3) | ||
|
||
res, err = c.All(context.Background()) | ||
require.NoError(t, err) | ||
assert.Len(t, res, 0) | ||
} | ||
|
||
func TestCursor_AllWithError(t *testing.T) { | ||
c := Cursor{ch: make(chan response, 3)} | ||
|
||
c.ch <- response{value: "12345"} | ||
c.ch <- response{value: "abc", err: errors.New("failed")} | ||
c.ch <- response{value: "xyz 0987"} | ||
close(c.ch) | ||
|
||
res, err := c.All(context.Background()) | ||
require.EqualError(t, err, "failed") | ||
assert.Len(t, res, 2) | ||
|
||
res, err = c.All(context.Background()) | ||
require.EqualError(t, err, "failed") | ||
assert.Len(t, res, 0) | ||
} |
Oops, something went wrong.