Refactor connection listing for tests (#1224)
This commit is contained in:
1 parent
ea611b4f6a
commit
b47b71d26c
3 files changed
+13
-18
No files matched your search
@@ -52,8 +52,9 @@ func NewRepository(r db.Reader, w db.Writer, kms *kms.Kms, opt ...Option) (*Repo
|
|||||||
|
|
||||||
// list will return a listing of resources and honor the WithLimit option or the
|
// list will return a listing of resources and honor the WithLimit option or the
|
||||||
// repo defaultLimit. Supports WithOrder option.
|
// repo defaultLimit. Supports WithOrder option.
|
||||||
func (r *Repository) list(ctx context.Context, resources interface{}, where string, args []interface{}, opts options) error {
|
func (r *Repository) list(ctx context.Context, resources interface{}, where string, args []interface{}, opt ...Option) error {
|
||||||
const op = "session.(Repository).list"
|
const op = "session.(Repository).list"
|
||||||
|
opts := getOpts(opt...)
|
||||||
limit := r.defaultLimit
|
limit := r.defaultLimit
|
||||||
var dbOpts []db.Option
|
var dbOpts []db.Option
|
||||||
if opts.withLimit != 0 {
|
if opts.withLimit != 0 {
|
||||||
|
|||||||
@@ -44,21 +44,15 @@ func (r *Repository) LookupConnection(ctx context.Context, connectionId string,
|
|||||||
return &connection, states, nil
|
return &connection, states, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListConnections will connections by session ID. If session ID is *, all
|
// ListConnectionsBySessionId will list connections by session ID. Supports the
|
||||||
// connections are returned. Supports the WithLimit and WithOrder options.
|
// WithLimit and WithOrder options.
|
||||||
func (r *Repository) ListConnectionsBySessionId(ctx context.Context, sessionId string, opt ...Option) ([]*Connection, error) {
|
func (r *Repository) ListConnectionsBySessionId(ctx context.Context, sessionId string, opt ...Option) ([]*Connection, error) {
|
||||||
const op = "session.(Repository).ListConnections"
|
const op = "session.(Repository).ListConnectionsBySessionId"
|
||||||
opts := getOpts(opt...)
|
if sessionId == "" {
|
||||||
var connections []*Connection
|
return nil, errors.New(errors.InvalidParameter, op, "no session ID supplied")
|
||||||
var args []interface{}
|
|
||||||
var where string
|
|
||||||
if sessionId != "*" {
|
|
||||||
where = "session_id = ?"
|
|
||||||
args = append(args, sessionId)
|
|
||||||
}
|
}
|
||||||
// The where clause will be ignored if args is empty, if we don't want to
|
var connections []*Connection
|
||||||
// scope to a specific session ID
|
err := r.list(ctx, &connections, "session_id = ?", []interface{}{sessionId}, opt...) // pass options, so WithLimit and WithOrder are supported
|
||||||
err := r.list(ctx, &connections, where, args, opts) // pass options, so WithLimit and WithOrder are supported
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, op)
|
return nil, errors.Wrap(err, op)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -292,8 +292,8 @@ func TestRepository_CloseDeadConnectionsOnWorkerReport(t *testing.T) {
|
|||||||
// expect all others to be open.
|
// expect all others to be open.
|
||||||
|
|
||||||
shouldBeClosed := worker2ConnIds[2:]
|
shouldBeClosed := worker2ConnIds[2:]
|
||||||
conns, err := repo.ListConnectionsBySessionId(ctx, "*")
|
var conns []*Connection
|
||||||
require.NoError(err)
|
require.NoError(repo.list(ctx, &conns, "", nil))
|
||||||
for _, conn := range conns {
|
for _, conn := range conns {
|
||||||
_, states, err := repo.LookupConnection(ctx, conn.PublicId)
|
_, states, err := repo.LookupConnection(ctx, conn.PublicId)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
@@ -315,8 +315,8 @@ func TestRepository_CloseDeadConnectionsOnWorkerReport(t *testing.T) {
|
|||||||
assert.Equal(6, count)
|
assert.Equal(6, count)
|
||||||
|
|
||||||
// We now expect all but those blessed few to be closed
|
// We now expect all but those blessed few to be closed
|
||||||
conns, err = repo.ListConnectionsBySessionId(ctx, "*")
|
conns = nil
|
||||||
require.NoError(err)
|
require.NoError(repo.list(ctx, &conns, "", nil))
|
||||||
for _, conn := range conns {
|
for _, conn := range conns {
|
||||||
_, states, err := repo.LookupConnection(ctx, conn.PublicId)
|
_, states, err := repo.LookupConnection(ctx, conn.PublicId)
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
|
|||||||
Reference in new issue
Block a user