Skip to content

Commit

Permalink
Fix concurrent test
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jun 5, 2019
1 parent f45e7eb commit 6aa52e1
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions pkg/dmsg/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestServer_Serve(t *testing.T) {

t.Run("test transport establishment concurrently", func(t *testing.T) {
initiatorsCount := 4
remotesCount := 4
remotesCount := 1

initiators := make([]*Client, 0, initiatorsCount)
remotes := make([]*Client, 0, remotesCount)
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestServer_Serve(t *testing.T) {
remotesWG.Add(len(usedRemotes))
for i, r := range remotes {
if _, ok := usedRemotes[i]; ok {
go func() {
go func(remoteInd int) {
var (
transport transport.Transport
err error
Expand All @@ -197,10 +197,10 @@ func TestServer_Serve(t *testing.T) {
acceptErrs <- err
}

remotesTps[i] = transport
remotesTps[remoteInd] = transport

remotesWG.Done()
}()
}(i)
}
}

Expand All @@ -209,33 +209,45 @@ func TestServer_Serve(t *testing.T) {
initiatorsTps := make([]transport.Transport, initiatorsCount)
initiatorsWG.Add(initiatorsCount)
for i := range initiators {
go func() {
var (
transport transport.Transport
err error
)
//go func(initiatorInd int) {
var (
transport transport.Transport
err error
)

if i == 3 {
log.Println()
}

transport, err = initiators[i].Dial(context.Background(), remotes[pickedRemotes[i]].pk)
if err != nil {
dialErrs <- err
}
transport, err = initiators[i].Dial(context.Background(),
remotes[pickedRemotes[i]].pk)
if err != nil {
dialErrs <- err
}

initiatorsTps = append(initiatorsTps, transport)
initiatorsTps = append(initiatorsTps, transport)

initiatorsWG.Done()
}()
log.Printf("Initiator %v done", i)

initiatorsWG.Done()
//}(i)
}

initiatorsWG.Wait()
close(dialErrs)
err = <-dialErrs
require.NoError(t, err)
log.Printf("%v", pickedRemotes)
log.Printf("%v", usedRemotes)

//time.Sleep(5 * time.Second)

remotesWG.Done()
remotesWG.Wait()
close(acceptErrs)
err = <-acceptErrs
require.NoError(t, err)

initiatorsWG.Wait()
close(dialErrs)
err = <-dialErrs
require.NoError(t, err)

require.Equal(t, len(usedRemotes)+initiatorsCount, len(s.conns))

/*err = <-aDone
Expand Down

0 comments on commit 6aa52e1

Please sign in to comment.