MCPcopy
hub / github.com/canopy-network/canopy / TestMessageCacheAdd

Function TestMessageCacheAdd

lib/peer_test.go:18–144  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

16)
17
18func TestMessageCacheAdd(t *testing.T) {
19 tests := []struct {
20 name string
21 detail string
22 cache MessageCache
23 toAdd *MessageAndMetadata
24 expectedOk bool
25 expected map[string]struct{}
26 }{
27 {
28 name: "exists",
29 detail: "not added as message already exists in cache",
30 cache: MessageCache{
31 queue: list.New(),
32 deDupe: &DeDuplicator[string]{m: map[string]struct{}{
33 func() string {
34 bz, _ := Marshal(&StringWrapper{Value: "b"})
35 return crypto.HashString(bz)
36 }(): {},
37 }},
38 maxSize: 2,
39 },
40 toAdd: &MessageAndMetadata{
41 Message: func() []byte {
42 bz, err := Marshal(&StringWrapper{Value: "b"})
43 require.NoError(t, err)
44 return bz
45 }(),
46 },
47 expected: map[string]struct{}{
48 func() string {
49 bz, _ := Marshal(&StringWrapper{Value: "b"})
50 return crypto.HashString(bz)
51 }(): {},
52 },
53 expectedOk: false,
54 },
55 {
56 name: "ok",
57 detail: "added without eviction",
58 cache: MessageCache{
59 queue: func() (l *list.List) {
60 l = list.New()
61 l.PushFront(&MessageAndMetadata{
62 Message: func() []byte {
63 bz, err := Marshal(&StringWrapper{Value: "b"})
64 require.NoError(t, err)
65 return bz
66 }(),
67 })
68 return
69 }(),
70 deDupe: &DeDuplicator[string]{m: map[string]struct{}{
71 func() string {
72 bz, _ := Marshal(&StringWrapper{Value: "b"})
73 return crypto.HashString(bz)
74 }(): {},
75 }},

Callers

nothing calls this directly

Calls 7

HashStringFunction · 0.92
RunMethod · 0.80
EqualMethod · 0.80
MapMethod · 0.80
MarshalFunction · 0.70
NewMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected