Skip to content
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

fix(dot/network): fix data race and add t.Parallel to all tests #2105

Closed
wants to merge 47 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
d05e39e
fix: remove race conditions on dot/network
EclesioMeloJunior Dec 6, 2021
0611f92
chore: use available ports queue and update all test to call t.Parall…
EclesioMeloJunior Dec 6, 2021
1e03e39
chore: resolve data races on dot/network
EclesioMeloJunior Dec 7, 2021
1009d6c
chore: add locks to global logger New
EclesioMeloJunior Dec 7, 2021
dd4f9c7
chore: resolve data race with stop channel to peerset
EclesioMeloJunior Dec 7, 2021
f6d51f0
chore: data race at peerstate nodes
EclesioMeloJunior Dec 7, 2021
589c4a9
chore: data race on cfg.SlotDuration
EclesioMeloJunior Dec 7, 2021
04b417a
chore: remove unused comment
EclesioMeloJunior Dec 7, 2021
9fa0bf0
chore: remove test log
EclesioMeloJunior Dec 9, 2021
034c4db
Merge branch 'eclesio/net-race-conditions' of github.com:ChainSafe/go…
EclesioMeloJunior Dec 9, 2021
b4ee352
Merge branch 'development' into eclesio/net-race-conditions
EclesioMeloJunior Dec 9, 2021
9bbc83b
Merge branch 'development' into eclesio/net-race-conditions
EclesioMeloJunior Jan 5, 2022
894e218
Merge branch 'development' into eclesio/net-race-conditions
EclesioMeloJunior Jan 6, 2022
8dc1d05
Merge branch 'development' into eclesio/net-race-conditions
EclesioMeloJunior Jan 6, 2022
ae518e8
Merge branch 'development' into eclesio/net-race-conditions
EclesioMeloJunior Jan 14, 2022
56b20e0
chore: remove unused `stopCh`
EclesioMeloJunior Jan 14, 2022
5879434
chore: resolve stream handler race condition
EclesioMeloJunior Jan 14, 2022
03f77aa
chore: resolve race at `TestExistingStream`
EclesioMeloJunior Jan 17, 2022
bbaa958
chore: resolve race at `(*PeersState).getNode()`
EclesioMeloJunior Jan 17, 2022
0518139
chore: fix data race at `TestStreamCloseEOF`
EclesioMeloJunior Jan 17, 2022
ec8dece
chore: add `MessageCacheTTL` at node config
EclesioMeloJunior Jan 17, 2022
59cc124
chore: split tests to avoid data race
EclesioMeloJunior Jan 17, 2022
a43421c
chore: introduce mutexes at peerset handler to avoide close a channel…
EclesioMeloJunior Jan 17, 2022
e7cf32f
feat: remove race conditions from `dot/network` package
EclesioMeloJunior Jan 17, 2022
7e8cc89
chore: remove `./test_data` from network pkg tests
EclesioMeloJunior Jan 17, 2022
1a2a1b3
Merge branch 'development' into eclesio/net-race-conditions
EclesioMeloJunior Jan 17, 2022
a459fe4
chore: fix typo
EclesioMeloJunior Jan 26, 2022
982d910
Merge branch 'development' into eclesio/net-race-conditions
EclesioMeloJunior Jan 26, 2022
19e503d
Merge branch 'eclesio/net-race-conditions' of github.com:ChainSafe/go…
EclesioMeloJunior Jan 26, 2022
15b4433
use timer :D
EclesioMeloJunior Jan 26, 2022
fade1ed
chore: test `ok` instead nil messages
EclesioMeloJunior Jan 26, 2022
5869657
chore: use a more meaningful name
EclesioMeloJunior Jan 26, 2022
ea4509d
chore: fix typo
EclesioMeloJunior Jan 26, 2022
74df15f
chore: improving code
EclesioMeloJunior Jan 26, 2022
3a8a47c
chore: remove availablePortQueue
EclesioMeloJunior Jan 27, 2022
37046e1
chore: fix problems with port number 0
EclesioMeloJunior Jan 28, 2022
1fc3b73
chore: use map to avoid inner loops
EclesioMeloJunior Jan 28, 2022
3bf902a
Merge branch 'development' into eclesio/net-race-conditions
EclesioMeloJunior Jan 28, 2022
290fd1b
chore: remove unneeded code
EclesioMeloJunior Jan 28, 2022
fa16436
chore: use availablePort(t) for tests
EclesioMeloJunior Jan 28, 2022
6ca2cfd
chore: replace available port by 0 at rpc integration tests
EclesioMeloJunior Jan 28, 2022
dd9a674
chore: using rwmutex in addPeerReputation and getNode
EclesioMeloJunior Jan 28, 2022
c84d611
chore: address comments
EclesioMeloJunior Jan 29, 2022
c46a26d
Merge branch 'development' into eclesio/net-race-conditions
EclesioMeloJunior Feb 2, 2022
c8f7deb
Merge branch 'development' into eclesio/net-race-conditions
EclesioMeloJunior May 30, 2022
5e70577
chore: fix lint
EclesioMeloJunior May 30, 2022
f1b9a3f
chore: just fall through the `select` case
EclesioMeloJunior May 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions dot/network/block_announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
)

