storj/pkg/pool/connection_pool_test.go
Dennis Coyle dee2c137c8
Remove BKAD dependency from pkg/kademlia (#294)
* slowly but surely

* hardcode ID for tests so we can get predictable results

* skipping bad test

* removing tests of bkad

* wip

* new algorithm for worker

* clean up

* remove skipped test

* changes

* uncomment

* fixed conflicts

* maybe done ?

* cleanup

* boot bkad

* wip

* cleanup

* undo change

* fixes

* wip

* wip

* moving nodeID around

* wip

* wip

* fixes

* fixes after merge

* added TODO

* fixed tests post identity

* linter fixes

* wip

* PR review comments

* wip

* fixing tests

* fix tests

* force db directory

* bad test

* fixes race condition

* small cleanups

* adding db folder

* testing

* wip

* cleanup

* cleanup

* linters

* export Restrict

* add timeout

* testing

* linters

* forgot one

* moar fixes from master merge

* PR comments

* moar PR comments

* removed stun flag

* remove duplicate declaration

* remove old tests

* remove timeout

* fix tests

* missed one

* changed StringToID >> IDFromString

* PR comments

* stupid linter

* moevd overlay mock

* fixed merge conflicts

* fixes

* linter
2018-10-08 11:09:37 -04:00

105 lines
2.1 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package pool
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
type TestFoo struct {
called string
}
func TestGet(t *testing.T) {
ctx := context.Background()
cases := []struct {
pool *ConnectionPool
key string
expected TestFoo
expectedError error
}{
{
pool: func() *ConnectionPool {
p := NewConnectionPool()
assert.NoError(t, p.Add(ctx, "foo", TestFoo{called: "hoot"}))
return p
}(),
key: "foo",
expected: TestFoo{called: "hoot"},
expectedError: nil,
},
}
for i := range cases {
v := &cases[i]
test, err := v.pool.Get(ctx, 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{
mu: sync.RWMutex{},
cache: map[string]interface{}{}},
key: "foo",
value: TestFoo{called: "hoot"},
expected: TestFoo{called: "hoot"},
expectedError: nil,
},
}
for i := range cases {
v := &cases[i]
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{
mu: sync.RWMutex{},
cache: map[string]interface{}{"foo": TestFoo{called: "hoot"}}},
key: "foo",
expected: nil,
expectedError: nil,
},
}
for i := range cases {
v := &cases[i]
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)
}
}