Skip to content

Commit

Permalink
sync2: multipeer: use black box testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan4th committed Oct 21, 2024
1 parent 9628cc1 commit 0c99172
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 142 deletions.
6 changes: 4 additions & 2 deletions sync2/multipeer/delim_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package multipeer
package multipeer_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/spacemeshos/go-spacemesh/sync2/multipeer"
)

func TestGetDelimiters(t *testing.T) {
Expand Down Expand Up @@ -89,7 +91,7 @@ func TestGetDelimiters(t *testing.T) {
},
},
} {
ds := getDelimiters(tc.numPeers, tc.keyLen, tc.maxDepth)
ds := multipeer.GetDelimiters(tc.numPeers, tc.keyLen, tc.maxDepth)
var hs []string
for _, d := range ds {
hs = append(hs, d.String())
Expand Down
25 changes: 25 additions & 0 deletions sync2/multipeer/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package multipeer

import (
"context"

"github.com/spacemeshos/go-spacemesh/p2p"
)

type (
SyncRunner = syncRunner
SplitSync = splitSync
)

var (
WithSyncRunner = withSyncRunner
WithClock = withClock
GetDelimiters = getDelimiters
NewSyncQueue = newSyncQueue
NewSplitSync = newSplitSync
NewSyncList = newSyncList
)

func (mpr *MultiPeerReconciler) FullSync(ctx context.Context, syncPeers []p2p.Peer) error {
return mpr.fullSync(ctx, syncPeers)
}
6 changes: 3 additions & 3 deletions sync2/multipeer/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/spacemeshos/go-spacemesh/sync2/rangesync"
)

//go:generate mockgen -typed -package=multipeer -destination=./mocks_test.go -source=./interface.go
//go:generate mockgen -typed -package=multipeer_test -destination=./mocks_test.go -source=./interface.go

// OrdredSet is an interface for a set that can be synced against a remote peer.
// It extends rangesync.OrderedSet with methods which are needed for multi-peer
Expand Down Expand Up @@ -83,6 +83,6 @@ type PairwiseSyncer interface {
}

type syncRunner interface {
splitSync(ctx context.Context, syncPeers []p2p.Peer) error
fullSync(ctx context.Context, syncPeers []p2p.Peer) error
SplitSync(ctx context.Context, syncPeers []p2p.Peer) error
FullSync(ctx context.Context, syncPeers []p2p.Peer) error
}
71 changes: 36 additions & 35 deletions sync2/multipeer/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions sync2/multipeer/multipeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ type runner struct {

var _ syncRunner = &runner{}

func (r *runner) splitSync(ctx context.Context, syncPeers []p2p.Peer) error {
func (r *runner) SplitSync(ctx context.Context, syncPeers []p2p.Peer) error {
s := newSplitSync(
r.mpr.logger, r.mpr.syncBase, r.mpr.peers, syncPeers,
r.mpr.splitSyncGracePeriod, r.mpr.clock, r.mpr.keyLen, r.mpr.maxDepth)
return s.sync(ctx)
return s.Sync(ctx)

Check warning on line 148 in sync2/multipeer/multipeer.go

View check run for this annotation

Codecov / codecov/patch

sync2/multipeer/multipeer.go#L144-L148

Added lines #L144 - L148 were not covered by tests
}

func (r *runner) fullSync(ctx context.Context, syncPeers []p2p.Peer) error {
func (r *runner) FullSync(ctx context.Context, syncPeers []p2p.Peer) error {
return r.mpr.fullSync(ctx, syncPeers)

Check warning on line 152 in sync2/multipeer/multipeer.go

View check run for this annotation

Codecov / codecov/patch

sync2/multipeer/multipeer.go#L151-L152

Added lines #L151 - L152 were not covered by tests
}

Expand Down Expand Up @@ -291,7 +291,7 @@ func (mpr *MultiPeerReconciler) fullSync(ctx context.Context, syncPeers []p2p.Pe
err := syncer.Sync(nil, nil)
switch {
case err == nil:
mpr.sl.noteSync()
mpr.sl.NoteSync()
case errors.Is(err, context.Canceled):
return err

Check warning on line 296 in sync2/multipeer/multipeer.go

View check run for this annotation

Codecov / codecov/patch

sync2/multipeer/multipeer.go#L295-L296

Added lines #L295 - L296 were not covered by tests
default:
Expand Down Expand Up @@ -336,7 +336,7 @@ func (mpr *MultiPeerReconciler) syncOnce(ctx context.Context, lastWasSplit bool)
full = false
if !lastWasSplit && mpr.needSplitSync(s) {
mpr.logger.Debug("doing split sync", zap.Int("peerCount", len(s.splitSyncable)))
err = mpr.runner.splitSync(ctx, s.splitSyncable)
err = mpr.runner.SplitSync(ctx, s.splitSyncable)
if err != nil {
mpr.logger.Debug("split sync failed", zap.Error(err))

Check warning on line 341 in sync2/multipeer/multipeer.go

View check run for this annotation

Codecov / codecov/patch

sync2/multipeer/multipeer.go#L341

Added line #L341 was not covered by tests
} else {
Expand All @@ -345,7 +345,7 @@ func (mpr *MultiPeerReconciler) syncOnce(ctx context.Context, lastWasSplit bool)
} else {
full = true
mpr.logger.Debug("doing full sync", zap.Int("peerCount", len(s.syncable)))
err = mpr.runner.fullSync(ctx, s.syncable)
err = mpr.runner.FullSync(ctx, s.syncable)
if err != nil {
mpr.logger.Debug("full sync failed", zap.Error(err))
} else {
Expand Down Expand Up @@ -437,5 +437,5 @@ LOOP:
// Synced returns true if the node is considered synced, that is, the specified
// number of full syncs has happened within the specified duration of time.
func (mpr *MultiPeerReconciler) Synced() bool {
return mpr.sl.synced()
return mpr.sl.Synced()
}
37 changes: 19 additions & 18 deletions sync2/multipeer/multipeer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package multipeer
package multipeer_test

import (
"context"
Expand All @@ -17,6 +17,7 @@ import (

"github.com/spacemeshos/go-spacemesh/fetch/peers"
"github.com/spacemeshos/go-spacemesh/p2p"
"github.com/spacemeshos/go-spacemesh/sync2/multipeer"
"github.com/spacemeshos/go-spacemesh/sync2/rangesync"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ type multiPeerSyncTester struct {
syncRunner *MocksyncRunner
peers *peers.Peers
clock fakeClock
reconciler *MultiPeerReconciler
reconciler *multipeer.MultiPeerReconciler
cancel context.CancelFunc
eg errgroup.Group
// EXPECT() calls should not be done concurrently
Expand All @@ -75,17 +76,17 @@ func newMultiPeerSyncTester(t *testing.T) *multiPeerSyncTester {
peers: peers.New(),
clock: clockwork.NewFakeClock().(fakeClock),
}
mt.reconciler = NewMultiPeerReconciler(mt.syncBase, mt.peers, 32, 24,
WithLogger(zaptest.NewLogger(t)),
WithSyncInterval(time.Minute),
WithSyncPeerCount(6),
WithMinSplitSyncPeers(2),
WithMinSplitSyncCount(90),
WithMaxFullDiff(20),
WithMinCompleteFraction(0.9),
WithNoPeersRecheckInterval(10*time.Second),
withSyncRunner(mt.syncRunner),
withClock(mt.clock))
mt.reconciler = multipeer.NewMultiPeerReconciler(mt.syncBase, mt.peers, 32, 24,
multipeer.WithLogger(zaptest.NewLogger(t)),
multipeer.WithSyncInterval(time.Minute),
multipeer.WithSyncPeerCount(6),
multipeer.WithMinSplitSyncPeers(2),
multipeer.WithMinSplitSyncCount(90),
multipeer.WithMaxFullDiff(20),
multipeer.WithMinCompleteFraction(0.9),
multipeer.WithNoPeersRecheckInterval(10*time.Second),
multipeer.WithSyncRunner(mt.syncRunner),
multipeer.WithClock(mt.clock))
return mt
}

Expand Down Expand Up @@ -120,13 +121,13 @@ func (mt *multiPeerSyncTester) expectProbe(times int, pr rangesync.ProbeResult)
}

func (mt *multiPeerSyncTester) expectFullSync(pl *peerList, times, numFails int) {
mt.syncRunner.EXPECT().fullSync(gomock.Any(), gomock.Any()).DoAndReturn(
mt.syncRunner.EXPECT().FullSync(gomock.Any(), gomock.Any()).DoAndReturn(
func(ctx context.Context, peers []p2p.Peer) error {
require.ElementsMatch(mt, pl.get(), peers)
// delegate to the real fullsync
return mt.reconciler.fullSync(ctx, peers)
return mt.reconciler.FullSync(ctx, peers)
})
mt.syncBase.EXPECT().Derive(gomock.Any()).DoAndReturn(func(p p2p.Peer) Syncer {
mt.syncBase.EXPECT().Derive(gomock.Any()).DoAndReturn(func(p p2p.Peer) multipeer.Syncer {
mt.mtx.Lock()
defer mt.mtx.Unlock()
require.Contains(mt, pl.get(), p)
Expand Down Expand Up @@ -173,7 +174,7 @@ func TestMultiPeerSync(t *testing.T) {
Count: 100,
Sim: 0.5, // too low for full sync
})
mt.syncRunner.EXPECT().splitSync(gomock.Any(), gomock.Any()).DoAndReturn(
mt.syncRunner.EXPECT().SplitSync(gomock.Any(), gomock.Any()).DoAndReturn(
func(_ context.Context, peers []p2p.Peer) error {
require.ElementsMatch(t, plSplit.get(), peers)
return nil
Expand Down Expand Up @@ -311,7 +312,7 @@ func TestMultiPeerSync(t *testing.T) {
mt.addPeers(10)
mt.syncBase.EXPECT().Count().Return(100, nil).AnyTimes()
mt.expectProbe(6, rangesync.ProbeResult{FP: "foo", Count: 100, Sim: 0.99})
mt.syncRunner.EXPECT().fullSync(gomock.Any(), gomock.Any()).DoAndReturn(
mt.syncRunner.EXPECT().FullSync(gomock.Any(), gomock.Any()).DoAndReturn(
func(ctx context.Context, peers []p2p.Peer) error {
mt.cancel()
return ctx.Err()
Expand Down
Loading

0 comments on commit 0c99172

Please sign in to comment.