Skip to content

Commit

Permalink
Wait for server connection to be established before checking
Browse files Browse the repository at this point in the history
connections.
  • Loading branch information
tbarker25 committed Aug 23, 2021
1 parent bc46619 commit 9248140
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
35 changes: 30 additions & 5 deletions process/process_test.go
Expand Up @@ -516,13 +516,15 @@ func Test_Connections(t *testing.T) {
t.Fatalf("unable to parse tcpServerAddr port: %v", err)
}

serverEstablished := make(chan struct{})
go func() { // TCP listening goroutine
conn, err := l.Accept()
if err != nil {
panic(err)
}
defer conn.Close()

serverEstablished <- struct{}{}
_, err = ioutil.ReadAll(conn)
if err != nil {
panic(err)
Expand All @@ -535,6 +537,10 @@ func Test_Connections(t *testing.T) {
}
defer conn.Close()

// Rarely the call to net.Dial returns before the server connection is
// established. Wait so that the test doesn't fail.
<-serverEstablished

c, err := p.Connections()
skipIfNotImplementedErr(t, err)
if err != nil {
Expand All @@ -543,14 +549,33 @@ func Test_Connections(t *testing.T) {
if len(c) == 0 {
t.Fatal("no connections found")
}
found := 0

serverConnections := 0
for _, connection := range c {
if connection.Status == "ESTABLISHED" && (connection.Laddr.IP == tcpServerAddrIP && connection.Laddr.Port == uint32(tcpServerAddrPort)) || (connection.Raddr.IP == tcpServerAddrIP && connection.Raddr.Port == uint32(tcpServerAddrPort)) {
found++
if connection.Laddr.IP == tcpServerAddrIP && connection.Laddr.Port == uint32(tcpServerAddrPort) && connection.Raddr.Port != 0 {
if connection.Status != "ESTABLISHED" {
t.Fatalf("expected server connection to be ESTABLISHED, have %+v", connection)
}
serverConnections++
}
}
if found != 2 { // two established connections, one for the server, the other for the client
t.Fatalf("wrong connections: %+v", c)

clientConnections := 0
for _, connection := range c {
if connection.Raddr.IP == tcpServerAddrIP && connection.Raddr.Port == uint32(tcpServerAddrPort) {
if connection.Status != "ESTABLISHED" {
t.Fatalf("expected client connection to be ESTABLISHED, have %+v", connection)
}
clientConnections++
}
}

if serverConnections != 1 { // two established connections, one for the server, the other for the client
t.Fatalf("expected 1 server connection, have %d.\nDetails: %+v", serverConnections, c)
}

if clientConnections != 1 { // two established connections, one for the server, the other for the client
t.Fatalf("expected 1 server connection, have %d.\nDetails: %+v", clientConnections, c)
}
}

Expand Down
35 changes: 30 additions & 5 deletions v3/process/process_test.go
Expand Up @@ -518,13 +518,15 @@ func Test_Connections(t *testing.T) {
t.Fatalf("unable to parse tcpServerAddr port: %v", err)
}

serverEstablished := make(chan struct{})
go func() { // TCP listening goroutine
conn, err := l.Accept()
if err != nil {
panic(err)
}
defer conn.Close()

serverEstablished <- struct{}{}
_, err = ioutil.ReadAll(conn)
if err != nil {
panic(err)
Expand All @@ -537,6 +539,10 @@ func Test_Connections(t *testing.T) {
}
defer conn.Close()

// Rarely the call to net.Dial returns before the server connection is
// established. Wait so that the test doesn't fail.
<-serverEstablished

c, err := p.Connections()
skipIfNotImplementedErr(t, err)
if err != nil {
Expand All @@ -545,14 +551,33 @@ func Test_Connections(t *testing.T) {
if len(c) == 0 {
t.Fatal("no connections found")
}
found := 0

serverConnections := 0
for _, connection := range c {
if connection.Status == "ESTABLISHED" && (connection.Laddr.IP == tcpServerAddrIP && connection.Laddr.Port == uint32(tcpServerAddrPort)) || (connection.Raddr.IP == tcpServerAddrIP && connection.Raddr.Port == uint32(tcpServerAddrPort)) {
found++
if connection.Laddr.IP == tcpServerAddrIP && connection.Laddr.Port == uint32(tcpServerAddrPort) && connection.Raddr.Port != 0 {
if connection.Status != "ESTABLISHED" {
t.Fatalf("expected server connection to be ESTABLISHED, have %+v", connection)
}
serverConnections++
}
}
if found != 2 { // two established connections, one for the server, the other for the client
t.Fatalf("wrong connections: %+v", c)

clientConnections := 0
for _, connection := range c {
if connection.Raddr.IP == tcpServerAddrIP && connection.Raddr.Port == uint32(tcpServerAddrPort) {
if connection.Status != "ESTABLISHED" {
t.Fatalf("expected client connection to be ESTABLISHED, have %+v", connection)
}
clientConnections++
}
}

if serverConnections != 1 { // two established connections, one for the server, the other for the client
t.Fatalf("expected 1 server connection, have %d.\nDetails: %+v", serverConnections, c)
}

if clientConnections != 1 { // two established connections, one for the server, the other for the client
t.Fatalf("expected 1 server connection, have %d.\nDetails: %+v", clientConnections, c)
}
}

Expand Down

0 comments on commit 9248140

Please sign in to comment.