Files
akvorado/common/helpers/intern_test.go
Vincent Bernat c769bb5234 inlet/bmp: initial support for BMP protocol
At first, there was a tentative to use BMP collector implementation
from bio-rd. However, this current implementation is using GoBGP
instead:

- BMP is very simple from a protocol point of view. The hard work is
  mostly around decoding. Both bio-rd and GoBGP can decode, but for
  testing, GoBGP is able to generate messages as well (this is its
  primary purpose, I suppose parsing was done for testing purpose).
  Using only one library is always better. An alternative would be
  GoBMP, but it also only do parsing.
- Logging and metrics can be customized easily (but the work was done
  for bio-rd, so not a real argument).
- bio-rd is an application and there is no API stability (and I did
  that too)
- GoBGP supports FlowSpec, which may be useful in the future for the
  DDoS part. Again, one library for everything is better (but
  honestly, GoBGP as a lib is not the best part of it, maybe
  github.com/jwhited/corebgp would be a better fit while keeping GoBGP
  for decoding/encoding).

There was a huge effort around having a RIB which is efficient
memory-wise (data are interned to save memory), performant during
reads, while being decent during insertions. We rely on a patched
version of Kentik's Patricia trees to be able to apply mutations to
the tree.

There was several tentatives to implement some kind of graceful
restart, but ultimetaly, the design is kept simple: when a BMP
connection goes down, routes will be removed after a configurable
time. If the connection comes back up, then it is just considered new.
It would have been ideal to rely on EoR markers, but the RFC is
unclear about them, and they are likely to be per peer, making it
difficult to know what to do if one peer is back, but not the other.

Remaining tasks:

- [ ] Confirm support for LocRIB
- [ ] Import data in ClickHouse
- [ ] Make data available in the frontend

Fix #52
2022-09-27 00:34:41 +02:00

150 lines
3.6 KiB
Go

// SPDX-FileCopyrightText: 2022 Free Mobile
// SPDX-License-Identifier: AGPL-3.0-only
package helpers
import "testing"
type likeInt int
func (i likeInt) Equal(j likeInt) bool { return i == j }
func (i likeInt) Hash() uint64 { return uint64(i) % 10 }
func TestPut(t *testing.T) {
p := NewInternPool[likeInt]()
a := p.Put(likeInt(10))
b := p.Put(likeInt(10))
c := p.Put(likeInt(11))
d := p.Put(likeInt(12))
if a != b {
t.Error("got two references for Put(10)")
}
if a == c || a == d || c == d {
t.Error("got same reference for Put(10)/Put(11)/Put(12)")
}
if p.Get(a) != likeInt(10) {
t.Errorf("Get(Put(10)) == %d != 10", p.Get(a))
}
if p.Get(c) != likeInt(11) {
t.Errorf("Get(Put(11)) == %d != 10", p.Get(c))
}
if p.Get(d) != likeInt(12) {
t.Errorf("Get(Put(12)) == %d != 10", p.Get(d))
}
}
func TestPutCollision(t *testing.T) {
p := NewInternPool[likeInt]()
a := p.Put(likeInt(10))
b := p.Put(likeInt(20))
c := p.Put(likeInt(11))
d := p.Put(likeInt(21))
if a == b || a == c || a == d || b == c || b == d || c == d {
t.Error("got same reference for two different values")
}
}
func TestTake(t *testing.T) {
p := NewInternPool[likeInt]()
val1 := likeInt(10)
ref1 := p.Put(val1)
val2 := likeInt(10)
ref2 := p.Put(val2)
val3 := likeInt(12)
ref3 := p.Put(val3)
val4 := likeInt(22) // collision
ref4 := p.Put(val4)
val5 := likeInt(32)
ref5 := p.Put(val5)
expectedValues := []internValue[likeInt]{
{},
{value: 10, refCount: 2},
{value: 12, refCount: 1, next: 3},
{value: 22, refCount: 1, previous: 2, next: 4},
{value: 32, refCount: 1, previous: 3},
}
if diff := Diff(p.values, expectedValues, DiffUnexported); diff != "" {
t.Fatalf("p.values (-got, +want):\n%s", diff)
}
p.Take(ref4)
expectedValues = []internValue[likeInt]{
{},
{value: 10, refCount: 2},
{value: 12, refCount: 1, next: 4},
{value: 22, refCount: 0, previous: 2, next: 4}, // free
{value: 32, refCount: 1, previous: 2},
}
if diff := Diff(p.values, expectedValues, DiffUnexported); diff != "" {
t.Fatalf("p.values (-got, +want):\n%s", diff)
}
ref6 := p.Put(likeInt(42))
if ref6 != ref4 {
t.Fatal("p.Put() did not reuse free slot")
}
expectedValues = []internValue[likeInt]{
{},
{value: 10, refCount: 2},
{value: 12, refCount: 1, next: 4},
{value: 42, refCount: 1, previous: 4},
{value: 32, refCount: 1, previous: 2, next: 3},
}
if diff := Diff(p.values, expectedValues, DiffUnexported); diff != "" {
t.Fatalf("p.values (-got, +want):\n%s", diff)
}
p.Take(ref3)
expectedValues = []internValue[likeInt]{
{},
{value: 10, refCount: 2},
{value: 12, refCount: 0, next: 4}, // free
{value: 42, refCount: 1, previous: 4},
{value: 32, refCount: 1, next: 3},
}
if diff := Diff(p.values, expectedValues, DiffUnexported); diff != "" {
t.Fatalf("p.values (-got, +want):\n%s", diff)
}
p.Take(ref5)
expectedValues = []internValue[likeInt]{
{},
{value: 10, refCount: 2},
{value: 12, refCount: 0, next: 4}, // free
{value: 42, refCount: 1},
{value: 32, refCount: 0, next: 3}, // free
}
if diff := Diff(p.values, expectedValues, DiffUnexported); diff != "" {
t.Fatalf("p.values (-got, +want):\n%s", diff)
}
p.Take(ref6)
expectedValues = []internValue[likeInt]{
{},
{value: 10, refCount: 2},
{value: 12, refCount: 0, next: 4}, // free
{value: 42, refCount: 0}, // free
{value: 32, refCount: 0, next: 3}, // free
}
if diff := Diff(p.values, expectedValues, DiffUnexported); diff != "" {
t.Fatalf("p.values (-got, +want):\n%s", diff)
}
p.Take(ref1)
p.Take(ref2)
diff := p.Len()
if diff != 0 {
t.Fatalf("Take() didn't free everything (%d remaining)", diff)
}
}