Skip to content

Commit

Permalink
test(retrievalmarket): provide context for numbers
Browse files Browse the repository at this point in the history
make clear what numbers expected in tests of payment processing mean for retrieval deals
  • Loading branch information
hannahhoward committed Jan 14, 2020
1 parent 35e497f commit eec7871
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
32 changes: 18 additions & 14 deletions retrievalmarket/impl/clientstates/client_states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ func TestProcessPaymentRequested(t *testing.T) {
f(dealState)
require.Empty(t, dealState.Message)
require.Equal(t, dealState.PaymentRequested, tokenamount.FromInt(0))
require.Equal(t, dealState.FundsSpent, tokenamount.FromInt(3000000))
require.Equal(t, dealState.BytesPaidFor, uint64(6000))
require.Equal(t, dealState.CurrentInterval, uint64(1500))
require.Equal(t, dealState.FundsSpent, tokenamount.Add(defaultFundsSpent, defaultPaymentRequested))
require.Equal(t, dealState.BytesPaidFor, defaultTotalReceived)
require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval+defaultIntervalIncrease)
require.Equal(t, dealState.Status, retrievalmarket.DealStatusOngoing)
})

Expand All @@ -217,9 +217,9 @@ func TestProcessPaymentRequested(t *testing.T) {
f(dealState)
require.Empty(t, dealState.Message)
require.Equal(t, dealState.PaymentRequested, tokenamount.FromInt(0))
require.Equal(t, dealState.FundsSpent, tokenamount.FromInt(3000000))
require.Equal(t, dealState.BytesPaidFor, uint64(6000))
require.Equal(t, dealState.CurrentInterval, uint64(1500))
require.Equal(t, dealState.FundsSpent, tokenamount.Add(defaultFundsSpent, defaultPaymentRequested))
require.Equal(t, dealState.BytesPaidFor, defaultTotalReceived)
require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval+defaultIntervalIncrease)
require.Equal(t, dealState.Status, retrievalmarket.DealStatusCompleted)
})

Expand Down Expand Up @@ -250,7 +250,8 @@ func TestProcessPaymentRequested(t *testing.T) {
t.Run("more bytes since last payment than interval works, can charge more", func(t *testing.T) {
dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded)
dealState.BytesPaidFor = defaultBytesPaidFor - 500
dealState.PaymentRequested = tokenamount.FromInt(750000)
largerPaymentRequested := tokenamount.FromInt(750000)
dealState.PaymentRequested = largerPaymentRequested

fe := environment(testnet.TestDealStreamParams{}, testnodes.TestRetrievalClientNodeParams{
Voucher: testVoucher,
Expand All @@ -259,9 +260,9 @@ func TestProcessPaymentRequested(t *testing.T) {
f(dealState)
require.Empty(t, dealState.Message)
require.Equal(t, dealState.PaymentRequested, tokenamount.FromInt(0))
require.Equal(t, dealState.FundsSpent, tokenamount.FromInt(3250000))
require.Equal(t, dealState.BytesPaidFor, uint64(6000))
require.Equal(t, dealState.CurrentInterval, uint64(1500))
require.Equal(t, dealState.FundsSpent, tokenamount.Add(defaultFundsSpent, largerPaymentRequested))
require.Equal(t, dealState.BytesPaidFor, defaultTotalReceived)
require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval+defaultIntervalIncrease)
require.Equal(t, dealState.Status, retrievalmarket.DealStatusOngoing)
})

Expand All @@ -279,17 +280,20 @@ func TestProcessPaymentRequested(t *testing.T) {

t.Run("too little payment requested works but records correctly", func(t *testing.T) {
dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded)
dealState.PaymentRequested = tokenamount.FromInt(250000)
smallerPaymentRequested := tokenamount.FromInt(250000)
dealState.PaymentRequested = smallerPaymentRequested
fe := environment(testnet.TestDealStreamParams{}, testnodes.TestRetrievalClientNodeParams{
Voucher: testVoucher,
})
f := clientstates.ProcessPaymentRequested(ctx, fe, *dealState)
f(dealState)
require.Empty(t, dealState.Message)
require.Equal(t, dealState.PaymentRequested, tokenamount.FromInt(0))
require.Equal(t, dealState.FundsSpent, tokenamount.FromInt(2750000))
require.Equal(t, dealState.BytesPaidFor, uint64(5500))
require.Equal(t, dealState.CurrentInterval, uint64(1000))
require.Equal(t, dealState.FundsSpent, tokenamount.Add(defaultFundsSpent, smallerPaymentRequested))
// only records change for those bytes paid for
require.Equal(t, dealState.BytesPaidFor, defaultBytesPaidFor+500)
// no interval increase
require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval)
require.Equal(t, dealState.Status, retrievalmarket.DealStatusOngoing)
})

Expand Down
42 changes: 22 additions & 20 deletions retrievalmarket/impl/providerstates/provider_states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestSendBlocks(t *testing.T) {
dealState := makeDealState(retrievalmarket.DealStatusAccepted)
expectedDealResponse := retrievalmarket.DealResponse{
Status: retrievalmarket.DealStatusFundsNeeded,
PaymentOwed: tokenamount.FromInt(500000),
PaymentOwed: defaultPaymentPerInterval,
Blocks: blocks,
ID: dealState.ID,
}
Expand All @@ -168,7 +168,7 @@ func TestSendBlocks(t *testing.T) {
f := providerstates.SendBlocks(ctx, fe, *dealState)
f(dealState)
require.Equal(t, dealState.Status, retrievalmarket.DealStatusFundsNeeded)
require.Equal(t, dealState.TotalSent, uint64(6000))
require.Equal(t, dealState.TotalSent, defaultTotalSent+defaultCurrentInterval)
require.Empty(t, dealState.Message)
})

Expand All @@ -177,7 +177,7 @@ func TestSendBlocks(t *testing.T) {
dealState := makeDealState(retrievalmarket.DealStatusAccepted)
expectedDealResponse := retrievalmarket.DealResponse{
Status: retrievalmarket.DealStatusFundsNeededLastPayment,
PaymentOwed: tokenamount.FromInt(500000),
PaymentOwed: defaultPaymentPerInterval,
Blocks: blocks,
ID: dealState.ID,
}
Expand All @@ -187,7 +187,7 @@ func TestSendBlocks(t *testing.T) {
f := providerstates.SendBlocks(ctx, fe, *dealState)
f(dealState)
require.Equal(t, dealState.Status, retrievalmarket.DealStatusFundsNeededLastPayment)
require.Equal(t, dealState.TotalSent, uint64(6000))
require.Equal(t, dealState.TotalSent, defaultTotalSent+defaultCurrentInterval)
require.Empty(t, dealState.Message)
})

Expand Down Expand Up @@ -233,10 +233,10 @@ func TestProcessPayment(t *testing.T) {
voucher := testnet.MakeTestSignedVoucher()
t.Run("it works", func(t *testing.T) {
node := testnodes.NewTestRetrievalProviderNode()
err := node.ExpectVoucher(payCh, voucher, nil, tokenamount.FromInt(500000), tokenamount.FromInt(500000), nil)
err := node.ExpectVoucher(payCh, voucher, nil, defaultPaymentPerInterval, defaultPaymentPerInterval, nil)
require.NoError(t, err)
dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded)
dealState.TotalSent = uint64(6000)
dealState.TotalSent = defaultTotalSent + defaultCurrentInterval
dealPayment := retrievalmarket.DealPayment{
ID: dealState.ID,
PaymentChannel: payCh,
Expand All @@ -249,16 +249,16 @@ func TestProcessPayment(t *testing.T) {
node.VerifyExpectations(t)
f(dealState)
require.Equal(t, dealState.Status, retrievalmarket.DealStatusOngoing)
require.Equal(t, dealState.FundsReceived, tokenamount.FromInt(3000000))
require.Equal(t, dealState.CurrentInterval, uint64(1500))
require.Equal(t, dealState.FundsReceived, tokenamount.Add(defaultFundsReceived, defaultPaymentPerInterval))
require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval+defaultIntervalIncrease)
require.Empty(t, dealState.Message)
})
t.Run("it completes", func(t *testing.T) {
node := testnodes.NewTestRetrievalProviderNode()
err := node.ExpectVoucher(payCh, voucher, nil, tokenamount.FromInt(500000), tokenamount.FromInt(500000), nil)
err := node.ExpectVoucher(payCh, voucher, nil, defaultPaymentPerInterval, defaultPaymentPerInterval, nil)
require.NoError(t, err)
dealState := makeDealState(retrievalmarket.DealStatusFundsNeededLastPayment)
dealState.TotalSent = uint64(6000)
dealState.TotalSent = defaultTotalSent + defaultCurrentInterval
dealPayment := retrievalmarket.DealPayment{
ID: dealState.ID,
PaymentChannel: payCh,
Expand All @@ -271,17 +271,18 @@ func TestProcessPayment(t *testing.T) {
node.VerifyExpectations(t)
f(dealState)
require.Equal(t, dealState.Status, retrievalmarket.DealStatusCompleted)
require.Equal(t, dealState.FundsReceived, tokenamount.FromInt(3000000))
require.Equal(t, dealState.CurrentInterval, uint64(1500))
require.Equal(t, dealState.FundsReceived, tokenamount.Add(defaultFundsReceived, defaultPaymentPerInterval))
require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval+defaultIntervalIncrease)
require.Empty(t, dealState.Message)
})

t.Run("not enough funds sent", func(t *testing.T) {
node := testnodes.NewTestRetrievalProviderNode()
err := node.ExpectVoucher(payCh, voucher, nil, tokenamount.FromInt(500000), tokenamount.FromInt(400000), nil)
smallerPayment := tokenamount.FromInt(400000)
err := node.ExpectVoucher(payCh, voucher, nil, defaultPaymentPerInterval, smallerPayment, nil)
require.NoError(t, err)
dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded)
dealState.TotalSent = uint64(6000)
dealState.TotalSent = defaultTotalSent + defaultCurrentInterval
dealPayment := retrievalmarket.DealPayment{
ID: dealState.ID,
PaymentChannel: payCh,
Expand All @@ -292,25 +293,25 @@ func TestProcessPayment(t *testing.T) {
ResponseWriter: testnet.ExpectDealResponseWriter(t, rm.DealResponse{
ID: dealState.ID,
Status: retrievalmarket.DealStatusFundsNeeded,
PaymentOwed: tokenamount.FromInt(100000),
PaymentOwed: tokenamount.Sub(defaultPaymentPerInterval, smallerPayment),
}),
})
f := providerstates.ProcessPayment(ctx, fe, *dealState)
node.VerifyExpectations(t)
f(dealState)
require.Equal(t, dealState.Status, retrievalmarket.DealStatusFundsNeeded)
require.Equal(t, dealState.FundsReceived, tokenamount.FromInt(2900000))
require.Equal(t, dealState.CurrentInterval, uint64(1000))
require.Equal(t, dealState.FundsReceived, tokenamount.Add(defaultFundsReceived, smallerPayment))
require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval)
require.Empty(t, dealState.Message)
})

t.Run("failure processing payment", func(t *testing.T) {
node := testnodes.NewTestRetrievalProviderNode()
message := "your money's no good here"
err := node.ExpectVoucher(payCh, voucher, nil, tokenamount.FromInt(500000), tokenamount.FromInt(0), errors.New(message))
err := node.ExpectVoucher(payCh, voucher, nil, defaultPaymentPerInterval, tokenamount.FromInt(0), errors.New(message))
require.NoError(t, err)
dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded)
dealState.TotalSent = uint64(6000)
dealState.TotalSent = defaultTotalSent + defaultCurrentInterval
dealPayment := retrievalmarket.DealPayment{
ID: dealState.ID,
PaymentChannel: payCh,
Expand All @@ -334,7 +335,7 @@ func TestProcessPayment(t *testing.T) {
t.Run("failure reading payment", func(t *testing.T) {
node := testnodes.NewTestRetrievalProviderNode()
dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded)
dealState.TotalSent = uint64(6000)
dealState.TotalSent = defaultTotalSent + defaultCurrentInterval
fe := environment(node, testnet.TestDealStreamParams{
PaymentReader: testnet.FailDealPaymentReader,
})
Expand Down Expand Up @@ -413,6 +414,7 @@ func (te *testProviderDealEnvironment) NextBlock(_ context.Context) (rm.Block, b
var defaultCurrentInterval = uint64(1000)
var defaultIntervalIncrease = uint64(500)
var defaultPricePerByte = tokenamount.FromInt(500)
var defaultPaymentPerInterval = tokenamount.Mul(defaultPricePerByte, tokenamount.FromInt(defaultCurrentInterval))
var defaultTotalSent = uint64(5000)
var defaultFundsReceived = tokenamount.FromInt(2500000)

Expand Down

0 comments on commit eec7871

Please sign in to comment.