2022-01-18 16:44:58 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2024-05-07 18:50:34 +02:00
|
|
|
"context"
|
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/signal/proto"
|
2022-01-18 16:44:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type MockClient struct {
|
2024-10-24 11:43:14 +02:00
|
|
|
CloseFunc func() error
|
|
|
|
GetStatusFunc func() Status
|
|
|
|
StreamConnectedFunc func() bool
|
|
|
|
ReadyFunc func() bool
|
|
|
|
WaitStreamConnectedFunc func()
|
|
|
|
ReceiveFunc func(ctx context.Context, msgHandler func(msg *proto.Message) error) error
|
|
|
|
SendToStreamFunc func(msg *proto.EncryptedMessage) error
|
|
|
|
SendFunc func(msg *proto.Message) error
|
|
|
|
SetOnReconnectedListenerFunc func(f func())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOnReconnectedListener sets the function to be called when the client reconnects.
|
|
|
|
func (sm *MockClient) SetOnReconnectedListener(_ func()) {
|
|
|
|
// Do nothing
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
|
|
|
|
2024-01-22 12:20:24 +01:00
|
|
|
func (sm *MockClient) IsHealthy() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
func (sm *MockClient) Close() error {
|
|
|
|
if sm.CloseFunc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return sm.CloseFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *MockClient) GetStatus() Status {
|
|
|
|
if sm.GetStatusFunc == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return sm.GetStatusFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *MockClient) StreamConnected() bool {
|
|
|
|
if sm.StreamConnectedFunc == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return sm.StreamConnectedFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *MockClient) Ready() bool {
|
|
|
|
if sm.ReadyFunc == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return sm.ReadyFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *MockClient) WaitStreamConnected() {
|
|
|
|
if sm.WaitStreamConnectedFunc == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sm.WaitStreamConnectedFunc()
|
|
|
|
}
|
|
|
|
|
2024-05-07 18:50:34 +02:00
|
|
|
func (sm *MockClient) Receive(ctx context.Context, msgHandler func(msg *proto.Message) error) error {
|
2022-01-18 16:44:58 +01:00
|
|
|
if sm.ReceiveFunc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-07 18:50:34 +02:00
|
|
|
return sm.ReceiveFunc(ctx, msgHandler)
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *MockClient) SendToStream(msg *proto.EncryptedMessage) error {
|
|
|
|
if sm.SendToStreamFunc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return sm.SendToStreamFunc(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *MockClient) Send(msg *proto.Message) error {
|
|
|
|
if sm.SendFunc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return sm.SendFunc(msg)
|
|
|
|
}
|