Skip to content

Commit

Permalink
return on error and assert closed network
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Abboud committed Jun 18, 2021
1 parent f614239 commit c0e523f
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions conjure-go-client/httpclient/http2_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// +build go1.16

package httpclient_test

import (
Expand All @@ -21,6 +23,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync/atomic"
"testing"
"time"
Expand All @@ -30,16 +33,17 @@ import (
"github.com/stretchr/testify/require"
)

// TestHTTP2ClientReusesBrokenConnection asserts the behavior of an HTTP/2 client that re-uses
// TestHTTP2Client_reusesBrokenConnection asserts the behavior of an HTTP/2 client that re-uses
// a broken connection and fails to make requests. The clients ReadIdleTimeout is set to 0, which means
// there will be no HTTP/2 health checks enabled and thus the client will re-use existing (broken) connections.
func TestHTTP2ClientReusesBrokenConnection(t *testing.T) {
func TestHTTP2Client_reusesBrokenConnection(t *testing.T) {
// only 1 expected dial since the second request will return an error for
testProxy(t, 0, 0, 1, true)
}

// TestHTTP2ClientReconnectsOnBrokenConnection asserts the behavior of an HTTP/2 client that has
// TestHTTP2Client_reconnectsOnBrokenConnection asserts the behavior of an HTTP/2 client that has
// the ReadIdleTimeout configured very low which forces the client to re-connect on subsequent requests.
func TestHTTP2ClientReconnectsOnBrokenConnection(t *testing.T) {
func TestHTTP2Client_reconnectsOnBrokenConnection(t *testing.T) {
testProxy(t, time.Second, time.Second, 2, false)
}

Expand Down Expand Up @@ -128,6 +132,18 @@ func newProxyServer(t *testing.T, proxyURL string) *proxyServer {
}
}

func (p *proxyServer) serve(t *testing.T, stopCh chan struct{}, expectErr bool) {
conn, err := p.ln.Accept()
if expectErr {
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), "use of closed network connection"))
return
}
require.NoError(t, err)
atomic.AddInt32(&p.dialCount, 1)
go p.handleConnection(t, conn, stopCh)
}

func (p *proxyServer) handleConnection(t *testing.T, in net.Conn, stopCh chan struct{}) {
out, err := net.Dial("tcp", p.proxyURL)
require.NoError(t, err)
Expand All @@ -141,17 +157,6 @@ func (p *proxyServer) handleConnection(t *testing.T, in net.Conn, stopCh chan st
require.NoError(t, out.Close())
}

func (p *proxyServer) serve(t *testing.T, stopCh chan struct{}, expectErr bool) {
conn, err := p.ln.Accept()
if expectErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
atomic.AddInt32(&p.dialCount, 1)
go p.handleConnection(t, conn, stopCh)
}

func (p *proxyServer) DialCount() int {
return int(atomic.LoadInt32(&p.dialCount))
}

0 comments on commit c0e523f

Please sign in to comment.