| 16 | ) |
| 17 | |
| 18 | func 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 | }}, |