Skip to content

Commit

Permalink
Make some tests pass with modern go
Browse files Browse the repository at this point in the history
  • Loading branch information
banks committed Aug 9, 2018
1 parent 395d529 commit 6e35db9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
33 changes: 16 additions & 17 deletions memberlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/miekg/dns"
"github.com/stretchr/testify/require"
)

var bindLock sync.Mutex
Expand Down Expand Up @@ -424,12 +425,15 @@ func TestMemberList_ResolveAddr_TCP_First(t *testing.T) {
}
port := uint16(m.config.BindPort)
expected := []ipPort{
ipPort{net.ParseIP("127.0.0.1"), port},
// Go now parses IPs like this and returns IP4-mapped IPv6 address.
// Confusingly if you print it you see the same as the input since
// IP.String converts IP4-mapped addresses back to dotted decimal notation
// but the underlying IP bytes don't compare as equal to the actual IPv4
// bytes the resolver will get from DNS.
ipPort{net.ParseIP("127.0.0.1").To4(), port},
ipPort{net.ParseIP("2001:db8:a0b:12f0::1"), port},
}
if !reflect.DeepEqual(ips, expected) {
t.Fatalf("bad: %#v expected: %#v", ips, expected)
}
require.Equal(t, expected, ips)
}
}

Expand Down Expand Up @@ -894,12 +898,14 @@ func TestMemberlist_UserData(t *testing.T) {
m1.schedule()
defer m1.Shutdown()

// Create a second delegate with things to send
d2 := &MockDelegate{}
d2.broadcasts = [][]byte{
bcasts := [][]byte{
[]byte("test"),
[]byte("foobar"),
}

// Create a second delegate with things to send
d2 := &MockDelegate{}
d2.broadcasts = bcasts
d2.state = []byte("my state")

// Create a second node
Expand Down Expand Up @@ -933,16 +939,9 @@ func TestMemberlist_UserData(t *testing.T) {
// Wait for a little while
time.Sleep(3 * time.Millisecond)

// Ensure we got the messages
if len(d1.msgs) != 2 {
t.Fatalf("should have 2 messages!")
}
if !reflect.DeepEqual(d1.msgs[0], []byte("test")) {
t.Fatalf("bad msg %v", d1.msgs[0])
}
if !reflect.DeepEqual(d1.msgs[1], []byte("foobar")) {
t.Fatalf("bad msg %v", d1.msgs[1])
}
// Ensure we got the messages. Ordering of messages is not guaranteed so just
// check we got them both in either order.
require.ElementsMatch(t, bcasts, d1.msgs)

// Check the push/pull state
if !reflect.DeepEqual(d1.remoteState, []byte("my state")) {
Expand Down
2 changes: 1 addition & 1 deletion net.go
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ func (m *Memberlist) sendPingAndWaitForAck(addr string, ping ping, deadline time
}

if ack.SeqNo != ping.SeqNo {
return false, fmt.Errorf("Sequence number from ack (%d) doesn't match ping (%d)", ack.SeqNo, ping.SeqNo, LogConn(conn))
return false, fmt.Errorf("Sequence number from ack (%d) doesn't match ping (%d)", ack.SeqNo, ping.SeqNo)
}

return true, nil
Expand Down
15 changes: 10 additions & 5 deletions transport_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package memberlist

import (
"bytes"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestTransport_Join(t *testing.T) {
Expand Down Expand Up @@ -116,9 +117,13 @@ func TestTransport_Send(t *testing.T) {
}
time.Sleep(100 * time.Millisecond)

received := bytes.Join(d1.msgs, []byte("|"))
expected := []byte("SendTo|SendToUDP|SendToTCP|SendBestEffort|SendReliable")
if !bytes.Equal(received, expected) {
t.Fatalf("bad: %s", received)
expected := []string{"SendTo", "SendToUDP", "SendToTCP", "SendBestEffort", "SendReliable"}

received := make([]string, len(d1.msgs))
for i, bs := range d1.msgs {
received[i] = string(bs)
}
// Some of these are UDP so often get re-ordered making the test flaky if we
// assert send ordering. Sort both slices to be tolerant of re-ordering.
require.ElementsMatch(t, expected, received)
}

0 comments on commit 6e35db9

Please sign in to comment.