2018-07-19 23:57:22 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestFoo struct {
|
|
|
|
called string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
pool ConnectionPool
|
|
|
|
key string
|
|
|
|
expected TestFoo
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pool: ConnectionPool{cache: map[string]interface{}{"foo": TestFoo{called: "hoot"}}},
|
|
|
|
key: "foo",
|
|
|
|
expected: TestFoo{called: "hoot"},
|
|
|
|
expectedError: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-22 07:39:57 +01:00
|
|
|
for i := range cases {
|
|
|
|
v := &cases[i]
|
2018-07-19 15:48:08 +01:00
|
|
|
test, err := v.pool.Get(context.Background(), v.key)
|
|
|
|
assert.Equal(t, v.expectedError, err)
|
|
|
|
assert.Equal(t, v.expected, test)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdd(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
pool ConnectionPool
|
|
|
|
key string
|
|
|
|
value TestFoo
|
|
|
|
expected TestFoo
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pool: ConnectionPool{cache: map[string]interface{}{}},
|
|
|
|
key: "foo",
|
|
|
|
value: TestFoo{called: "hoot"},
|
|
|
|
expected: TestFoo{called: "hoot"},
|
|
|
|
expectedError: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-22 07:39:57 +01:00
|
|
|
for i := range cases {
|
|
|
|
v := &cases[i]
|
2018-07-19 15:48:08 +01:00
|
|
|
err := v.pool.Add(context.Background(), v.key, v.value)
|
|
|
|
assert.Equal(t, v.expectedError, err)
|
|
|
|
|
|
|
|
test, err := v.pool.Get(context.Background(), v.key)
|
|
|
|
assert.Equal(t, v.expectedError, err)
|
|
|
|
|
|
|
|
assert.Equal(t, v.expected, test)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
pool ConnectionPool
|
|
|
|
key string
|
|
|
|
expected interface{}
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pool: ConnectionPool{cache: map[string]interface{}{"foo": TestFoo{called: "hoot"}}},
|
|
|
|
key: "foo",
|
|
|
|
expected: nil,
|
|
|
|
expectedError: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-22 07:39:57 +01:00
|
|
|
for i := range cases {
|
|
|
|
v := &cases[i]
|
2018-07-19 15:48:08 +01:00
|
|
|
err := v.pool.Remove(context.Background(), v.key)
|
|
|
|
assert.Equal(t, v.expectedError, err)
|
|
|
|
|
|
|
|
test, err := v.pool.Get(context.Background(), v.key)
|
|
|
|
assert.Equal(t, v.expectedError, err)
|
|
|
|
|
|
|
|
assert.Equal(t, v.expected, test)
|
|
|
|
}
|
|
|
|
}
|