lnd.xprv/lnwire/pong_test.go
Olaoluwa Osuntokun 3f39f5413e
lnwire: add ping and pong messages
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.
2016-11-10 17:09:27 -08:00

32 lines
651 B
Go

package lnwire
import (
"bytes"
"reflect"
"testing"
)
func TestPingEncodeDecode(t *testing.T) {
ping := &Ping{
Nonce: 9,
}
// Next encode the ping message into an empty bytes buffer.
var b bytes.Buffer
if err := ping.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode ping: %v", err)
}
// Deserialize the encoded ping message into a new empty struct.
ping2 := &Ping{}
if err := ping2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode ping: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(ping, ping2) {
t.Fatalf("encode/decode ping messages don't match %#v vs %#v",
ping, ping2)
}
}