whisper: fix loop in expire() (#17532)

This commit is contained in:
Adam Babik 2018-08-29 12:56:13 +02:00 committed by Péter Szilágyi
parent 9574968116
commit 75ae5af62a
4 changed files with 42 additions and 28 deletions

@ -717,7 +717,7 @@ func (w *Whisper) expire() {
w.stats.messagesCleared++ w.stats.messagesCleared++
w.stats.memoryCleared += sz w.stats.memoryCleared += sz
w.stats.memoryUsed -= sz w.stats.memoryUsed -= sz
return true return false
}) })
w.expirations[expiry].Clear() w.expirations[expiry].Clear()
delete(w.expirations, expiry) delete(w.expirations, expiry)

@ -487,8 +487,14 @@ func TestExpiry(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
} }
params.TTL = 1 params.TTL = 1
messagesCount := 5
// Send a few messages one after another. Due to low PoW and expiration buckets
// with one second resolution, it covers a case when there are multiple items
// in a single expiration bucket.
for i := 0; i < messagesCount; i++ {
msg, err := NewSentMessage(params) msg, err := NewSentMessage(params)
if err != nil { if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err) t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
@ -502,12 +508,13 @@ func TestExpiry(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("failed to send envelope with seed %d: %s.", seed, err) t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
} }
}
// wait till received or timeout // wait till received or timeout
var received, expired bool var received, expired bool
for j := 0; j < 20; j++ { for j := 0; j < 20; j++ {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
if len(w.Envelopes()) > 0 { if len(w.Envelopes()) == messagesCount {
received = true received = true
break break
} }

@ -900,7 +900,7 @@ func (whisper *Whisper) expire() {
whisper.stats.messagesCleared++ whisper.stats.messagesCleared++
whisper.stats.memoryCleared += sz whisper.stats.memoryCleared += sz
whisper.stats.memoryUsed -= sz whisper.stats.memoryUsed -= sz
return true return false
}) })
whisper.expirations[expiry].Clear() whisper.expirations[expiry].Clear()
delete(whisper.expirations, expiry) delete(whisper.expirations, expiry)

@ -465,8 +465,14 @@ func TestExpiry(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
} }
params.TTL = 1 params.TTL = 1
messagesCount := 5
// Send a few messages one after another. Due to low PoW and expiration buckets
// with one second resolution, it covers a case when there are multiple items
// in a single expiration bucket.
for i := 0; i < messagesCount; i++ {
msg, err := NewSentMessage(params) msg, err := NewSentMessage(params)
if err != nil { if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err) t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
@ -480,12 +486,13 @@ func TestExpiry(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("failed to send envelope with seed %d: %s.", seed, err) t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
} }
}
// wait till received or timeout // wait till received or timeout
var received, expired bool var received, expired bool
for j := 0; j < 20; j++ { for j := 0; j < 20; j++ {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
if len(w.Envelopes()) > 0 { if len(w.Envelopes()) == messagesCount {
received = true received = true
break break
} }