2021-08-29 17:48:31 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2021-08-29 17:48:31 +02:00
|
|
|
"testing"
|
2022-08-27 12:57:03 +02:00
|
|
|
"time"
|
2023-11-16 18:21:52 +01:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/proto"
|
2021-08-29 17:48:31 +02:00
|
|
|
)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
// var peersUpdater *PeersUpdateManager
|
2021-08-29 17:48:31 +02:00
|
|
|
|
|
|
|
func TestCreateChannel(t *testing.T) {
|
|
|
|
peer := "test-create"
|
2023-11-16 18:21:52 +01:00
|
|
|
peersUpdater := NewPeersUpdateManager(nil)
|
2024-07-03 11:33:02 +02:00
|
|
|
defer peersUpdater.CloseChannel(context.Background(), peer)
|
2021-08-29 17:48:31 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
_ = peersUpdater.CreateChannel(context.Background(), peer)
|
2021-08-29 17:48:31 +02:00
|
|
|
if _, ok := peersUpdater.peerChannels[peer]; !ok {
|
|
|
|
t.Error("Error creating the channel")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendUpdate(t *testing.T) {
|
|
|
|
peer := "test-sendupdate"
|
2023-11-16 18:21:52 +01:00
|
|
|
peersUpdater := NewPeersUpdateManager(nil)
|
2022-08-27 12:57:03 +02:00
|
|
|
update1 := &UpdateMessage{Update: &proto.SyncResponse{
|
|
|
|
NetworkMap: &proto.NetworkMap{
|
|
|
|
Serial: 0,
|
|
|
|
},
|
|
|
|
}}
|
2024-07-03 11:33:02 +02:00
|
|
|
_ = peersUpdater.CreateChannel(context.Background(), peer)
|
2021-08-29 17:48:31 +02:00
|
|
|
if _, ok := peersUpdater.peerChannels[peer]; !ok {
|
|
|
|
t.Error("Error creating the channel")
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
peersUpdater.SendUpdate(context.Background(), peer, update1)
|
2021-08-29 17:48:31 +02:00
|
|
|
select {
|
|
|
|
case <-peersUpdater.peerChannels[peer]:
|
|
|
|
default:
|
|
|
|
t.Error("Update wasn't send")
|
|
|
|
}
|
2022-08-27 12:57:03 +02:00
|
|
|
|
|
|
|
for range [channelBufferSize]int{} {
|
2024-07-03 11:33:02 +02:00
|
|
|
peersUpdater.SendUpdate(context.Background(), peer, update1)
|
2022-08-27 12:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update2 := &UpdateMessage{Update: &proto.SyncResponse{
|
|
|
|
NetworkMap: &proto.NetworkMap{
|
|
|
|
Serial: 10,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peersUpdater.SendUpdate(context.Background(), peer, update2)
|
2022-08-27 12:57:03 +02:00
|
|
|
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]:
|
|
|
|
if updateReader.Update.NetworkMap.Serial == update2.Update.NetworkMap.Serial {
|
|
|
|
t.Error("got the update that shouldn't have been sent")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseChannel(t *testing.T) {
|
|
|
|
peer := "test-close"
|
2023-11-16 18:21:52 +01:00
|
|
|
peersUpdater := NewPeersUpdateManager(nil)
|
2024-07-03 11:33:02 +02:00
|
|
|
_ = peersUpdater.CreateChannel(context.Background(), peer)
|
2021-08-29 17:48:31 +02:00
|
|
|
if _, ok := peersUpdater.peerChannels[peer]; !ok {
|
|
|
|
t.Error("Error creating the channel")
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
peersUpdater.CloseChannel(context.Background(), peer)
|
2021-08-29 17:48:31 +02:00
|
|
|
if _, ok := peersUpdater.peerChannels[peer]; ok {
|
|
|
|
t.Error("Error closing the channel")
|
|
|
|
}
|
|
|
|
}
|