Commit 95d497f3 authored by Petar Radovic's avatar Petar Radovic Committed by GitHub

Hive tests fix - expect values eventually (#71)

* hive tests fix

* tests name in hive tests
parent 6e9510b1
......@@ -110,6 +110,7 @@ github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo=
github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc=
......@@ -232,6 +233,7 @@ github.com/libp2p/go-libp2p-testing v0.0.2/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MB
github.com/libp2p/go-libp2p-testing v0.0.3/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E=
github.com/libp2p/go-libp2p-testing v0.0.4/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E=
github.com/libp2p/go-libp2p-testing v0.1.0/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0=
github.com/libp2p/go-libp2p-testing v0.1.1 h1:U03z3HnGI7Ni8Xx6ONVZvUFOAzWYmolWf5W5jAOPNmU=
github.com/libp2p/go-libp2p-testing v0.1.1/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0=
github.com/libp2p/go-libp2p-tls v0.1.1 h1:tjW7njTM8JX8FbEvqr8/VSKBdZYZ7CtGtv3i6NiFf10=
github.com/libp2p/go-libp2p-tls v0.1.1/go.mod h1:wZfuewxOndz5RTnCAxFliGjvYSDA40sKitV4c50uI1M=
......@@ -351,9 +353,11 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.11.0 h1:JAKSXpt1YjtLA7YpPiqO9ss6sNXEsPfSGdwN0UHqzrw=
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.8.1 h1:C5Dqfs/LeauYDX0jJXIe2SWmwCbGzx9yF8C8xy3Lh34=
github.com/onsi/gomega v1.8.1/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg=
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
......@@ -540,6 +544,7 @@ golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
......@@ -559,6 +564,7 @@ gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
......@@ -567,6 +573,7 @@ gopkg.in/mail.v2 v2.3.1/go.mod h1:htwXN1Qh09vZJ1NVKxQqHPBaCBbzKhp5GzuJEA4VJWw=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/src-d/go-cli.v0 v0.0.0-20181105080154-d492247bbc0d/go.mod h1:z+K8VcOYVYcSwSjGebuDL6176A1XskgbtNl64NSg+n8=
gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFabTyABE=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
......
......@@ -103,97 +103,110 @@ func TestBroadcastPeers(t *testing.T) {
},
}
for _, tc := range testCases {
addressbookclean := inmem.New()
exporter, ok := addressbookclean.(AddressExporter)
if !ok {
t.Fatal("could not type assert AddressExporter")
}
// create a hive server that handles the incoming stream
server := hive.New(hive.Options{
Logger: logger,
AddressBook: addressbookclean,
})
// setup the stream recorder to record stream data
recorder := streamtest.New(
streamtest.WithProtocols(server.Protocol()),
)
// create a hive client that will do broadcast
client := hive.New(hive.Options{
Streamer: recorder,
Logger: logger,
AddressBook: addressbook,
})
if err := client.BroadcastPeers(context.Background(), tc.addresee, tc.peers...); err != nil {
t.Fatal(err)
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
addressbookclean := inmem.New()
exporter, ok := addressbookclean.(AddressExporter)
if !ok {
t.Fatal("could not type assert AddressExporter")
}
// get a record for this stream
records, err := recorder.Records(tc.addresee, "hive", "1.0.0", "peers")
if err != nil {
t.Fatal(err)
}
if l := len(records); l != len(tc.wantMsgs) {
t.Fatalf("got %v records, want %v", l, len(tc.wantMsgs))
}
// create a hive server that handles the incoming stream
server := hive.New(hive.Options{
Logger: logger,
AddressBook: addressbookclean,
})
// setup the stream recorder to record stream data
recorder := streamtest.New(
streamtest.WithProtocols(server.Protocol()),
)
// create a hive client that will do broadcast
client := hive.New(hive.Options{
Streamer: recorder,
Logger: logger,
AddressBook: addressbook,
})
if err := client.BroadcastPeers(context.Background(), tc.addresee, tc.peers...); err != nil {
t.Fatal(err)
}
// there is a one record per batch (wantMsg)
for i, record := range records {
messages, err := readAndAssertPeersMsgs(record.In(), 1)
// get a record for this stream
records, err := recorder.Records(tc.addresee, "hive", "1.0.0", "peers")
if err != nil {
t.Fatal(err)
}
if l := len(records); l != len(tc.wantMsgs) {
t.Fatalf("got %v records, want %v", l, len(tc.wantMsgs))
}
if fmt.Sprint(messages[0]) != fmt.Sprint(tc.wantMsgs[i]) {
t.Errorf("Messages got %v, want %v", messages, tc.wantMsgs)
// there is a one record per batch (wantMsg)
for i, record := range records {
messages, err := readAndAssertPeersMsgs(record.In(), 1)
if err != nil {
t.Fatal(err)
}
if fmt.Sprint(messages[0]) != fmt.Sprint(tc.wantMsgs[i]) {
t.Errorf("Messages got %v, want %v", messages, tc.wantMsgs)
}
}
expectOverlaysEventually(t, exporter, tc.wantOverlays)
expectMultiaddresessEventually(t, exporter, tc.wantMultiAddresses)
})
}
}
func expectOverlaysEventually(t *testing.T, exporter AddressExporter, wantOverlays []swarm.Address) {
for i := 0; i < 100; i++ {
var stringOverlays []string
var stringWantOverlays []string
for _, k := range exporter.Overlays() {
stringOverlays = append(stringOverlays, k.String())
}
if !compareOverlays(exporter.Overlays(), tc.wantOverlays) {
t.Errorf("Overlays got %v, want %v", exporter.Overlays(), tc.wantOverlays)
for _, k := range wantOverlays {
stringWantOverlays = append(stringWantOverlays, k.String())
}
if !compareMultiaddrses(exporter.Multiaddresses(), tc.wantMultiAddresses) {
t.Errorf("Multiaddresses got %v, want %v", exporter.Multiaddresses(), tc.wantMultiAddresses)
sort.Strings(stringOverlays)
sort.Strings(stringWantOverlays)
if reflect.DeepEqual(stringOverlays, stringWantOverlays) {
return
}
time.Sleep(50 * time.Millisecond)
}
t.Errorf("Overlays got %v, want %v", exporter.Overlays(), wantOverlays)
}
func compareOverlays(keys []swarm.Address, wantKeys []swarm.Address) bool {
var stringKeys []string
for _, k := range keys {
stringKeys = append(stringKeys, k.String())
}
var stringWantKeys []string
for _, k := range wantKeys {
stringWantKeys = append(stringWantKeys, k.String())
}
func expectMultiaddresessEventually(t *testing.T, exporter AddressExporter, wantMultiaddresses []ma.Multiaddr) {
for i := 0; i < 100; i++ {
var stringMultiaddresses []string
for _, v := range exporter.Multiaddresses() {
stringMultiaddresses = append(stringMultiaddresses, v.String())
}
sort.Strings(stringKeys)
sort.Strings(stringWantKeys)
return reflect.DeepEqual(stringKeys, stringWantKeys)
}
var stringWantMultiAddresses []string
for _, v := range wantMultiaddresses {
stringWantMultiAddresses = append(stringWantMultiAddresses, v.String())
}
func compareMultiaddrses(values []ma.Multiaddr, wantValues []ma.Multiaddr) bool {
var stringVal []string
for _, v := range values {
stringVal = append(stringVal, v.String())
}
sort.Strings(stringMultiaddresses)
sort.Strings(stringWantMultiAddresses)
if reflect.DeepEqual(stringMultiaddresses, stringWantMultiAddresses) {
return
}
var stringWantVal []string
for _, v := range wantValues {
stringWantVal = append(stringWantVal, v.String())
time.Sleep(50 * time.Millisecond)
}
sort.Strings(stringVal)
sort.Strings(stringWantVal)
return reflect.DeepEqual(stringVal, stringWantVal)
t.Errorf("Multiaddresses got %v, want %v", exporter.Multiaddresses(), wantMultiaddresses)
}
func readAndAssertPeersMsgs(in []byte, expectedLen int) ([]pb.Peers, error) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment