-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GO-2828: add debug diagnostics for spaces and connections #314
Open
AnastasiaShemyakinskaya
wants to merge
6
commits into
main
Choose a base branch
from
go-2828-add-debug-diagnostics-for-spaces-and-connections
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+206
−4
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cb2af70
GO-2828: add peer stat
AnastasiaShemyakinskaya a3c50a4
GO-2828: add more fields
AnastasiaShemyakinskaya f85006c
Merge branch 'main' of github.com:anyproto/any-sync into go-2828-add-…
AnastasiaShemyakinskaya c0f3ea7
GO-2828: fix comments
AnastasiaShemyakinskaya 87f038c
Merge branch 'main' of github.com:anyproto/any-sync into go-2828-add-…
AnastasiaShemyakinskaya abaaa02
GO-2828: partly fixed comments
AnastasiaShemyakinskaya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ func TestPool_Get(t *testing.T) { | |
fx := newFixture(t) | ||
defer fx.Finish() | ||
fx.Dialer.dial = func(ctx context.Context, peerId string) (peer peer.Peer, err error) { | ||
return newTestPeer("1"), nil | ||
return newTestPeer("1", time.Now(), 0, 0), nil | ||
} | ||
p, err := fx.Get(ctx, "1") | ||
assert.NoError(t, err) | ||
|
@@ -49,15 +49,15 @@ func TestPool_Get(t *testing.T) { | |
t.Run("retry for closed", func(t *testing.T) { | ||
fx := newFixture(t) | ||
defer fx.Finish() | ||
tp := newTestPeer("1") | ||
tp := newTestPeer("1", time.Now(), 0, 0) | ||
fx.Dialer.dial = func(ctx context.Context, peerId string) (peer peer.Peer, err error) { | ||
return tp, nil | ||
} | ||
p, err := fx.Get(ctx, "1") | ||
assert.NoError(t, err) | ||
assert.NotNil(t, p) | ||
p.Close() | ||
tp2 := newTestPeer("1") | ||
tp2 := newTestPeer("1", time.Now(), 0, 0) | ||
fx.Dialer.dial = func(ctx context.Context, peerId string) (peer peer.Peer, err error) { | ||
return tp2, nil | ||
} | ||
|
@@ -80,7 +80,7 @@ func TestPool_GetOneOf(t *testing.T) { | |
t.Run("from cache", func(t *testing.T) { | ||
fx := newFixture(t) | ||
defer fx.Finish() | ||
tp1 := newTestPeer("1") | ||
tp1 := newTestPeer("1", time.Now(), 0, 0) | ||
addToCache(t, fx, tp1) | ||
p, err := fx.GetOneOf(ctx, []string{"3", "2", "1"}) | ||
require.NoError(t, err) | ||
|
@@ -89,10 +89,10 @@ func TestPool_GetOneOf(t *testing.T) { | |
t.Run("from cache - skip closed", func(t *testing.T) { | ||
fx := newFixture(t) | ||
defer fx.Finish() | ||
tp2 := newTestPeer("2") | ||
tp2 := newTestPeer("2", time.Now(), 0, 0) | ||
addToCache(t, fx, tp2) | ||
tp2.Close() | ||
tp1 := newTestPeer("1") | ||
tp1 := newTestPeer("1", time.Now(), 0, 0) | ||
addToCache(t, fx, tp1) | ||
p, err := fx.GetOneOf(ctx, []string{"3", "2", "1"}) | ||
require.NoError(t, err) | ||
|
@@ -107,7 +107,7 @@ func TestPool_GetOneOf(t *testing.T) { | |
return nil, fmt.Errorf("not expected call") | ||
} | ||
called = true | ||
return newTestPeer(peerId), nil | ||
return newTestPeer(peerId, time.Now(), 0, 0), nil | ||
} | ||
p, err := fx.GetOneOf(ctx, []string{"3", "2", "1"}) | ||
require.NoError(t, err) | ||
|
@@ -139,12 +139,12 @@ func TestPool_AddPeer(t *testing.T) { | |
t.Run("success", func(t *testing.T) { | ||
fx := newFixture(t) | ||
defer fx.Finish() | ||
require.NoError(t, fx.AddPeer(ctx, newTestPeer("p1"))) | ||
require.NoError(t, fx.AddPeer(ctx, newTestPeer("p1", time.Now(), 0, 0))) | ||
}) | ||
t.Run("two peers", func(t *testing.T) { | ||
fx := newFixture(t) | ||
defer fx.Finish() | ||
p1, p2 := newTestPeer("p1"), newTestPeer("p1") | ||
p1, p2 := newTestPeer("p1", time.Now(), 0, 0), newTestPeer("p1", time.Now(), 0, 0) | ||
require.NoError(t, fx.AddPeer(ctx, p1)) | ||
require.NoError(t, fx.AddPeer(ctx, p2)) | ||
select { | ||
|
@@ -167,7 +167,7 @@ func TestPool_Pick(t *testing.T) { | |
t.Run("success", func(t *testing.T) { | ||
fx := newFixture(t) | ||
defer fx.Finish() | ||
p1 := newTestPeer("p1") | ||
p1 := newTestPeer("p1", time.Now(), 0, 0) | ||
require.NoError(t, fx.AddPeer(ctx, p1)) | ||
|
||
p, err := fx.Pick(ctx, "p1") | ||
|
@@ -179,7 +179,7 @@ func TestPool_Pick(t *testing.T) { | |
t.Run("peer is closed", func(t *testing.T) { | ||
fx := newFixture(t) | ||
defer fx.Finish() | ||
p1 := newTestPeer("p1") | ||
p1 := newTestPeer("p1", time.Now(), 0, 0) | ||
require.NoError(t, fx.AddPeer(ctx, p1)) | ||
require.NoError(t, p1.Close()) | ||
p, err := fx.Pick(ctx, "p1") | ||
|
@@ -189,6 +189,123 @@ func TestPool_Pick(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestProvideStat_NoPeers(t *testing.T) { | ||
t.Run("only incoming peers", func(t *testing.T) { | ||
// given | ||
fx := newFixture(t) | ||
defer fx.Finish() | ||
created := time.Now() | ||
p1 := newTestPeer("p1", created, 1, 1) | ||
require.NoError(t, fx.AddPeer(ctx, p1)) | ||
|
||
statProvider, ok := fx.Service.(*poolService) | ||
assert.True(t, ok) | ||
|
||
// when | ||
stat := statProvider.ProvideStat() | ||
|
||
// then | ||
assert.NotNil(t, stat) | ||
poolStat, ok := stat.(*poolStats) | ||
assert.True(t, ok) | ||
|
||
assert.Len(t, poolStat.PeerStats, 1) | ||
assert.Equal(t, p1.id, poolStat.PeerStats[0].PeerId) | ||
assert.Equal(t, p1.created, poolStat.PeerStats[0].Created) | ||
assert.Equal(t, p1.subConnections, poolStat.PeerStats[0].SubConnections) | ||
assert.Equal(t, p1.version, poolStat.PeerStats[0].Version) | ||
}) | ||
t.Run("outgoing and incoming peers", func(t *testing.T) { | ||
// given | ||
fx := newFixture(t) | ||
defer fx.Finish() | ||
created := time.Now() | ||
created1 := time.Now() | ||
p1 := newTestPeer("p1", created, 2, 2) | ||
require.NoError(t, fx.AddPeer(ctx, p1)) | ||
|
||
fx.Dialer.dial = func(ctx context.Context, peerId string) (peer peer.Peer, err error) { | ||
return newTestPeer(peerId, created1, 0, 0), nil | ||
} | ||
|
||
peerId := "p2" | ||
_, err := fx.Get(ctx, peerId) | ||
require.NoError(t, err) | ||
|
||
statProvider, ok := fx.Service.(*poolService) | ||
assert.True(t, ok) | ||
|
||
// when | ||
stat := statProvider.ProvideStat() | ||
|
||
// then | ||
assert.NotNil(t, stat) | ||
poolStat, ok := stat.(*poolStats) | ||
assert.True(t, ok) | ||
|
||
assert.Len(t, poolStat.PeerStats, 2) | ||
assert.Equal(t, peerId, poolStat.PeerStats[0].PeerId) | ||
assert.Equal(t, created1, poolStat.PeerStats[0].Created) | ||
assert.Equal(t, 0, poolStat.PeerStats[0].SubConnections) | ||
assert.Equal(t, uint32(0), poolStat.PeerStats[0].Version) | ||
|
||
assert.Equal(t, p1.id, poolStat.PeerStats[1].PeerId) | ||
assert.Equal(t, p1.created, poolStat.PeerStats[1].Created) | ||
assert.Equal(t, p1.subConnections, poolStat.PeerStats[1].SubConnections) | ||
assert.Equal(t, p1.version, poolStat.PeerStats[1].Version) | ||
}) | ||
t.Run("only outcoming peers", func(t *testing.T) { | ||
// given | ||
peerId := "p1" | ||
fx := newFixture(t) | ||
defer fx.Finish() | ||
created := time.Now() | ||
subConn := 3 | ||
version := uint32(2) | ||
|
||
fx.Dialer.dial = func(ctx context.Context, peerId string) (peer peer.Peer, err error) { | ||
return newTestPeer(peerId, created, subConn, version), nil | ||
} | ||
|
||
_, err := fx.Get(ctx, peerId) | ||
require.NoError(t, err) | ||
|
||
statProvider, ok := fx.Service.(*poolService) | ||
assert.True(t, ok) | ||
|
||
// when | ||
stat := statProvider.ProvideStat() | ||
|
||
// then | ||
assert.NotNil(t, stat) | ||
poolStat, ok := stat.(*poolStats) | ||
assert.True(t, ok) | ||
|
||
assert.Len(t, poolStat.PeerStats, 1) | ||
assert.Equal(t, peerId, poolStat.PeerStats[0].PeerId) | ||
assert.Equal(t, created, poolStat.PeerStats[0].Created) | ||
assert.Equal(t, subConn, poolStat.PeerStats[0].SubConnections) | ||
assert.Equal(t, version, poolStat.PeerStats[0].Version) | ||
}) | ||
t.Run("no peers", func(t *testing.T) { | ||
// given | ||
fx := newFixture(t) | ||
defer fx.Finish() | ||
|
||
statProvider, ok := fx.Service.(*poolService) | ||
assert.True(t, ok) | ||
|
||
// when | ||
stat := statProvider.ProvideStat() | ||
|
||
// then | ||
assert.NotNil(t, stat) | ||
poolStat, ok := stat.(*poolStats) | ||
assert.True(t, ok) | ||
assert.Len(t, poolStat.PeerStats, 0) | ||
}) | ||
} | ||
|
||
func newFixture(t *testing.T) *fixture { | ||
fx := &fixture{ | ||
Service: New(), | ||
|
@@ -240,16 +357,31 @@ func (d *dialerMock) Name() (name string) { | |
return "net.peerservice" | ||
} | ||
|
||
func newTestPeer(id string) *testPeer { | ||
func newTestPeer(id string, created time.Time, subConnections int, version uint32) *testPeer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make another constructor for testPeer with all that stuff, don't change other tests |
||
return &testPeer{ | ||
id: id, | ||
closed: make(chan struct{}), | ||
id: id, | ||
closed: make(chan struct{}), | ||
created: created, | ||
subConnections: subConnections, | ||
version: version, | ||
} | ||
} | ||
|
||
type testPeer struct { | ||
id string | ||
closed chan struct{} | ||
id string | ||
closed chan struct{} | ||
created time.Time | ||
subConnections int | ||
version uint32 | ||
} | ||
|
||
func (t *testPeer) ProvideStat() *peer.Stat { | ||
return &peer.Stat{ | ||
PeerId: t.id, | ||
Created: t.created, | ||
SubConnections: t.subConnections, | ||
Version: t.version, | ||
} | ||
} | ||
|
||
func (t *testPeer) SetTTL(ttl time.Duration) { | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a field called
AliveTimeSecs
which will tell how many seconds it is aliveThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just write pls p.Lock(), defer p.Unlock() and then return &Stat{...}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you propose to calculate
AliveTimeSecs
, because don't see such field in peer? 🧐