Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #44 from ipfs/bugs/stabilize-test-suite
Browse files Browse the repository at this point in the history
fix(tests): stabilize unreliable session tests
  • Loading branch information
hannahhoward authored Dec 19, 2018
2 parents f1fb3da + 6b3042f commit 916de59
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
19 changes: 5 additions & 14 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ func (fwm *fakeWantManager) CancelWants(ctx context.Context, cids []cid.Cid, pee
type fakePeerManager struct {
lk sync.RWMutex
peers []peer.ID
findMorePeersRequested bool
findMorePeersRequested chan struct{}
}

func (fpm *fakePeerManager) FindMorePeers(context.Context, cid.Cid) {
fpm.lk.Lock()
fpm.findMorePeersRequested = true
fpm.lk.Unlock()
fpm.findMorePeersRequested <- struct{}{}
}

func (fpm *fakePeerManager) GetOptimizedPeers() []peer.ID {
Expand Down Expand Up @@ -161,7 +159,7 @@ func TestSessionFindMorePeers(t *testing.T) {
wantReqs := make(chan wantReq, 1)
cancelReqs := make(chan wantReq, 1)
fwm := &fakeWantManager{wantReqs, cancelReqs}
fpm := &fakePeerManager{}
fpm := &fakePeerManager{findMorePeersRequested: make(chan struct{})}
id := testutil.GenerateSessionID()
session := New(ctx, id, fwm, fpm)
session.SetBaseTickDelay(200 * time.Microsecond)
Expand All @@ -187,15 +185,8 @@ func TestSessionFindMorePeers(t *testing.T) {
<-wantReqs
<-cancelReqs

// wait long enough for a tick to occur
time.Sleep(20 * time.Millisecond)

// trigger to find providers should have happened
fpm.lk.Lock()
if fpm.findMorePeersRequested != true {
t.Fatal("should have attempted to find more peers but didn't")
}
fpm.lk.Unlock()
// wait for a request to get more peers to occur
<-fpm.findMorePeersRequested

// verify a broadcast was made
receivedWantReq := <-wantReqs
Expand Down
11 changes: 10 additions & 1 deletion sessionpeermanager/sessionpeermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sessionpeermanager

import (
"context"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -39,20 +40,27 @@ func (fpn *fakePeerNetwork) FindProvidersAsync(ctx context.Context, c cid.Cid, n

type fakeConnManager struct {
taggedPeers []peer.ID
wait sync.WaitGroup
}

func (fcm *fakeConnManager) TagPeer(p peer.ID, tag string, n int) {
fcm.wait.Add(1)
fcm.taggedPeers = append(fcm.taggedPeers, p)
}

func (fcm *fakeConnManager) UntagPeer(p peer.ID, tag string) {
defer fcm.wait.Done()

for i := 0; i < len(fcm.taggedPeers); i++ {
if fcm.taggedPeers[i] == p {
fcm.taggedPeers[i] = fcm.taggedPeers[len(fcm.taggedPeers)-1]
fcm.taggedPeers = fcm.taggedPeers[:len(fcm.taggedPeers)-1]
return
}
}

}

func (*fakeConnManager) GetTagInfo(p peer.ID) *ifconnmgr.TagInfo { return nil }
func (*fakeConnManager) TrimOpenConns(ctx context.Context) {}
func (*fakeConnManager) Notifee() inet.Notifiee { return nil }
Expand Down Expand Up @@ -130,7 +138,8 @@ func TestUntaggingPeers(t *testing.T) {
t.Fatal("Peers were not tagged!")
}
<-ctx.Done()
time.Sleep(5 * time.Millisecond)
fcm.wait.Wait()

if len(fcm.taggedPeers) != 0 {
t.Fatal("Peers were not untagged!")
}
Expand Down

0 comments on commit 916de59

Please sign in to comment.