-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marius Sincovici <marius.sincovici@nordsec.com>
- Loading branch information
1 parent
4e09d14
commit 0444cee
Showing
13 changed files
with
423 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package models | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type CachedValue[T comparable] struct { | ||
value T | ||
latestError error | ||
cachedDate time.Time | ||
validity time.Duration | ||
updaterFn func(self *CachedValue[T]) | ||
mu *sync.Mutex | ||
} | ||
|
||
func NewCachedValue[T comparable]( | ||
value T, | ||
err error, | ||
cachedDate time.Time, | ||
validity time.Duration, | ||
updaterFn func(*CachedValue[T]), | ||
) *CachedValue[T] { | ||
return &CachedValue[T]{ | ||
value: value, | ||
latestError: err, | ||
cachedDate: cachedDate, | ||
validity: validity, | ||
updaterFn: updaterFn, | ||
mu: &sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (c *CachedValue[T]) Get() (T, error) { | ||
shouldUpdate := false | ||
c.mu.Lock() | ||
defer func() { | ||
c.mu.Unlock() | ||
if shouldUpdate { | ||
c.updaterFn(c) | ||
} | ||
}() | ||
shouldUpdate = c.cachedDate.Add(c.validity).Before(time.Now()) | ||
return c.value, c.latestError | ||
} | ||
|
||
func (c *CachedValue[T]) Set(value T, err error) bool { | ||
|
||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
c.cachedDate = time.Now() | ||
updated := c.value == value | ||
c.value = value | ||
c.latestError = err | ||
|
||
return updated | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package models_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/NordSecurity/nordvpn-linux/daemon/models" | ||
"github.com/NordSecurity/nordvpn-linux/test/category" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCachedValue(t *testing.T) { | ||
category.Set(t, category.Unit) | ||
|
||
tests := []struct { | ||
name string | ||
initialValue int | ||
err error | ||
validity time.Duration | ||
updatedValue int | ||
shouldUpdate bool | ||
}{ | ||
{ | ||
name: "works for initial value and update not called", | ||
initialValue: 1, | ||
err: nil, | ||
validity: 1 * time.Minute, | ||
shouldUpdate: false, | ||
}, | ||
{ | ||
name: "update callback function works", | ||
initialValue: 2, | ||
err: nil, | ||
validity: 0 * time.Second, | ||
shouldUpdate: true, | ||
updatedValue: 10, | ||
}, | ||
{ | ||
name: "initial value is error and update fixes it", | ||
initialValue: 0, | ||
err: errors.New("failed"), | ||
validity: 0 * time.Second, | ||
shouldUpdate: true, | ||
updatedValue: 10, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
updateCalled := false | ||
item := models.NewCachedValue( | ||
test.initialValue, | ||
test.err, time.Now(), | ||
test.validity, | ||
func(self *models.CachedValue[int]) { | ||
updateCalled = true | ||
self.Set(test.updatedValue, nil) | ||
}, | ||
) | ||
value, err := item.Get() | ||
|
||
if err != nil { | ||
assert.Equal(t, value, test.initialValue) | ||
} | ||
assert.ErrorIs(t, err, test.err) | ||
|
||
assert.Equal(t, updateCalled, test.shouldUpdate) | ||
if test.shouldUpdate { | ||
value, err := item.Get() | ||
assert.Equal(t, value, test.updatedValue) | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package internal | ||
|
||
import "github.com/NordSecurity/nordvpn-linux/meshnet/pb" | ||
|
||
type MeshnetChecker interface { | ||
IsMeshnetOn() bool | ||
} | ||
|
||
type MeshnetFetcher interface { | ||
FetchMeshnetPeers() *pb.GetPeersResponse | ||
} | ||
|
||
type MeshnetDataManager interface { | ||
GetMeshnetPeers() (*pb.GetPeersResponse, error) | ||
SetMeshnetPeers(peers *pb.GetPeersResponse, err error) bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.