Files
netbird/management/server/updatechannel_test.go
2024-11-06 20:29:59 +01:00

117 lines
3.2 KiB
Go

package server
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/netbirdio/netbird/management/proto"
)
// var peersUpdater *PeersUpdateManager
func TestCreateChannel(t *testing.T) {
peer := "test-create"
peersUpdater := NewPeersUpdateManager(nil)
defer peersUpdater.CloseChannel(context.Background(), peer, "sessionID")
_ = peersUpdater.CreateChannel(context.Background(), peer)
if _, ok := peersUpdater.peerChannels[peer]; !ok {
t.Error("Error creating the channel")
}
}
func TestSendUpdate(t *testing.T) {
peer := "test-sendupdate"
peersUpdater := NewPeersUpdateManager(nil)
update1 := &UpdateMessage{Update: &proto.SyncResponse{
NetworkMap: &proto.NetworkMap{
Serial: 0,
},
}}
_ = peersUpdater.CreateChannel(context.Background(), peer)
if _, ok := peersUpdater.peerChannels[peer]; !ok {
t.Error("Error creating the channel")
}
peersUpdater.SendUpdate(context.Background(), peer, update1)
select {
case <-peersUpdater.peerChannels[peer].channel:
default:
t.Error("Update wasn't send")
}
for range [channelBufferSize]int{} {
peersUpdater.SendUpdate(context.Background(), peer, update1)
}
update2 := &UpdateMessage{Update: &proto.SyncResponse{
NetworkMap: &proto.NetworkMap{
Serial: 10,
},
}}
peersUpdater.SendUpdate(context.Background(), peer, update2)
timeout := time.After(5 * time.Second)
for range [channelBufferSize]int{} {
select {
case <-timeout:
t.Error("timed out reading previously sent updates")
case updateReader := <-peersUpdater.peerChannels[peer].channel:
if updateReader.Update.NetworkMap.Serial == update2.Update.NetworkMap.Serial {
t.Error("got the update that shouldn't have been sent")
}
}
}
}
func TestCloseChannel_WithCorrectSessionID(t *testing.T) {
peer := "test-close"
peersUpdater := NewPeersUpdateManager(nil)
peerUpdates := peersUpdater.CreateChannel(context.Background(), peer)
if _, ok := peersUpdater.peerChannels[peer]; !ok {
t.Error("Error creating the channel")
}
updateDB := peersUpdater.CloseChannel(context.Background(), peer, peerUpdates.sessionID)
if _, ok := peersUpdater.peerChannels[peer]; ok {
t.Error("Error closing the channel")
}
assert.Equal(t, true, updateDB)
}
func TestCloseChannel_WithWrongSessionID(t *testing.T) {
peer := "test-close"
peersUpdater := NewPeersUpdateManager(nil)
peersUpdater.CreateChannel(context.Background(), peer)
if _, ok := peersUpdater.peerChannels[peer]; !ok {
t.Error("Error creating the channel")
}
updateDB := peersUpdater.CloseChannel(context.Background(), peer, "wrongSessionID")
if _, ok := peersUpdater.peerChannels[peer]; !ok {
t.Error("Should not close channel with wrong session id")
}
assert.Equal(t, false, updateDB)
}
func TestCloseChannel_WithForceOverwrite(t *testing.T) {
peer := "test-close"
peersUpdater := NewPeersUpdateManager(nil)
peersUpdater.CreateChannel(context.Background(), peer)
if _, ok := peersUpdater.peerChannels[peer]; !ok {
t.Error("Error creating the channel")
}
updateDB := peersUpdater.CloseChannel(context.Background(), peer, SessionIdForceOverwrite)
if _, ok := peersUpdater.peerChannels[peer]; ok {
t.Error("Should close channel if forced")
}
assert.Equal(t, true, updateDB)
}