Skip to content

Commit

Permalink
Backfill some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ingar committed Jul 24, 2020
1 parent dab4950 commit 54ce6f1
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 7 deletions.
6 changes: 3 additions & 3 deletions docs/retrievalclient.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ stateDiagram-v2
19 --> 15 : ClientEventComplete
8 --> 17 : <invalid Value>

note left of 6 : The following events only record in this state.<br><br>ClientEventLastPaymentRequested<br>ClientEventPaymentRequested<br>ClientEventAllBlocksReceived<br>ClientEventBlocksReceived


note left of 3 : The following events only record in this state.<br><br>ClientEventLastPaymentRequested<br>ClientEventPaymentRequested<br>ClientEventAllBlocksReceived<br>ClientEventBlocksReceived


Expand All @@ -87,3 +84,6 @@ stateDiagram-v2

note left of 5 : The following events only record in this state.<br><br>ClientEventLastPaymentRequested<br>ClientEventPaymentRequested<br>ClientEventAllBlocksReceived<br>ClientEventBlocksReceived


note left of 6 : The following events only record in this state.<br><br>ClientEventLastPaymentRequested<br>ClientEventPaymentRequested<br>ClientEventAllBlocksReceived<br>ClientEventBlocksReceived

6 changes: 3 additions & 3 deletions docs/retrievalclient.mmd.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion retrievalmarket/storage_retrieval_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func newStorageHarness(ctx context.Context, t *testing.T) *storageHarness {
require.NoError(t, dt2.RegisterVoucherType(&requestvalidation.StorageDataTransferVoucher{}, rv2))
storedAsk, err := storedask.NewStoredAsk(td.Ds2, datastore.NewKey("latest-ask"), providerNode, providerAddr)
require.NoError(t, err)
providerDealFunds, err := funds.NewDealFunds(td.Ds1, datastore.NewKey("storage/client/dealfunds"))
providerDealFunds, err := funds.NewDealFunds(td.Ds1, datastore.NewKey("storage/provider/dealfunds"))
require.NoError(t, err)

provider, err := stormkt.NewProvider(
Expand Down
4 changes: 4 additions & 0 deletions shared_testutil/test_deal_funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type TestDealFunds struct {
ReleaseCalls []abi.TokenAmount
}

func (f *TestDealFunds) Get() abi.TokenAmount {
return f.reserved
}

func (f *TestDealFunds) Reserve(amount abi.TokenAmount) (abi.TokenAmount, error) {
f.reserved = big.Add(f.reserved, amount)
f.ReserveCalls = append(f.ReserveCalls, amount)
Expand Down
7 changes: 7 additions & 0 deletions storagemarket/impl/clientstates/client_states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func TestEnsureFunds(t *testing.T) {
runAndInspect(t, storagemarket.StorageDealEnsureClientFunds, clientstates.EnsureClientFunds, testCase{
inspector: func(deal storagemarket.ClientDeal, env *fakeEnvironment) {
tut.AssertDealState(t, storagemarket.StorageDealFundsEnsured, deal.State)
assert.Equal(t, env.dealFunds.ReserveCalls[0], deal.Proposal.ClientBalanceRequirement())
assert.Len(t, env.dealFunds.ReleaseCalls, 0)
},
})
})
Expand All @@ -44,6 +46,8 @@ func TestEnsureFunds(t *testing.T) {
nodeParams: nodeParams{AddFundsCid: tut.GenerateCids(1)[0]},
inspector: func(deal storagemarket.ClientDeal, env *fakeEnvironment) {
tut.AssertDealState(t, storagemarket.StorageDealClientFunding, deal.State)
assert.Equal(t, env.dealFunds.ReserveCalls[0], deal.Proposal.ClientBalanceRequirement())
assert.Len(t, env.dealFunds.ReleaseCalls, 0)
},
})
})
Expand All @@ -55,6 +59,8 @@ func TestEnsureFunds(t *testing.T) {
inspector: func(deal storagemarket.ClientDeal, env *fakeEnvironment) {
tut.AssertDealState(t, storagemarket.StorageDealFailing, deal.State)
assert.Equal(t, "adding market funds failed: Something went wrong", deal.Message)
assert.Equal(t, env.dealFunds.ReserveCalls[0], deal.Proposal.ClientBalanceRequirement())
assert.Equal(t, env.dealFunds.ReleaseCalls[0], deal.Proposal.ClientBalanceRequirement())
},
})
})
Expand Down Expand Up @@ -343,6 +349,7 @@ func TestValidateDealPublished(t *testing.T) {
inspector: func(deal storagemarket.ClientDeal, env *fakeEnvironment) {
tut.AssertDealState(t, storagemarket.StorageDealSealing, deal.State)
assert.Equal(t, abi.DealID(5), deal.DealID)
assert.Equal(t, env.dealFunds.ReleaseCalls[0], deal.Proposal.ClientBalanceRequirement())
},
})
})
Expand Down
7 changes: 7 additions & 0 deletions storagemarket/impl/funds/funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (

// DealFunds is used to track funds needed for (possibly multiple) deals in progress
type DealFunds interface {
// returns the current amount tracked
Get() abi.TokenAmount

// Reserve is used to mark funds as "in-use" for a deal
// returns the new amount tracked
Reserve(amount abi.TokenAmount) (abi.TokenAmount, error)
Expand Down Expand Up @@ -48,6 +51,10 @@ func NewDealFunds(ds datastore.Batching, key datastore.Key) (DealFunds, error) {
return df, nil
}

func (f *dealFundsImpl) Get() abi.TokenAmount {
return f.reserved
}

func (f *dealFundsImpl) Reserve(amount abi.TokenAmount) (abi.TokenAmount, error) {
f.lock.Lock()
defer f.lock.Unlock()
Expand Down
48 changes: 48 additions & 0 deletions storagemarket/impl/funds/funds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package funds_test

import (
"testing"

"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/specs-actors/actors/abi/big"
"github.com/ipfs/go-datastore"
dss "github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/assert"

"github.com/filecoin-project/go-fil-markets/storagemarket/impl/funds"
)

func TestDealFunds(t *testing.T) {
ds := dss.MutexWrap(datastore.NewMapDatastore())
key := datastore.NewKey("deal_funds_test")

f, err := funds.NewDealFunds(ds, key)
assert.NoError(t, err)

// initializes to zero
assert.Equal(t, f.Get(), big.Zero())

// reserve funds and return new total
newAmount, err := f.Reserve(abi.NewTokenAmount(123))
assert.NoError(t, err)
assert.Equal(t, abi.NewTokenAmount(123), newAmount)
assert.Equal(t, abi.NewTokenAmount(123), f.Get())

// reserve more funds and return new total
newAmount, err = f.Reserve(abi.NewTokenAmount(100))
assert.NoError(t, err)
assert.Equal(t, abi.NewTokenAmount(223), newAmount)
assert.Equal(t, abi.NewTokenAmount(223), f.Get())

// release funds and return new total
newAmount, err = f.Release(abi.NewTokenAmount(123))
assert.NoError(t, err)
assert.Equal(t, abi.NewTokenAmount(100), newAmount)
assert.Equal(t, abi.NewTokenAmount(100), f.Get())

// creating new funds will read stored value
f, err = funds.NewDealFunds(ds, key)
assert.NoError(t, err)
assert.Equal(t, abi.NewTokenAmount(100), newAmount)
assert.Equal(t, abi.NewTokenAmount(100), f.Get())
}
6 changes: 6 additions & 0 deletions storagemarket/impl/providerstates/provider_states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ func TestEnsureProviderFunds(t *testing.T) {
"succeeds immediately": {
dealInspector: func(t *testing.T, deal storagemarket.MinerDeal, env *fakeEnvironment) {
tut.AssertDealState(t, storagemarket.StorageDealPublish, deal.State)
require.Equal(t, env.dealFunds.ReserveCalls[0], deal.Proposal.ProviderBalanceRequirement())
require.Len(t, env.dealFunds.ReleaseCalls, 0)
},
},
"succeeds by sending an AddBalance message": {
Expand All @@ -350,6 +352,8 @@ func TestEnsureProviderFunds(t *testing.T) {
dealInspector: func(t *testing.T, deal storagemarket.MinerDeal, env *fakeEnvironment) {
tut.AssertDealState(t, storagemarket.StorageDealProviderFunding, deal.State)
require.Equal(t, &cids[0], deal.AddFundsCid)
require.Equal(t, env.dealFunds.ReserveCalls[0], deal.Proposal.ProviderBalanceRequirement())
require.Len(t, env.dealFunds.ReleaseCalls, 0)
},
},
"get miner worker fails": {
Expand All @@ -368,6 +372,8 @@ func TestEnsureProviderFunds(t *testing.T) {
dealInspector: func(t *testing.T, deal storagemarket.MinerDeal, env *fakeEnvironment) {
tut.AssertDealState(t, storagemarket.StorageDealFailing, deal.State)
require.Equal(t, "error calling node: ensuring funds: not enough funds", deal.Message)
require.Equal(t, env.dealFunds.ReserveCalls[0], deal.Proposal.ProviderBalanceRequirement())
require.Equal(t, env.dealFunds.ReleaseCalls[0], deal.Proposal.ProviderBalanceRequirement())
},
},
}
Expand Down

0 comments on commit 54ce6f1

Please sign in to comment.