swarm/swap: introduced sleep after retrieval for network test

This commit is contained in:
Fabio Barone 2018-10-08 13:26:30 -05:00
parent b1c51e7505
commit fe7ffe3eb1
4 changed files with 65 additions and 12 deletions

View file

@ -353,6 +353,9 @@ func (p *Peer) handleIncoming(handle func(ctx context.Context, msg interface{})
} }
if p.spec.Hook != nil { if p.spec.Hook != nil {
if wmsg.Size != uint32(len(wmsg.Payload)) {
log.Warn("Advertised message size and payload length don't match")
}
err := p.spec.Hook.Receive(p, wmsg.Size, val) err := p.spec.Hook.Receive(p, wmsg.Size, val)
if err != nil { if err != nil {
return err return err

View file

@ -32,14 +32,13 @@ import (
const ( const (
defaultMaxMsgSize = 1024 * 1024 defaultMaxMsgSize = 1024 * 1024
OracleID = "swap"
swapProtocolName = "swap" swapProtocolName = "swap"
swapVersion = 1 swapVersion = 1
) )
var ( var (
payAt = int64(-4096 * 10000) // threshold that triggers payment {request} (bytes) payAt = int64(-4096 * 10000000) // threshold that triggers payment {request} (bytes)
dropAt = int64(-4096 * 12000) // threshold that triggers disconnect (bytes) dropAt = int64(-4096 * 12000000) // threshold that triggers disconnect (bytes)
ErrNotAccountedMsg = errors.New("Message does not need accounting") ErrNotAccountedMsg = errors.New("Message does not need accounting")
ErrInsufficientFunds = errors.New("Insufficient funds") ErrInsufficientFunds = errors.New("Insufficient funds")

View file

@ -23,6 +23,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math" "math"
mrand "math/rand"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -148,6 +149,52 @@ func TestLimits(t *testing.T) {
} }
} }
//check that the disconnect threshold is below the payment threshold
func TestRepeatedBookings(t *testing.T) {
//create a test swap account
swap, testDir := createTestSwap(t)
defer os.RemoveAll(testDir)
testPeer := newDummyPeer()
amount := mrand.Intn(100)
cnt := 1 + mrand.Intn(10)
for i := 0; i < cnt; i++ {
swap.Credit(testPeer.Peer.Peer, uint64(amount))
}
expectedBalance := int64(cnt * amount)
realBalance := swap.balances[testPeer.ID()]
if expectedBalance != realBalance {
t.Fatal(fmt.Sprintf("After %d credits of %d, expected balance to be: %d, but is: %d", cnt, amount, expectedBalance, realBalance))
}
testPeer2 := newDummyPeer()
amount = mrand.Intn(100)
cnt = 1 + mrand.Intn(10)
for i := 0; i < cnt; i++ {
swap.Debit(testPeer2.Peer.Peer, uint64(amount))
}
expectedBalance = int64(0 - (cnt * amount))
realBalance = swap.balances[testPeer2.ID()]
if expectedBalance != realBalance {
t.Fatal(fmt.Sprintf("After %d debits of %d, expected balance to be: %d, but is: %d", cnt, amount, expectedBalance, realBalance))
}
//mixed debits and credits
amount1 := mrand.Intn(100)
amount2 := mrand.Intn(100)
amount3 := mrand.Intn(100)
swap.Credit(testPeer2.Peer.Peer, uint64(amount1))
swap.Credit(testPeer2.Peer.Peer, uint64(amount2))
swap.Debit(testPeer2.Peer.Peer, uint64(amount3))
expectedBalance = expectedBalance + int64(amount1+amount2-amount3)
realBalance = swap.balances[testPeer2.ID()]
if expectedBalance != realBalance {
t.Fatal(fmt.Sprintf("After mixed debits and credits, expected balance to be: %d, but is: %d", expectedBalance, realBalance))
}
}
//unit test for exceeds pay limit //unit test for exceeds pay limit
//when the payment threshold is reached, a cheque will be issued //when the payment threshold is reached, a cheque will be issued
//this test checks that a cheque is present if a message is sent //this test checks that a cheque is present if a message is sent

View file

@ -21,7 +21,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math"
"math/rand" "math/rand"
"os" "os"
"strconv" "strconv"
@ -140,6 +139,8 @@ func TestSwapNetworkSymmetricFileUpload(t *testing.T) {
break break
} }
} }
time.Sleep(5 * time.Second)
//every node has a map to all nodes it had interactions //every node has a map to all nodes it had interactions
//each entry in the map is a map of the other node with all the balances //each entry in the map is a map of the other node with all the balances
balancesMap := make(map[enode.ID]map[enode.ID]int64) balancesMap := make(map[enode.ID]map[enode.ID]int64)
@ -165,6 +166,7 @@ func TestSwapNetworkSymmetricFileUpload(t *testing.T) {
//get the peer's balance with this node //get the peer's balance with this node
balance, err := swarm.swap.GetPeerBalance(n) balance, err := swarm.swap.GetPeerBalance(n)
fmt.Println(balance)
if err == nil { if err == nil {
subBalances[n] = balance subBalances[n] = balance
log.Debug(fmt.Sprintf("Balance of node %s to node %s: %d", node.TerminalString(), n.TerminalString(), balance)) log.Debug(fmt.Sprintf("Balance of node %s to node %s: %d", node.TerminalString(), n.TerminalString(), balance))
@ -203,8 +205,8 @@ func TestSwapNetworkSymmetricFileUpload(t *testing.T) {
log.Trace(fmt.Sprintf("balance of %s with %s: %d", k.TerminalString(), n.TerminalString(), balanceKwithN)) log.Trace(fmt.Sprintf("balance of %s with %s: %d", k.TerminalString(), n.TerminalString(), balanceKwithN))
log.Trace(fmt.Sprintf("balance of %s with %s: %d", n.TerminalString(), k.TerminalString(), mapForSubK[k])) log.Trace(fmt.Sprintf("balance of %s with %s: %d", n.TerminalString(), k.TerminalString(), mapForSubK[k]))
//...check that they have the same balance in Abs terms and that it is not 0 //...check that they have the same balance in Abs terms and that it is not 0
if math.Abs(float64(balanceKwithN)) != math.Abs(float64(mapForSubK[k])) && balanceKwithN != 0 { if balanceKwithN+mapForSubK[k] != 0 && balanceKwithN != 0 {
log.Error(fmt.Sprintf("Expected balances to be |abs| = 0 AND balance1 != 0, but they are not: %d, %d", balanceKwithN, mapForSubK[k])) log.Error(fmt.Sprintf("Expected balances to be a+b = 0 AND balance(a) != 0, but they are not, balance k with n: %d, balance n with k: %d", balanceKwithN, mapForSubK[k]))
success = false success = false
} }
} }
@ -278,10 +280,10 @@ func TestSwapNetworkAsymmetricFileUpload(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
//this is actually quite a big maxFileSize, which results //NOTE: maxFileSize is 4 kB, this in order to provide faster tests
//in the test running for nearly 2 minutes //it would be interesting to run these tests with bigger files
//maybe for the test, we could reduce it //(to see how drop limits are affected etc.)
const maxFileSize = 1024 * 1024 * 4 //1024 bytes * 1024 * 4 = 4MB const maxFileSize = 1024 * 4 //1024 bytes * 4 = 4kB
const minfileSize = 1024 const minfileSize = 1024
//pseudo random algo to define if a node will upload or not //pseudo random algo to define if a node will upload or not
@ -323,6 +325,8 @@ func TestSwapNetworkAsymmetricFileUpload(t *testing.T) {
} }
} }
time.Sleep(5 * time.Second)
balancesMap := make(map[enode.ID]map[enode.ID]int64) balancesMap := make(map[enode.ID]map[enode.ID]int64)
for _, node := range sim.NodeIDs() { for _, node := range sim.NodeIDs() {
@ -370,8 +374,8 @@ func TestSwapNetworkAsymmetricFileUpload(t *testing.T) {
if n == subK { if n == subK {
log.Trace(fmt.Sprintf("balance of %s with %s: %d", k.TerminalString(), n.TerminalString(), balanceKwithN)) log.Trace(fmt.Sprintf("balance of %s with %s: %d", k.TerminalString(), n.TerminalString(), balanceKwithN))
log.Trace(fmt.Sprintf("balance of %s with %s: %d", n.TerminalString(), k.TerminalString(), mapForSubK[k])) log.Trace(fmt.Sprintf("balance of %s with %s: %d", n.TerminalString(), k.TerminalString(), mapForSubK[k]))
if math.Abs(float64(balanceKwithN)) != math.Abs(float64(mapForSubK[k])) && balanceKwithN != 0 { if balanceKwithN+mapForSubK[k] != 0 && balanceKwithN != 0 {
log.Error("Expected balances to be |abs| = 0 AND balance1 != 0, but they are not") log.Error(fmt.Sprintf("Expected balances to be a+b = 0 AND balance(a) != 0, but they are not, balance k with n: %d, balance n with k: %d", balanceKwithN, mapForSubK[k]))
success = false success = false
} }
} }