prysm-pulse/shared/p2p/feed_test.go

48 lines
986 B
Go
Raw Normal View History

package p2p
import (
"reflect"
2018-09-09 22:15:24 +00:00
"sync"
"testing"
2018-09-09 22:15:24 +00:00
"github.com/gogo/protobuf/proto"
2018-09-09 22:15:24 +00:00
testpb "github.com/prysmaticlabs/prysm/proto/testing"
)
func TestFeed_ReturnsSameFeed(t *testing.T) {
tests := []struct {
2018-09-09 22:15:24 +00:00
a proto.Message
b proto.Message
want bool
}{
// Equality tests
2018-09-09 22:15:24 +00:00
{a: &testpb.TestMessage{}, b: &testpb.TestMessage{}, want: true},
{a: &testpb.Puzzle{}, b: &testpb.Puzzle{}, want: true},
// Inequality tests
2018-09-09 22:15:24 +00:00
{a: &testpb.TestMessage{}, b: &testpb.Puzzle{}, want: false},
{a: &testpb.Puzzle{}, b: &testpb.TestMessage{}, want: false},
}
s, _ := NewServer(&ServerConfig{})
for _, tt := range tests {
feed1 := s.Feed(tt.a)
feed2 := s.Feed(tt.b)
if (feed1 == feed2) != tt.want {
t.Errorf("Expected %v == %v to be %t", feed1, feed2, tt.want)
}
}
}
func TestFeed_ConcurrentWrite(t *testing.T) {
2018-09-09 22:15:24 +00:00
s := Server{
feeds: make(map[reflect.Type]Feed),
2018-09-09 22:15:24 +00:00
mutex: &sync.Mutex{},
}
for i := 0; i < 5; i++ {
2018-09-09 22:15:24 +00:00
go s.Feed(&testpb.TestMessage{})
}
}