Add tests
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
parent
195cf4f84d
commit
2b62bb6eab
@ -4,11 +4,12 @@ import (
|
||||
"crypto/rand"
|
||||
"sync"
|
||||
|
||||
"github.com/filecoin-project/go-lotus/node/config"
|
||||
"github.com/ipfs/go-datastore"
|
||||
dssync "github.com/ipfs/go-datastore/sync"
|
||||
crypto "github.com/libp2p/go-libp2p-crypto"
|
||||
"github.com/libp2p/go-libp2p-core/crypto"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
|
||||
"github.com/filecoin-project/go-lotus/node/config"
|
||||
)
|
||||
|
||||
type MemRepo struct {
|
||||
@ -35,6 +36,7 @@ type lockedMemRepo struct {
|
||||
|
||||
var _ Repo = &MemRepo{}
|
||||
|
||||
// MemRepoOptions contains options for memory repo
|
||||
type MemRepoOptions struct {
|
||||
ds datastore.Datastore
|
||||
configF func() *config.Root
|
||||
@ -42,6 +44,9 @@ type MemRepoOptions struct {
|
||||
wallet interface{}
|
||||
}
|
||||
|
||||
// NewMemory creates new memory based repo with provided options.
|
||||
// opts can be nil will be replaced with default
|
||||
// any filed in opts can be nil, it will be replaced by defaults
|
||||
func NewMemory(opts *MemRepoOptions) *MemRepo {
|
||||
if opts == nil {
|
||||
opts = &MemRepoOptions{}
|
||||
@ -117,6 +122,7 @@ func (lmem *lockedMemRepo) Close() error {
|
||||
lmem.mem.api.Lock()
|
||||
lmem.mem.api.ma = nil
|
||||
lmem.mem.api.Unlock()
|
||||
<-lmem.mem.repoLock // unlock
|
||||
return nil
|
||||
|
||||
}
|
||||
|
57
node/repo/memrepo_test.go
Normal file
57
node/repo/memrepo_test.go
Normal file
@ -0,0 +1,57 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMemRepo(t *testing.T) {
|
||||
repo := NewMemory(nil)
|
||||
apima, err := repo.APIEndpoint()
|
||||
if assert.Error(t, err) {
|
||||
assert.Equal(t, ErrNoAPIEndpoint, err)
|
||||
}
|
||||
assert.Nil(t, apima, "with no api endpoint, return should be nil")
|
||||
|
||||
lrepo, err := repo.Lock()
|
||||
assert.NoError(t, err, "should be able to lock once")
|
||||
assert.NotNil(t, lrepo, "locked repo shouldn't be nil")
|
||||
|
||||
{
|
||||
lrepo2, err := repo.Lock()
|
||||
if assert.Error(t, err) {
|
||||
assert.Equal(t, ErrRepoAlreadyLocked, err)
|
||||
}
|
||||
assert.Nil(t, lrepo2, "with locked repo errors, nil should be returned")
|
||||
}
|
||||
|
||||
err = lrepo.Close()
|
||||
assert.NoError(t, err, "should be able to unlock")
|
||||
|
||||
lrepo, err = repo.Lock()
|
||||
assert.NoError(t, err, "should be able to relock")
|
||||
assert.NotNil(t, lrepo, "locked repo shouldn't be nil")
|
||||
|
||||
ma, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/43244")
|
||||
assert.NoError(t, err, "creating multiaddr shouldn't error")
|
||||
|
||||
err = lrepo.SetAPIEndpoint(ma)
|
||||
assert.NoError(t, err, "setting multiaddr shouldn't error")
|
||||
|
||||
apima, err = repo.APIEndpoint()
|
||||
assert.NoError(t, err, "setting multiaddr shouldn't error")
|
||||
assert.Equal(t, ma, apima, "returned API multiaddr should be the same")
|
||||
|
||||
err = lrepo.Close()
|
||||
assert.NoError(t, err, "should be able to close")
|
||||
|
||||
apima, err = repo.APIEndpoint()
|
||||
|
||||
if assert.Error(t, err) {
|
||||
assert.Equal(t, ErrNoAPIEndpoint, err, "after closing repo, api should be nil")
|
||||
}
|
||||
assert.Nil(t, apima, "with closed repo, apima should be set back to nil")
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user