Skip to content

Commit

Permalink
add test for score adjustment from topis params reset
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Sep 9, 2020
1 parent c82d664 commit ffa2e3e
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,68 @@ func TestScoreRecapTopicParams(t *testing.T) {
}
}

func TestScoreResetTopicParams(t *testing.T) {
// Create parameters with reasonable default values
mytopic := "mytopic"
params := &PeerScoreParams{
AppSpecificScore: func(peer.ID) float64 { return 0 },
Topics: make(map[string]*TopicScoreParams),
}
topicScoreParams := &TopicScoreParams{
TopicWeight: 1,
TimeInMeshQuantum: time.Second,
InvalidMessageDeliveriesWeight: -1,
InvalidMessageDeliveriesDecay: 1.0,
}

params.Topics[mytopic] = topicScoreParams

// peer A always delivers the message first.
// peer B delivers next (within the delivery window).
// peer C delivers outside the delivery window.
// we expect peers A and B to have a score of zero, since all other parameter weights are zero.
// Peer C should have a negative score.
peerA := peer.ID("A")

ps := newPeerScore(params)
ps.AddPeer(peerA, "myproto")

// reject a bunch of messages
nMessages := 100
for i := 0; i < nMessages; i++ {
pbMsg := makeTestMessage(i)
pbMsg.TopicIDs = []string{mytopic}
msg := Message{ReceivedFrom: peerA, Message: pbMsg}
ps.ValidateMessage(&msg)
ps.RejectMessage(&msg, rejectValidationFailed)
}

// check the topic score
aScore := ps.Score(peerA)
if aScore != -10000 {
t.Fatalf("expected a -10000 score, but got %f instead", aScore)
}

// reset the topic paramaters recapping the deliveries counters
newTopicScoreParams := &TopicScoreParams{
TopicWeight: 1,
TimeInMeshQuantum: time.Second,
InvalidMessageDeliveriesWeight: -10,
InvalidMessageDeliveriesDecay: 1.0,
}

err := ps.SetTopicScoreParams(mytopic, newTopicScoreParams)
if err != nil {
t.Fatal(err)
}

// verify the topic score was adjusted
aScore = ps.Score(peerA)
if aScore != -100000 {
t.Fatalf("expected a -1000000 score, but got %f instead", aScore)
}
}

func withinVariance(score float64, expected float64, variance float64) bool {
if expected >= 0 {
return score > expected*(1-variance) && score < expected*(1+variance)
Expand Down

0 comments on commit ffa2e3e

Please sign in to comment.