whisper: test timeout extended (#16088)

* whisper: timeout extended

* whisper: test updated

* whisper: test updated
This commit is contained in:
gluk256 2018-02-15 13:42:44 +01:00 committed by Péter Szilágyi
parent 2003b79779
commit fac6d9ce77

View File

@ -70,9 +70,8 @@ var keys = []string{
"7184c1701569e3a4c4d2ddce691edd983b81e42e09196d332e1ae2f1e062cff4",
}
const NumNodes = 16 // must not exceed the number of keys (32)
type TestData struct {
started int
counter [NumNodes]int
mutex sync.RWMutex
}
@ -84,21 +83,29 @@ type TestNode struct {
filerID string
}
const NumNodes = 8 // must not exceed the number of keys (32)
var result TestData
var nodes [NumNodes]*TestNode
var sharedKey = hexutil.MustDecode("0x03ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31")
var wrongKey = hexutil.MustDecode("0xf91156714d7ec88d3edc1c652c2181dbb3044e8771c683f3b30d33c12b986b11")
var sharedTopic = TopicType{0xF, 0x1, 0x2, 0}
var expectedMessage = []byte("per rectum ad astra")
var wrongTopic = TopicType{0, 0, 0, 0}
var expectedMessage = []byte("per aspera ad astra")
var unexpectedMessage = []byte("per rectum ad astra")
var masterBloomFilter []byte
var masterPow = 0.00000001
var round = 1
var debugMode = false
var prevTime time.Time
var cntPrev int
func TestSimulation(t *testing.T) {
// create a chain of whisper nodes,
// installs the filters with shared (predefined) parameters
initialize(t)
// each node sends a number of random (undecryptable) messages
// each node sends one random (not decryptable) message
for i := 0; i < NumNodes; i++ {
sendMsg(t, false, i)
}
@ -115,7 +122,6 @@ func TestSimulation(t *testing.T) {
// send new pow and bloom exchange messages
resetParams(t)
round++
// node #1 sends one expected (decryptable) message
sendMsg(t, true, 1)
@ -140,6 +146,8 @@ func resetParams(t *testing.T) {
for i := 0; i < NumNodes; i++ {
nodes[i].shh.SetBloomFilter(masterBloomFilter)
}
round++
}
func initBloom(t *testing.T) {
@ -219,15 +227,22 @@ func initialize(t *testing.T) {
nodes[i] = &node
}
for i := 1; i < NumNodes; i++ {
go nodes[i].server.Start()
for i := 0; i < NumNodes; i++ {
go startServer(t, nodes[i].server)
}
// we need to wait until the first node actually starts
err = nodes[0].server.Start()
waitForServersToStart(t)
}
func startServer(t *testing.T, s *p2p.Server) {
err := s.Start()
if err != nil {
t.Fatalf("failed to start the fisrt server.")
}
result.mutex.Lock()
defer result.mutex.Unlock()
result.started++
}
func stopServers() {
@ -246,8 +261,10 @@ func checkPropagation(t *testing.T, includingNodeZero bool) {
return
}
const cycle = 50
const iterations = 200
prevTime = time.Now()
// (cycle * iterations) should not exceed 50 seconds, since TTL=50
const cycle = 200 // time in milliseconds
const iterations = 250
first := 0
if !includingNodeZero {
@ -262,29 +279,29 @@ func checkPropagation(t *testing.T, includingNodeZero bool) {
}
mail := f.Retrieve()
if !validateMail(t, i, mail) {
return
}
validateMail(t, i, mail)
if isTestComplete() {
checkTestStatus()
return
}
}
checkTestStatus()
time.Sleep(cycle * time.Millisecond)
}
t.Fatalf("Test was not complete: timeout %d seconds. nodes=%v", iterations*cycle/1000, nodes)
if !includingNodeZero {
f := nodes[0].shh.GetFilter(nodes[0].filerID)
if f != nil {
t.Fatalf("node zero received a message with low PoW.")
}
}
t.Fatalf("Test was not complete (%d round): timeout %d seconds. nodes=%v", round, iterations*cycle/1000, nodes)
}
func validateMail(t *testing.T, index int, mail []*ReceivedMessage) bool {
func validateMail(t *testing.T, index int, mail []*ReceivedMessage) {
var cnt int
for _, m := range mail {
if bytes.Equal(m.Payload, expectedMessage) {
@ -294,14 +311,13 @@ func validateMail(t *testing.T, index int, mail []*ReceivedMessage) bool {
if cnt == 0 {
// no messages received yet: nothing is wrong
return true
return
}
if cnt > 1 {
t.Fatalf("node %d received %d.", index, cnt)
return false
}
if cnt > 0 {
if cnt == 1 {
result.mutex.Lock()
defer result.mutex.Unlock()
result.counter[index] += cnt
@ -309,7 +325,28 @@ func validateMail(t *testing.T, index int, mail []*ReceivedMessage) bool {
t.Fatalf("node %d accumulated %d.", index, result.counter[index])
}
}
return true
}
func checkTestStatus() {
var cnt int
var arr [NumNodes]int
for i := 0; i < NumNodes; i++ {
arr[i] = nodes[i].server.PeerCount()
envelopes := nodes[i].shh.Envelopes()
if len(envelopes) >= NumNodes {
cnt++
}
}
if debugMode {
if cntPrev != cnt {
fmt.Printf(" %v \t number of nodes that have received all msgs: %d, number of peers per node: %v \n",
time.Since(prevTime), cnt, arr)
prevTime = time.Now()
cntPrev = cnt
}
}
}
func isTestComplete() bool {
@ -324,7 +361,7 @@ func isTestComplete() bool {
for i := 0; i < NumNodes; i++ {
envelopes := nodes[i].shh.Envelopes()
if len(envelopes) < 2 {
if len(envelopes) < NumNodes+1 {
return false
}
}
@ -339,9 +376,10 @@ func sendMsg(t *testing.T, expected bool, id int) {
opt := MessageParams{KeySym: sharedKey, Topic: sharedTopic, Payload: expectedMessage, PoW: 0.00000001, WorkTime: 1}
if !expected {
opt.KeySym[0]++
opt.Topic[0]++
opt.Payload = opt.Payload[1:]
opt.KeySym = wrongKey
opt.Topic = wrongTopic
opt.Payload = unexpectedMessage
opt.Payload[0] = byte(id)
}
msg, err := NewSentMessage(&opt)
@ -459,3 +497,14 @@ func checkBloomFilterExchange(t *testing.T) {
time.Sleep(50 * time.Millisecond)
}
}
func waitForServersToStart(t *testing.T) {
const iterations = 200
for j := 0; j < iterations; j++ {
time.Sleep(50 * time.Millisecond)
if result.started == NumNodes {
return
}
}
t.Fatalf("Failed to start all the servers, running: %d", result.started)
}