func TestEncodeBlockAnnounce(t *testing.T) {
t.Parallel()

expected := common.MustHexToBytes("0x01000000000000000000000000000000000000000000000000000000000000003501020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000c0642414245340201000000ef55a50f00000000044241424549040118ca239392960473fe1bc65f94ee27d890a49c1b200c006ff5dcc525330ecc16770100000000000000b46f01874ce7abbb5220e8fd89bede0adad14c73039d91e28e881823433e723f0100000000000000d684d9176d6eb69887540c9a89fa6097adea82fc4b0ff26d1062b488f352e179010000000000000068195a71bdde49117a616424bdc60a1733e96acb1da5aeab5d268cf2a572e94101000000000000001a0575ef4ae24bdfd31f4cb5bd61239ae67c12d4e64ae51ac756044aa6ad8200010000000000000018168f2aad0081a25728961ee00627cfe35e39833c805016632bf7c14da5800901000000000000000000000000000000000000000000000000000000000000000000000000000000054241424501014625284883e564bc1e4063f5ea2b49846cdddaa3761d04f543b698c1c3ee935c40d25b869247c36c6b8a8cbbd7bb2768f560ab7c276df3c62df357a7e3b1ec8d00") //nolint:lll

digestVdt := types.NewDigest()
Expand Down Expand Up @@ -52,6 +54,8 @@ func TestEncodeBlockAnnounce(t *testing.T) {
}

func TestDecodeBlockAnnounce(t *testing.T) {
t.Parallel()

enc := common.MustHexToBytes("0x01000000000000000000000000000000000000000000000000000000000000003501020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000c0642414245340201000000ef55a50f00000000044241424549040118ca239392960473fe1bc65f94ee27d890a49c1b200c006ff5dcc525330ecc16770100000000000000b46f01874ce7abbb5220e8fd89bede0adad14c73039d91e28e881823433e723f0100000000000000d684d9176d6eb69887540c9a89fa6097adea82fc4b0ff26d1062b488f352e179010000000000000068195a71bdde49117a616424bdc60a1733e96acb1da5aeab5d268cf2a572e94101000000000000001a0575ef4ae24bdfd31f4cb5bd61239ae67c12d4e64ae51ac756044aa6ad8200010000000000000018168f2aad0081a25728961ee00627cfe35e39833c805016632bf7c14da5800901000000000000000000000000000000000000000000000000000000000000000000000000000000054241424501014625284883e564bc1e4063f5ea2b49846cdddaa3761d04f543b698c1c3ee935c40d25b869247c36c6b8a8cbbd7bb2768f560ab7c276df3c62df357a7e3b1ec8d00") //nolint:lll

digestVdt := types.NewDigest()
Expand Down Expand Up @@ -90,6 +94,8 @@ func TestDecodeBlockAnnounce(t *testing.T) {
}

func TestEncodeBlockAnnounceHandshake(t *testing.T) {
t.Parallel()

expected := common.MustHexToBytes("0x044d00000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000") //nolint:lll
testHandshake := BlockAnnounceHandshake{
Roles: 4,
Expand All @@ -104,6 +110,8 @@ func TestEncodeBlockAnnounceHandshake(t *testing.T) {
}

func TestDecodeBlockAnnounceHandshake(t *testing.T) {
t.Parallel()

enc := common.MustHexToBytes("0x044d00000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000") //nolint:lll
expected := BlockAnnounceHandshake{
Roles: 4,
Expand All @@ -119,11 +127,12 @@ func TestDecodeBlockAnnounceHandshake(t *testing.T) {
}

func TestHandleBlockAnnounceMessage(t *testing.T) {
t.Parallel()
basePath := utils.NewTestBasePath(t, "nodeA")

config := &Config{
BasePath: basePath,
Port: 7001,
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -142,9 +151,11 @@ func TestHandleBlockAnnounceMessage(t *testing.T) {
}

func TestValidateBlockAnnounceHandshake(t *testing.T) {
t.Parallel()

configA := &Config{
BasePath: utils.NewTestBasePath(t, "nodeA"),
Port: 7001,
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
Expand Down
36 changes: 11 additions & 25 deletions dot/network/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package network

import (
"io"
"reflect"
"testing"

"github.com/ChainSafe/gossamer/dot/state"
Expand All @@ -17,62 +16,51 @@ import (

// test buildIdentity method
func TestBuildIdentity(t *testing.T) {
testDir := utils.NewTestDir(t)
defer utils.RemoveTestDir(t)
t.Parallel()

testDir := t.TempDir()

configA := &Config{
logger: log.New(log.SetWriter(io.Discard)),
BasePath: testDir,
}

err := configA.buildIdentity()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

configB := &Config{
logger: log.New(log.SetWriter(io.Discard)),
BasePath: testDir,
}

err = configB.buildIdentity()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if !reflect.DeepEqual(configA.privateKey, configB.privateKey) {
t.Error("Private keys should match")
}
require.Equal(t, configA.privateKey, configB.privateKey)

configC := &Config{
logger: log.New(log.SetWriter(io.Discard)),
RandSeed: 1,
}

err = configC.buildIdentity()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

configD := &Config{
logger: log.New(log.SetWriter(io.Discard)),
RandSeed: 2,
}

err = configD.buildIdentity()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if reflect.DeepEqual(configC.privateKey, configD.privateKey) {
t.Error("Private keys should not match")
}
require.NotEqual(t, configC.privateKey, configD.privateKey)
}

// test build configuration method
func TestBuild(t *testing.T) {
t.Parallel()
testBasePath := utils.NewTestBasePath(t, "node")
defer utils.RemoveTestDir(t)

testBlockState := &state.BlockState{}
testRandSeed := int64(1)
Expand All @@ -85,9 +73,7 @@ func TestBuild(t *testing.T) {
}

err := cfg.build()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

require.Equal(t, testBlockState, cfg.BlockState)
require.Equal(t, testBasePath, cfg.BasePath)
Expand Down
34 changes: 24 additions & 10 deletions dot/network/connmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import (
)

func TestMinPeers(t *testing.T) {
t.Parallel()

const min = 1

nodes := make([]*Service, 2)
for i := range nodes {
config := &Config{
BasePath: utils.NewTestBasePath(t, fmt.Sprintf("node%d", i)),
Port: 7000 + uint16(i),
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -36,7 +38,7 @@ func TestMinPeers(t *testing.T) {

configB := &Config{
BasePath: utils.NewTestBasePath(t, "nodeB"),
Port: 7002,
Port: uint16(availablePorts.get()),
Bootnodes: []string{addrs.String(), addrs1.String()},
NoMDNS: true,
MinPeers: min,
Expand All @@ -55,12 +57,15 @@ func TestMinPeers(t *testing.T) {
}

func TestMaxPeers(t *testing.T) {
t.Parallel()

const max = 3
nodes := make([]*Service, max+2)

for i := range nodes {
config := &Config{
BasePath: utils.NewTestBasePath(t, fmt.Sprintf("node%d", i)),
Port: 7000 + uint16(i),
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
MaxPeers: max,
Expand Down Expand Up @@ -89,6 +94,8 @@ func TestMaxPeers(t *testing.T) {
}

func TestProtectUnprotectPeer(t *testing.T) {
t.Parallel()

const (
min = 1
max = 4
Expand Down Expand Up @@ -125,24 +132,27 @@ func TestPersistentPeers(t *testing.T) {
t.Skip() // this sometimes fails on CI
}

t.Parallel()

configA := &Config{
BasePath: utils.NewTestBasePath(t, "node-a"),
Port: 7000,
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
nodeA := createTestService(t, configA)

addrs := nodeA.host.multiaddrs()

configB := &Config{
BasePath: utils.NewTestBasePath(t, "node-b"),
Port: 7001,
Port: uint16(availablePorts.get()),
NoMDNS: true,
PersistentPeers: []string{addrs[0].String()},
}
nodeB := createTestService(t, configB)

time.Sleep(time.Millisecond * 600)

// B should have connected to A during bootstrap
conns := nodeB.host.h.Network().ConnsToPeer(nodeA.host.id())
require.NotEqual(t, 0, len(conns))
Expand All @@ -161,10 +171,12 @@ func TestRemovePeer(t *testing.T) {
t.Skip() // this sometimes fails on CI
}

t.Parallel()

basePathA := utils.NewTestBasePath(t, "nodeA")
configA := &Config{
BasePath: basePathA,
Port: 7001,
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -177,7 +189,7 @@ func TestRemovePeer(t *testing.T) {
basePathB := utils.NewTestBasePath(t, "nodeB")
configB := &Config{
BasePath: basePathB,
Port: 7002,
Port: uint16(availablePorts.get()),
Bootnodes: []string{addrA.String()},
NoMDNS: true,
}
Expand All @@ -200,11 +212,13 @@ func TestSetReservedPeer(t *testing.T) {
t.Skip() // this sometimes fails on CI
}

t.Parallel()

nodes := make([]*Service, 3)
for i := range nodes {
config := &Config{
BasePath: utils.NewTestBasePath(t, fmt.Sprintf("node%d", i)),
Port: 7000 + uint16(i),
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -219,7 +233,7 @@ func TestSetReservedPeer(t *testing.T) {
basePathD := utils.NewTestBasePath(t, "node3")
config := &Config{
BasePath: basePathD,
Port: 7004,
Port: uint16(availablePorts.get()),
NoMDNS: true,
PersistentPeers: []string{addrA.String(), addrB.String()},
}
Expand Down
24 changes: 15 additions & 9 deletions dot/network/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (

func newTestDiscovery(t *testing.T, num int) []*discovery {
t.Helper()

var discs []*discovery
for i := 0; i < num; i++ {
config := &Config{
BasePath: utils.NewTestBasePath(t, fmt.Sprintf("node%d", i)),
Port: uint16(7001 + i),
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
Expand Down Expand Up @@ -52,9 +53,7 @@ func connectNoSync(ctx context.Context, t *testing.T, a, b *discovery) {

idB := b.h.ID()
addrB := b.h.Peerstore().Addrs(idB)
if len(addrB) == 0 {
t.Fatal("peers setup incorrectly: no local address")
}
require.NotEqual(t, 0, len(addrB), "peers setup incorrectly: no local address")

a.h.Peerstore().AddAddrs(idB, addrB, time.Minute)
pi := peer.AddrInfo{ID: idB}
Expand All @@ -65,6 +64,7 @@ func connectNoSync(ctx context.Context, t *testing.T, a, b *discovery) {
time.Sleep(TestBackoffTimeout)
err = a.h.Connect(ctx, pi)
}

require.NoError(t, err)
}

Expand All @@ -74,6 +74,8 @@ func TestKadDHT(t *testing.T) {
return
}

t.Parallel()

// setup 3 nodes
nodes := newTestDiscovery(t, 3)

Expand All @@ -100,9 +102,11 @@ func TestKadDHT(t *testing.T) {
}

func TestBeginDiscovery(t *testing.T) {
t.Parallel()

configA := &Config{
BasePath: utils.NewTestBasePath(t, "nodeA"),
Port: 7001,
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -112,7 +116,7 @@ func TestBeginDiscovery(t *testing.T) {

configB := &Config{
BasePath: utils.NewTestBasePath(t, "nodeB"),
Port: 7002,
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -136,9 +140,11 @@ func TestBeginDiscovery(t *testing.T) {
}

func TestBeginDiscovery_ThreeNodes(t *testing.T) {
t.Parallel()

configA := &Config{
BasePath: utils.NewTestBasePath(t, "nodeA"),
Port: 7001,
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -148,7 +154,7 @@ func TestBeginDiscovery_ThreeNodes(t *testing.T) {

configB := &Config{
BasePath: utils.NewTestBasePath(t, "nodeB"),
Port: 7002,
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -158,7 +164,7 @@ func TestBeginDiscovery_ThreeNodes(t *testing.T) {

configC := &Config{
BasePath: utils.NewTestBasePath(t, "nodeC"),
Port: 7003,
Port: uint16(availablePorts.get()),
NoBootstrap: true,
NoMDNS: true,
}
Expand Down
Loading