mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
whisper: test added
This commit is contained in:
parent
709d6e47f8
commit
581841f74d
2 changed files with 88 additions and 1 deletions
|
|
@ -317,7 +317,10 @@ func (w *Whisper) Unwatch(id string) {
|
||||||
// Send injects a message into the whisper send queue, to be distributed in the
|
// Send injects a message into the whisper send queue, to be distributed in the
|
||||||
// network in the coming cycles.
|
// network in the coming cycles.
|
||||||
func (w *Whisper) Send(envelope *Envelope) error {
|
func (w *Whisper) Send(envelope *Envelope) error {
|
||||||
_, err := w.add(envelope)
|
ok, err := w.add(envelope)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("failed to add envelope")
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -354,3 +354,87 @@ func TestExpiry(t *testing.T) {
|
||||||
t.Fatalf("expire failed, seed: %d.", seed)
|
t.Fatalf("expire failed, seed: %d.", seed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCustomization(t *testing.T) {
|
||||||
|
InitSingleTest()
|
||||||
|
|
||||||
|
w := New()
|
||||||
|
defer w.SetMinimumPoW(DefaultMinimumPoW)
|
||||||
|
defer w.SetMaxMessageLength(DefaultMaxMessageLength)
|
||||||
|
w.Start(nil)
|
||||||
|
defer w.Stop()
|
||||||
|
|
||||||
|
const smallPoW = 0.00001
|
||||||
|
|
||||||
|
f, err := generateFilter(t, true)
|
||||||
|
params, err := generateMessageParams()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
params.KeySym = f.KeySym
|
||||||
|
params.Topic = f.Topics[2]
|
||||||
|
params.PoW = smallPoW
|
||||||
|
params.TTL = 3600 * 24 // one day
|
||||||
|
msg := NewSentMessage(params)
|
||||||
|
env, err := msg.Wrap(params)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = w.Send(env)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("successfully sent envelope with PoW %.06f, false positive (seed %d).", env.PoW(), seed)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.SetMinimumPoW(smallPoW / 2)
|
||||||
|
err = w.Send(env)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
params.TTL++
|
||||||
|
msg = NewSentMessage(params)
|
||||||
|
env, err = msg.Wrap(params)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
||||||
|
}
|
||||||
|
w.SetMaxMessageLength(env.size() - 1)
|
||||||
|
err = w.Send(env)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("successfully sent oversized envelope (seed %d): false positive.", seed)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.SetMaxMessageLength(DefaultMaxMessageLength)
|
||||||
|
err = w.Send(env)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to send second envelope with seed %d: %s.", seed, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait till received or timeout
|
||||||
|
var received bool
|
||||||
|
for j := 0; j < 20; j++ {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
if len(w.Envelopes()) > 1 {
|
||||||
|
received = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !received {
|
||||||
|
t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check w.messages()
|
||||||
|
id, err := w.Watch(f)
|
||||||
|
time.Sleep(5 * time.Millisecond)
|
||||||
|
mail := f.Retrieve()
|
||||||
|
if len(mail) > 0 {
|
||||||
|
t.Fatalf("received premature mail")
|
||||||
|
}
|
||||||
|
|
||||||
|
mail = w.Messages(id)
|
||||||
|
if len(mail) != 2 {
|
||||||
|
t.Fatalf("failed to get whisper messages")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue