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

Commit 7ca6d63

Browse files
authored
Merge pull request #446 from ipfs/fix/wt-race
fix: guard access to the mock wiretap with a lock
2 parents bc3df6b + 0a5174d commit 7ca6d63

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

bitswap_test.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -868,16 +868,27 @@ type logItem struct {
868868
msg bsmsg.BitSwapMessage
869869
}
870870
type mockWireTap struct {
871+
mu sync.Mutex
871872
log []logItem
872873
}
873874

874875
func (m *mockWireTap) MessageReceived(p peer.ID, msg bsmsg.BitSwapMessage) {
876+
m.mu.Lock()
877+
defer m.mu.Unlock()
875878
m.log = append(m.log, logItem{'r', p, msg})
876879
}
877880
func (m *mockWireTap) MessageSent(p peer.ID, msg bsmsg.BitSwapMessage) {
881+
m.mu.Lock()
882+
defer m.mu.Unlock()
878883
m.log = append(m.log, logItem{'s', p, msg})
879884
}
880885

886+
func (m *mockWireTap) getLog() []logItem {
887+
m.mu.Lock()
888+
defer m.mu.Unlock()
889+
return m.log[:len(m.log):len(m.log)]
890+
}
891+
881892
func TestWireTap(t *testing.T) {
882893
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
883894
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
@@ -921,53 +932,55 @@ func TestWireTap(t *testing.T) {
921932
t.Fatal(err)
922933
}
923934

935+
log := wiretap.getLog()
936+
924937
// After communication, 3 messages should be logged via WireTap
925-
if l := len(wiretap.log); l != 3 {
938+
if l := len(log); l != 3 {
926939
t.Fatal("expected 3 items logged via WireTap, found", l)
927940
}
928941

929942
// Received: 'Have'
930-
if wiretap.log[0].dir != 'r' {
943+
if log[0].dir != 'r' {
931944
t.Error("expected message to be received")
932945
}
933-
if wiretap.log[0].pid != instances[1].Peer {
934-
t.Error("expected peer", instances[1].Peer, ", found", wiretap.log[0].pid)
946+
if log[0].pid != instances[1].Peer {
947+
t.Error("expected peer", instances[1].Peer, ", found", log[0].pid)
935948
}
936-
if l := len(wiretap.log[0].msg.Wantlist()); l != 1 {
949+
if l := len(log[0].msg.Wantlist()); l != 1 {
937950
t.Fatal("expected 1 entry in Wantlist, found", l)
938951
}
939-
if wiretap.log[0].msg.Wantlist()[0].WantType != pb.Message_Wantlist_Have {
952+
if log[0].msg.Wantlist()[0].WantType != pb.Message_Wantlist_Have {
940953
t.Error("expected WantType equal to 'Have', found 'Block'")
941954
}
942955

943956
// Sent: Block
944-
if wiretap.log[1].dir != 's' {
957+
if log[1].dir != 's' {
945958
t.Error("expected message to be sent")
946959
}
947-
if wiretap.log[1].pid != instances[1].Peer {
948-
t.Error("expected peer", instances[1].Peer, ", found", wiretap.log[1].pid)
960+
if log[1].pid != instances[1].Peer {
961+
t.Error("expected peer", instances[1].Peer, ", found", log[1].pid)
949962
}
950-
if l := len(wiretap.log[1].msg.Blocks()); l != 1 {
963+
if l := len(log[1].msg.Blocks()); l != 1 {
951964
t.Fatal("expected 1 entry in Blocks, found", l)
952965
}
953-
if wiretap.log[1].msg.Blocks()[0].Cid() != blocks[0].Cid() {
966+
if log[1].msg.Blocks()[0].Cid() != blocks[0].Cid() {
954967
t.Error("wrong block Cid")
955968
}
956969

957970
// Received: 'Cancel'
958-
if wiretap.log[2].dir != 'r' {
971+
if log[2].dir != 'r' {
959972
t.Error("expected message to be received")
960973
}
961-
if wiretap.log[2].pid != instances[1].Peer {
962-
t.Error("expected peer", instances[1].Peer, ", found", wiretap.log[2].pid)
974+
if log[2].pid != instances[1].Peer {
975+
t.Error("expected peer", instances[1].Peer, ", found", log[2].pid)
963976
}
964-
if l := len(wiretap.log[2].msg.Wantlist()); l != 1 {
977+
if l := len(log[2].msg.Wantlist()); l != 1 {
965978
t.Fatal("expected 1 entry in Wantlist, found", l)
966979
}
967-
if wiretap.log[2].msg.Wantlist()[0].WantType != pb.Message_Wantlist_Block {
980+
if log[2].msg.Wantlist()[0].WantType != pb.Message_Wantlist_Block {
968981
t.Error("expected WantType equal to 'Block', found 'Have'")
969982
}
970-
if wiretap.log[2].msg.Wantlist()[0].Cancel != true {
983+
if log[2].msg.Wantlist()[0].Cancel != true {
971984
t.Error("expected entry with Cancel set to 'true'")
972985
}
973986

@@ -991,7 +1004,9 @@ func TestWireTap(t *testing.T) {
9911004
t.Fatal(err)
9921005
}
9931006

994-
if l := len(wiretap.log); l != 3 {
1007+
log = wiretap.getLog()
1008+
1009+
if l := len(log); l != 3 {
9951010
t.Fatal("expected 3 items logged via WireTap, found", l)
9961011
}
9971012

0 commit comments

Comments
 (0)