3f39f5413e
This commit adds Ping and Pong messages to the suite of lnwire messages. The usage of these messages within the daemon are similar to the usage of Bitcoin’s ping/pong messages. Pings are to be sent periodically with a random nonce to check connection activity and also to gauge latency. Pong’s are to be sent in reply to ping messages, echo’ing the same nonce used.
32 lines
651 B
Go
32 lines
651 B
Go
package lnwire
|
|
|
|
import (
|
|
"bytes"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestPongEncodeDecode(t *testing.T) {
|
|
pong := &Pong{
|
|
Nonce: 9,
|
|
}
|
|
|
|
// Next encode the pong message into an empty bytes buffer.
|
|
var b bytes.Buffer
|
|
if err := pong.Encode(&b, 0); err != nil {
|
|
t.Fatalf("unable to encode pong: %v", err)
|
|
}
|
|
|
|
// Deserialize the encoded pong message into a new empty struct.
|
|
pong2 := &Pong{}
|
|
if err := pong2.Decode(&b, 0); err != nil {
|
|
t.Fatalf("unable to decode ping: %v", err)
|
|
}
|
|
|
|
// Assert equality of the two instances.
|
|
if !reflect.DeepEqual(pong, pong2) {
|
|
t.Fatalf("encode/decode pong messages don't match %#v vs %#v",
|
|
pong, pong2)
|
|
}
|
|
}
|