73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
|
package paychmgr
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"github.com/ipfs/go-cid"
|
||
|
|
||
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
||
|
tutils "github.com/filecoin-project/specs-actors/support/testing"
|
||
|
ds "github.com/ipfs/go-datastore"
|
||
|
ds_sync "github.com/ipfs/go-datastore/sync"
|
||
|
"github.com/stretchr/testify/require"
|
||
|
)
|
||
|
|
||
|
func TestPaychSettle(t *testing.T) {
|
||
|
ctx := context.Background()
|
||
|
store := NewStore(ds_sync.MutexWrap(ds.NewMapDatastore()))
|
||
|
|
||
|
expch := tutils.NewIDAddr(t, 100)
|
||
|
expch2 := tutils.NewIDAddr(t, 101)
|
||
|
from := tutils.NewIDAddr(t, 101)
|
||
|
to := tutils.NewIDAddr(t, 102)
|
||
|
|
||
|
sm := newMockStateManager()
|
||
|
pchapi := newMockPaychAPI()
|
||
|
defer pchapi.close()
|
||
|
|
||
|
mgr, err := newManager(sm, store, pchapi)
|
||
|
require.NoError(t, err)
|
||
|
|
||
|
amt := big.NewInt(10)
|
||
|
_, mcid, err := mgr.GetPaych(ctx, from, to, amt)
|
||
|
require.NoError(t, err)
|
||
|
|
||
|
time.Sleep(10 * time.Millisecond)
|
||
|
|
||
|
// Send channel create response
|
||
|
response := testChannelResponse(t, expch)
|
||
|
pchapi.finishWaitingCalls(response)
|
||
|
|
||
|
// Get the channel address
|
||
|
ch, err := mgr.GetPaychWaitReady(ctx, mcid)
|
||
|
require.NoError(t, err)
|
||
|
require.Equal(t, expch, ch)
|
||
|
|
||
|
// Settle the channel
|
||
|
_, err = mgr.Settle(ctx, ch)
|
||
|
require.NoError(t, err)
|
||
|
|
||
|
// Send another request for funds to the same from/to
|
||
|
// (should create a new channel because the previous channel
|
||
|
// is settling)
|
||
|
amt2 := big.NewInt(5)
|
||
|
_, mcid2, err := mgr.GetPaych(ctx, from, to, amt2)
|
||
|
require.NoError(t, err)
|
||
|
require.NotEqual(t, cid.Undef, mcid2)
|
||
|
|
||
|
time.Sleep(10 * time.Millisecond)
|
||
|
|
||
|
// Send new channel create response
|
||
|
response2 := testChannelResponse(t, expch2)
|
||
|
pchapi.finishWaitingCalls(response2)
|
||
|
|
||
|
time.Sleep(10 * time.Millisecond)
|
||
|
|
||
|
// Make sure the new channel is different from the old channel
|
||
|
ch2, err := mgr.GetPaychWaitReady(ctx, mcid2)
|
||
|
require.NoError(t, err)
|
||
|
require.NotEqual(t, ch, ch2)
|
||
|
}
|