2018-07-19 23:57:22 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-10-25 17:11:50 +01:00
|
|
|
package node
|
2018-07-19 15:48:08 +01:00
|
|
|
|
|
|
|
import (
|
2018-10-08 16:09:37 +01:00
|
|
|
"sync"
|
2018-07-19 15:48:08 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestFoo struct {
|
|
|
|
called string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
cases := []struct {
|
2018-10-08 16:09:37 +01:00
|
|
|
pool *ConnectionPool
|
2018-07-19 15:48:08 +01:00
|
|
|
key string
|
|
|
|
expected TestFoo
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
2018-10-08 16:09:37 +01:00
|
|
|
pool: func() *ConnectionPool {
|
|
|
|
p := NewConnectionPool()
|
2018-10-25 17:11:50 +01:00
|
|
|
assert.NoError(t, p.Add("foo", TestFoo{called: "hoot"}))
|
2018-10-08 16:09:37 +01:00
|
|
|
return p
|
|
|
|
}(),
|
2018-07-19 15:48:08 +01:00
|
|
|
key: "foo",
|
|
|
|
expected: TestFoo{called: "hoot"},
|
|
|
|
expectedError: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-22 07:39:57 +01:00
|
|
|
for i := range cases {
|
|
|
|
v := &cases[i]
|
2018-10-25 17:11:50 +01:00
|
|
|
test, err := v.pool.Get(v.key)
|
2018-07-19 15:48:08 +01:00
|
|
|
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
|
|
|
|
}{
|
|
|
|
{
|
2018-10-08 16:09:37 +01:00
|
|
|
pool: ConnectionPool{
|
|
|
|
mu: sync.RWMutex{},
|
|
|
|
cache: map[string]interface{}{}},
|
2018-07-19 15:48:08 +01:00
|
|
|
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-10-25 17:11:50 +01:00
|
|
|
err := v.pool.Add(v.key, v.value)
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.Equal(t, v.expectedError, err)
|
|
|
|
|
2018-10-25 17:11:50 +01:00
|
|
|
test, err := v.pool.Get(v.key)
|
2018-07-19 15:48:08 +01:00
|
|
|
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
|
|
|
|
}{
|
|
|
|
{
|
2018-10-08 16:09:37 +01:00
|
|
|
pool: ConnectionPool{
|
|
|
|
mu: sync.RWMutex{},
|
|
|
|
cache: map[string]interface{}{"foo": TestFoo{called: "hoot"}}},
|
2018-07-19 15:48:08 +01:00
|
|
|
key: "foo",
|
|
|
|
expected: nil,
|
|
|
|
expectedError: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-22 07:39:57 +01:00
|
|
|
for i := range cases {
|
|
|
|
v := &cases[i]
|
2018-10-25 17:11:50 +01:00
|
|
|
err := v.pool.Remove(v.key)
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.Equal(t, v.expectedError, err)
|
|
|
|
|
2018-10-25 17:11:50 +01:00
|
|
|
test, err := v.pool.Get(v.key)
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.Equal(t, v.expectedError, err)
|
|
|
|
|
|
|
|
assert.Equal(t, v.expected, test)
|
|
|
|
}
|
|
|
|
}
|