Skip to content

Commit

Permalink
test: TestJobUpdateActualIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaranski committed Jan 16, 2025
1 parent a7d425b commit af4e60e
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions daemon/job_actual_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"time"

"github.com/NordSecurity/nordvpn-linux/core"
"github.com/NordSecurity/nordvpn-linux/daemon/events"
daemonEvents "github.com/NordSecurity/nordvpn-linux/daemon/events"
"github.com/NordSecurity/nordvpn-linux/daemon/pb"
"github.com/NordSecurity/nordvpn-linux/daemon/state"
"github.com/NordSecurity/nordvpn-linux/events"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -60,7 +63,7 @@ func TestInsightsIPUntilSuccess(t *testing.T) {
},
{
name: "Context canceled",
ctxTimeout: 0,
ctxTimeout: time.Millisecond,
insightsAPI: func() insightFunc {
return func() (*core.Insights, error) {
time.Sleep(10 * time.Millisecond)
Expand Down Expand Up @@ -168,7 +171,7 @@ func TestUpdateActualIP(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

dm := NewDataManager("", "", "", "", events.NewDataUpdateEvents())
dm := NewDataManager("", "", "", "", daemonEvents.NewDataUpdateEvents())
api := &mockInsights{
insightsFunc: tt.insightsAPI,
}
Expand All @@ -185,3 +188,38 @@ func TestUpdateActualIP(t *testing.T) {
})
}
}

func receiveWithTimeout(t *testing.T, ch <-chan interface{}) interface{} {
const TIMEOUT time.Duration = time.Second * 5
select {
case msg := <-ch:
return msg
case <-time.After(TIMEOUT):
t.Fatal("no message received")
}
return nil
}

func TestJobUpdateActualIP(t *testing.T) {
address := netip.AddrFrom4([4]byte{192, 168, 1, 2})
rpc := testRPC()
rpc.statePublisher = state.NewState()
stateChan, _ := rpc.statePublisher.AddSubscriber()
api := &mockInsights{
insightsFunc: func() (*core.Insights, error) {
return &core.Insights{IP: address.String()}, nil
},
}
go JobActualIP(rpc.statePublisher, rpc.dm, api)
time.Sleep(time.Millisecond * 100)
assert.Equal(t, netip.Addr{}, rpc.dm.GetActualIP())
eventConnect := events.DataConnect{}
go func() {
err := rpc.statePublisher.NotifyConnect(eventConnect)
assert.NoError(t, err)
}()

assert.Equal(t, eventConnect, receiveWithTimeout(t, stateChan))
assert.Equal(t, pb.UpdateEvent_ACTUAL_IP_UPDATE, receiveWithTimeout(t, stateChan))
assert.Equal(t, address, rpc.dm.GetActualIP())
}

0 comments on commit af4e60e

Please sign in to comment.