go-libp2p-pubsub/mcache_test.go

168 lines
3.0 KiB
Go
Raw Normal View History

package pubsub
2018-02-21 09:18:56 +00:00
import (
"encoding/binary"
"fmt"
"testing"
pb "github.com/libp2p/go-libp2p-pubsub/pb"
2018-02-21 09:18:56 +00:00
)
func TestMessageCache(t *testing.T) {
mcache := NewMessageCache(3, 5)
msgID := DefaultMsgIdFn
2018-02-21 09:18:56 +00:00
msgs := make([]*pb.Message, 60)
for i := range msgs {
msgs[i] = makeTestMessage(i)
}
for i := 0; i < 10; i++ {
mcache.Put(msgs[i])
}
for i := 0; i < 10; i++ {
mid := msgID(msgs[i])
m, ok := mcache.Get(mid)
if !ok {
2018-02-21 11:05:26 +00:00
t.Fatalf("Message %d not in cache", i)
2018-02-21 09:18:56 +00:00
}
if m != msgs[i] {
2018-02-21 11:05:26 +00:00
t.Fatalf("Message %d does not match cache", i)
2018-02-21 09:18:56 +00:00
}
}
gids := mcache.GetGossipIDs("test")
if len(gids) != 10 {
2018-02-21 11:05:26 +00:00
t.Fatalf("Expected 10 gossip IDs; got %d", len(gids))
2018-02-21 09:18:56 +00:00
}
for i := 0; i < 10; i++ {
mid := msgID(msgs[i])
if mid != gids[i] {
2018-02-21 11:05:26 +00:00
t.Fatalf("GossipID mismatch for message %d", i)
2018-02-21 09:18:56 +00:00
}
}
mcache.Shift()
for i := 10; i < 20; i++ {
mcache.Put(msgs[i])
}
for i := 0; i < 20; i++ {
mid := msgID(msgs[i])
m, ok := mcache.Get(mid)
if !ok {
2018-02-21 11:05:26 +00:00
t.Fatalf("Message %d not in cache", i)
2018-02-21 09:18:56 +00:00
}
if m != msgs[i] {
2018-02-21 11:05:26 +00:00
t.Fatalf("Message %d does not match cache", i)
2018-02-21 09:18:56 +00:00
}
}
gids = mcache.GetGossipIDs("test")
if len(gids) != 20 {
2018-02-21 11:05:26 +00:00
t.Fatalf("Expected 20 gossip IDs; got %d", len(gids))
2018-02-21 09:18:56 +00:00
}
for i := 0; i < 10; i++ {
mid := msgID(msgs[i])
if mid != gids[10+i] {
2018-02-21 11:05:26 +00:00
t.Fatalf("GossipID mismatch for message %d", i)
2018-02-21 09:18:56 +00:00
}
}
for i := 10; i < 20; i++ {
mid := msgID(msgs[i])
if mid != gids[i-10] {
2018-02-21 11:05:26 +00:00
t.Fatalf("GossipID mismatch for message %d", i)
2018-02-21 09:18:56 +00:00
}
}
mcache.Shift()
for i := 20; i < 30; i++ {
mcache.Put(msgs[i])
}
mcache.Shift()
for i := 30; i < 40; i++ {
mcache.Put(msgs[i])
}
mcache.Shift()
for i := 40; i < 50; i++ {
mcache.Put(msgs[i])
}
mcache.Shift()
for i := 50; i < 60; i++ {
mcache.Put(msgs[i])
}
if len(mcache.msgs) != 50 {
2018-02-21 11:05:26 +00:00
t.Fatalf("Expected 50 messages in the cache; got %d", len(mcache.msgs))
2018-02-21 09:18:56 +00:00
}
for i := 0; i < 10; i++ {
mid := msgID(msgs[i])
_, ok := mcache.Get(mid)
if ok {
2018-02-21 11:05:26 +00:00
t.Fatalf("Message %d still in cache", i)
2018-02-21 09:18:56 +00:00
}
}
for i := 10; i < 60; i++ {
mid := msgID(msgs[i])
m, ok := mcache.Get(mid)
if !ok {
2018-02-21 11:05:26 +00:00
t.Fatalf("Message %d not in cache", i)
2018-02-21 09:18:56 +00:00
}
if m != msgs[i] {
2018-02-21 11:05:26 +00:00
t.Fatalf("Message %d does not match cache", i)
2018-02-21 09:18:56 +00:00
}
}
gids = mcache.GetGossipIDs("test")
if len(gids) != 30 {
2018-02-21 11:05:26 +00:00
t.Fatalf("Expected 30 gossip IDs; got %d", len(gids))
2018-02-21 09:18:56 +00:00
}
for i := 0; i < 10; i++ {
mid := msgID(msgs[50+i])
if mid != gids[i] {
2018-02-21 11:05:26 +00:00
t.Fatalf("GossipID mismatch for message %d", i)
2018-02-21 09:18:56 +00:00
}
}
for i := 10; i < 20; i++ {
mid := msgID(msgs[30+i])
if mid != gids[i] {
2018-02-21 11:05:26 +00:00
t.Fatalf("GossipID mismatch for message %d", i)
2018-02-21 09:18:56 +00:00
}
}
for i := 20; i < 30; i++ {
mid := msgID(msgs[10+i])
if mid != gids[i] {
2018-02-21 11:05:26 +00:00
t.Fatalf("GossipID mismatch for message %d", i)
2018-02-21 09:18:56 +00:00
}
}
}
func makeTestMessage(n int) *pb.Message {
seqno := make([]byte, 8)
binary.BigEndian.PutUint64(seqno, uint64(n))
data := []byte(fmt.Sprintf("%d", n))
2020-09-29 15:05:54 +00:00
topic := "test"
2018-02-21 09:18:56 +00:00
return &pb.Message{
2020-09-29 15:05:54 +00:00
Data: data,
Topic: &topic,
From: []byte("test"),
Seqno: seqno,
2018-02-21 09:18:56 +00:00
}
}