2021-09-29 14:39:44 +00:00
|
|
|
package tracer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
|
2022-12-09 20:20:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2021-09-29 14:39:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testTracerTransport struct {
|
|
|
|
t *testing.T
|
|
|
|
executeTest func(t *testing.T, evt TracerTransportEvent)
|
|
|
|
}
|
|
|
|
|
2021-09-29 14:49:03 +00:00
|
|
|
const peerIDA peer.ID = "12D3KooWAbSVMgRejb6ECg6fRTkCPGCfu8396msZVryu8ivcz44G"
|
2021-09-29 14:39:44 +00:00
|
|
|
|
|
|
|
func NewTestTraceTransport(t *testing.T, executeTest func(t *testing.T, evt TracerTransportEvent)) TracerTransport {
|
|
|
|
return &testTracerTransport{
|
|
|
|
t: t,
|
|
|
|
executeTest: executeTest,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ttt *testTracerTransport) Transport(evt TracerTransportEvent) error {
|
|
|
|
ttt.executeTest(ttt.t, evt)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTracer_PeerScores(t *testing.T) {
|
|
|
|
|
|
|
|
testTransport := NewTestTraceTransport(t, func(t *testing.T, evt TracerTransportEvent) {
|
2021-09-29 14:49:03 +00:00
|
|
|
require.Equal(t, peerIDA.Pretty(), evt.lotusTraceEvent.PeerID)
|
2021-09-29 14:39:44 +00:00
|
|
|
require.Equal(t, "source-auth-token-test", evt.lotusTraceEvent.SourceAuth)
|
|
|
|
require.Equal(t, float64(32), evt.lotusTraceEvent.PeerScore.Score)
|
|
|
|
|
|
|
|
n := time.Now().UnixNano()
|
|
|
|
require.LessOrEqual(t, *evt.lotusTraceEvent.Timestamp, n)
|
|
|
|
|
2021-09-29 14:49:03 +00:00
|
|
|
require.Equal(t, peerIDA.Pretty(), evt.lotusTraceEvent.PeerScore.PeerID)
|
2021-09-29 14:39:44 +00:00
|
|
|
require.Equal(t, 1, len(evt.lotusTraceEvent.PeerScore.Topics))
|
|
|
|
|
|
|
|
topic := evt.lotusTraceEvent.PeerScore.Topics[0]
|
|
|
|
require.Equal(t, "topicA", topic.Topic)
|
|
|
|
require.Equal(t, float64(100), topic.FirstMessageDeliveries)
|
|
|
|
})
|
|
|
|
|
|
|
|
lt := NewLotusTracer(
|
|
|
|
[]TracerTransport{testTransport},
|
2021-09-29 14:49:03 +00:00
|
|
|
peerIDA,
|
2021-09-29 14:39:44 +00:00
|
|
|
"source-auth-token-test",
|
|
|
|
)
|
|
|
|
|
|
|
|
topics := make(map[string]*pubsub.TopicScoreSnapshot)
|
|
|
|
topics["topicA"] = &pubsub.TopicScoreSnapshot{
|
|
|
|
FirstMessageDeliveries: float64(100),
|
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[peer.ID]*pubsub.PeerScoreSnapshot)
|
2021-09-29 14:49:03 +00:00
|
|
|
m[peerIDA] = &pubsub.PeerScoreSnapshot{
|
2021-09-29 14:39:44 +00:00
|
|
|
Score: float64(32),
|
|
|
|
Topics: topics,
|
|
|
|
}
|
|
|
|
|
|
|
|
lt.PeerScores(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTracer_PubSubTrace(t *testing.T) {
|
2021-09-29 14:49:03 +00:00
|
|
|
n := time.Now().Unix()
|
2021-09-29 14:39:44 +00:00
|
|
|
|
|
|
|
testTransport := NewTestTraceTransport(t, func(t *testing.T, evt TracerTransportEvent) {
|
2021-09-29 14:49:03 +00:00
|
|
|
require.Equal(t, []byte(peerIDA), evt.pubsubTraceEvent.PeerID)
|
2021-09-29 14:39:44 +00:00
|
|
|
require.Equal(t, &n, evt.pubsubTraceEvent.Timestamp)
|
|
|
|
})
|
|
|
|
|
|
|
|
lt := NewLotusTracer(
|
|
|
|
[]TracerTransport{testTransport},
|
|
|
|
"pid",
|
|
|
|
"source-auth",
|
|
|
|
)
|
|
|
|
|
|
|
|
lt.Trace(&pubsub_pb.TraceEvent{
|
2021-09-29 14:49:03 +00:00
|
|
|
PeerID: []byte(peerIDA),
|
2021-09-29 14:39:44 +00:00
|
|
|
Timestamp: &n,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTracer_MultipleTransports(t *testing.T) {
|
|
|
|
testTransportA := NewTestTraceTransport(t, func(t *testing.T, evt TracerTransportEvent) {
|
2021-09-29 14:49:03 +00:00
|
|
|
require.Equal(t, []byte(peerIDA), evt.pubsubTraceEvent.PeerID)
|
2021-09-29 14:39:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
testTransportB := NewTestTraceTransport(t, func(t *testing.T, evt TracerTransportEvent) {
|
2021-09-29 14:49:03 +00:00
|
|
|
require.Equal(t, []byte(peerIDA), evt.pubsubTraceEvent.PeerID)
|
2021-09-29 14:39:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
executeTest := NewLotusTracer(
|
|
|
|
[]TracerTransport{testTransportA, testTransportB},
|
|
|
|
"pid",
|
|
|
|
"source-auth",
|
|
|
|
)
|
|
|
|
|
|
|
|
executeTest.Trace(&pubsub_pb.TraceEvent{
|
2021-09-29 14:49:03 +00:00
|
|
|
PeerID: []byte(peerIDA),
|
2021-09-29 14:39:44 +00:00
|
|
|
})
|
|
|
|
}
|