2018-08-29 03:45:26 +00:00
|
|
|
package paychan
|
|
|
|
|
|
|
|
import (
|
2018-09-19 19:49:00 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-08-29 03:45:26 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEndBlocker(t *testing.T) {
|
2018-09-03 20:38:51 +00:00
|
|
|
// TODO test that endBlocker doesn't close channels before the execution time
|
|
|
|
|
2018-08-29 03:45:26 +00:00
|
|
|
// SETUP
|
2018-09-01 17:16:56 +00:00
|
|
|
accountSeeds := []string{"senderSeed", "receiverSeed"}
|
|
|
|
ctx, _, channelKeeper, addrs, _, _, _ := createMockApp(accountSeeds)
|
2018-08-29 03:45:26 +00:00
|
|
|
sender := addrs[0]
|
|
|
|
receiver := addrs[1]
|
2018-09-19 19:49:00 +00:00
|
|
|
coins := sdk.Coins{sdk.NewInt64Coin("KVA", 10)}
|
2018-08-29 03:45:26 +00:00
|
|
|
|
|
|
|
// create new channel
|
|
|
|
channelID := ChannelID(0) // should be 0 as first channel
|
|
|
|
channel := Channel{
|
|
|
|
ID: channelID,
|
|
|
|
Participants: [2]sdk.AccAddress{sender, receiver},
|
|
|
|
Coins: coins,
|
|
|
|
}
|
|
|
|
channelKeeper.setChannel(ctx, channel)
|
|
|
|
|
|
|
|
// create closing update and submittedUpdate
|
2018-09-19 19:49:00 +00:00
|
|
|
payout := Payout{sdk.Coins{sdk.NewInt64Coin("KVA", 3)}, sdk.Coins{sdk.NewInt64Coin("KVA", 7)}}
|
2018-08-29 03:45:26 +00:00
|
|
|
update := Update{
|
|
|
|
ChannelID: channelID,
|
2018-09-01 17:16:56 +00:00
|
|
|
Payout: payout,
|
2018-08-29 03:45:26 +00:00
|
|
|
}
|
|
|
|
sUpdate := SubmittedUpdate{
|
|
|
|
Update: update,
|
|
|
|
ExecutionTime: 0, // current blocktime
|
|
|
|
}
|
|
|
|
// Set empty submittedUpdatesQueue TODO work out proper genesis initialisation
|
|
|
|
channelKeeper.setSubmittedUpdatesQueue(ctx, SubmittedUpdatesQueue{})
|
|
|
|
// flag channel for closure
|
|
|
|
channelKeeper.addToSubmittedUpdatesQueue(ctx, sUpdate)
|
|
|
|
|
|
|
|
// ACTION
|
|
|
|
EndBlocker(ctx, channelKeeper)
|
|
|
|
|
|
|
|
// CHECK RESULTS
|
|
|
|
// ideally just check if keeper.channelClose was called, but can't
|
|
|
|
// writing endBlocker to accept an interface of which keeper is implementation would make this possible
|
|
|
|
// check channel is gone
|
|
|
|
_, found := channelKeeper.getChannel(ctx, channelID)
|
|
|
|
assert.False(t, found)
|
|
|
|
// check queue is empty, NOTE: due to encoding, an empty queue (underneath just an int slice) will be decoded as nil slice rather than an empty slice
|
2018-09-01 23:29:51 +00:00
|
|
|
suq := channelKeeper.getSubmittedUpdatesQueue(ctx)
|
2018-08-29 03:45:26 +00:00
|
|
|
assert.Equal(t, SubmittedUpdatesQueue(nil), suq)
|
|
|
|
// check submittedUpdate is gone
|
|
|
|
_, found = channelKeeper.getSubmittedUpdate(ctx, channelID)
|
|
|
|
assert.False(t, found)
|
|
|
|
}
|