2021-06-03 12:23:18 +02:00
|
|
|
package peer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-08-22 12:21:19 +02:00
|
|
|
"time"
|
2024-06-13 01:20:46 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/signal/metrics"
|
2021-06-03 12:23:18 +02:00
|
|
|
)
|
|
|
|
|
2022-08-22 12:21:19 +02:00
|
|
|
func TestRegistry_ShouldNotDeregisterWhenHasNewerStreamRegistered(t *testing.T) {
|
2024-06-13 01:20:46 +02:00
|
|
|
metrics, err := metrics.NewAppMetrics(otel.Meter(""))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
r := NewRegistry(metrics)
|
2022-08-22 12:21:19 +02:00
|
|
|
|
|
|
|
peerID := "peer"
|
|
|
|
|
|
|
|
olderPeer := NewPeer(peerID, nil)
|
|
|
|
r.Register(olderPeer)
|
|
|
|
time.Sleep(time.Nanosecond)
|
|
|
|
|
|
|
|
newerPeer := NewPeer(peerID, nil)
|
|
|
|
r.Register(newerPeer)
|
|
|
|
registered, _ := r.Get(olderPeer.Id)
|
|
|
|
|
|
|
|
assert.NotNil(t, registered, "peer can't be nil")
|
|
|
|
assert.Equal(t, newerPeer, registered)
|
|
|
|
|
|
|
|
r.Deregister(olderPeer)
|
|
|
|
registered, _ = r.Get(olderPeer.Id)
|
|
|
|
|
|
|
|
assert.NotNil(t, registered, "peer can't be nil")
|
|
|
|
assert.Equal(t, newerPeer, registered)
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:08:40 +02:00
|
|
|
func TestRegistry_GetNonExistentPeer(t *testing.T) {
|
2024-06-13 01:20:46 +02:00
|
|
|
metrics, err := metrics.NewAppMetrics(otel.Meter(""))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
r := NewRegistry(metrics)
|
2021-06-10 17:08:40 +02:00
|
|
|
|
|
|
|
peer, ok := r.Get("non_existent_peer")
|
|
|
|
|
|
|
|
if peer != nil {
|
|
|
|
t.Errorf("expected non_existent_peer not found in the registry")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
t.Errorf("expected non_existent_peer not found in the registry")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 12:23:18 +02:00
|
|
|
func TestRegistry_Register(t *testing.T) {
|
2024-06-13 01:20:46 +02:00
|
|
|
metrics, err := metrics.NewAppMetrics(otel.Meter(""))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
r := NewRegistry(metrics)
|
2021-06-03 12:23:18 +02:00
|
|
|
peer1 := NewPeer("test_peer_1", nil)
|
|
|
|
peer2 := NewPeer("test_peer_2", nil)
|
|
|
|
r.Register(peer1)
|
|
|
|
r.Register(peer2)
|
|
|
|
|
2021-06-10 17:08:40 +02:00
|
|
|
if _, ok := r.Get("test_peer_1"); !ok {
|
2021-06-03 12:23:18 +02:00
|
|
|
t.Errorf("expected test_peer_1 not found in the registry")
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:08:40 +02:00
|
|
|
if _, ok := r.Get("test_peer_2"); !ok {
|
2021-06-03 12:23:18 +02:00
|
|
|
t.Errorf("expected test_peer_2 not found in the registry")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegistry_Deregister(t *testing.T) {
|
2024-06-13 01:20:46 +02:00
|
|
|
metrics, err := metrics.NewAppMetrics(otel.Meter(""))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
r := NewRegistry(metrics)
|
2021-06-03 12:23:18 +02:00
|
|
|
peer1 := NewPeer("test_peer_1", nil)
|
|
|
|
peer2 := NewPeer("test_peer_2", nil)
|
|
|
|
r.Register(peer1)
|
|
|
|
r.Register(peer2)
|
|
|
|
|
|
|
|
r.Deregister(peer1)
|
|
|
|
|
2021-06-10 17:08:40 +02:00
|
|
|
if _, ok := r.Get("test_peer_1"); ok {
|
2021-06-03 12:23:18 +02:00
|
|
|
t.Errorf("expected test_peer_1 to absent in the registry after deregistering")
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:08:40 +02:00
|
|
|
if _, ok := r.Get("test_peer_2"); !ok {
|
2021-06-03 12:23:18 +02:00
|
|
|
t.Errorf("expected test_peer_2 not found in the registry")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|