2020-07-27 00:56:55 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-06 18:05:16 +00:00
|
|
|
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2020-07-27 00:56:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_PublishToTopicConcurrentMapWrite(t *testing.T) {
|
2020-09-09 09:48:52 +00:00
|
|
|
s, err := NewService(context.Background(), &Config{})
|
2020-07-27 00:56:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(10)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
assert.NoError(t, s.PublishToTopic(ctx, fmt.Sprintf("foo%v", i), []byte{}))
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2020-09-06 18:05:16 +00:00
|
|
|
|
|
|
|
func TestMessageIDFunction_HashesCorrectly(t *testing.T) {
|
|
|
|
msg := [32]byte{'J', 'U', 'N', 'K'}
|
|
|
|
pMsg := &pubsubpb.Message{Data: msg[:]}
|
|
|
|
hashedData := hashutil.Hash(pMsg.Data)
|
2020-10-02 15:08:51 +00:00
|
|
|
msgID := string(hashedData[:])
|
2020-09-06 18:05:16 +00:00
|
|
|
assert.Equal(t, msgID, msgIDFunction(pMsg), "Got incorrect msg id")
|
|
|
|
}
|