Skip to content

Commit

Permalink
all old tests refactored and passing
Browse files Browse the repository at this point in the history
  • Loading branch information
tednaleid committed Dec 27, 2023
1 parent bdc511d commit 600a167
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 247 deletions.
8 changes: 4 additions & 4 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func setupCommand(
ErrWriter: stderr,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "connect-timeout",
Usage: "number of seconds to wait for a connection to be established before timeout",
Value: conf.ConnectTimeoutSeconds,
Destination: &conf.ConnectTimeoutSeconds,
Name: "connect-timeout-ms",
Usage: "number of milliseconds to wait for a connection to be established before timeout",
Value: conf.ConnectTimeoutMillis,
Destination: &conf.ConnectTimeoutMillis,
},
&cli.StringFlag{
Name: "data-template",
Expand Down
10 changes: 10 additions & 0 deletions cli/cli_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ func TestWorkers(t *testing.T) {
assert.Equal(t, 5, separateResults.context.ResponseWorkers)
}

func TestRetries(t *testing.T) {
results, _ := ParseArgs([]string{"ganda"})
assert.NotNil(t, results)
assert.Equal(t, int64(0), results.context.Retries)

separateResults, _ := ParseArgs([]string{"ganda", "--retry", "5"})
assert.NotNil(t, results)
assert.Equal(t, int64(5), separateResults.context.Retries)
}

func TestInvalidWorkers(t *testing.T) {
testCases := []struct {
input string
Expand Down
202 changes: 0 additions & 202 deletions cli/cli_old_test.go

This file was deleted.

125 changes: 124 additions & 1 deletion cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"
)

type HttpServerStub struct {
Expand Down Expand Up @@ -112,4 +113,126 @@ func TestErrorResponse(t *testing.T) {
)
}

// TODO start here convert the rest of the tests in cli_old_test.go to use the new RunApp function
func TestTimeout(t *testing.T) {
t.Parallel()
server := NewHttpServerStub(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Millisecond)
fmt.Fprint(w, "Should not get this, should time out first")
}))
defer server.Server.Close()

runResults, _ := RunApp([]string{"ganda", "--connect-timeout-ms", "1"}, server.stubStdinUrl("bar"))

url := server.urlFor("bar")

runResults.assert(
t,
"",
url+" Error: Get \""+url+"\": context deadline exceeded (Client.Timeout exceeded while awaiting headers)\n",
)
}

func TestRetryEnabledShouldRetry5XX(t *testing.T) {
t.Parallel()
requests := 0
server := NewHttpServerStub(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests++
if requests == 1 {
w.WriteHeader(500)
} else {
fmt.Fprint(w, "Retried request")
}
}))
defer server.Server.Close()

runResults, _ := RunApp([]string{"ganda", "--retry", "1"}, server.stubStdinUrl("bar"))

url := server.urlFor("bar")

assert.Equal(t, 2, requests, "expected a failed request followed by a successful one")
runResults.assert(
t,
"Retried request\n",
"Response: 500 "+url+" (1)\nResponse: 200 "+url+"\n",
)
}

func TestRunningOutOfRetriesShouldShowError(t *testing.T) {
t.Parallel()
requests := 0
server := NewHttpServerStub(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests++
w.WriteHeader(500)
}))
defer server.Server.Close()

runResults, _ := RunApp([]string{"ganda", "--retry", "2"}, server.stubStdinUrl("bar"))

url := server.urlFor("bar")

assert.Equal(t, 3, requests, "3 total requests (original and 2 retries), all failed so expecting error")
runResults.assert(
t,
"",
"Response: 500 "+url+" (1)\nResponse: 500 "+url+" (2)\nResponse: 500 "+url+"\n",
)
}

func TestRetryEnabledShouldNotRetry4XX(t *testing.T) {
t.Parallel()
requestCount := 0
server := NewHttpServerStub(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestCount++
w.WriteHeader(400)
}))
defer server.Server.Close()

runResults, _ := RunApp([]string{"ganda", "--retry", "1"}, server.stubStdinUrl("bar"))

url := server.urlFor("bar")

assert.Equal(t, 1, requestCount, "we shouldn't retry 4xx errors")
runResults.assert(t,
"",
"Response: 400 "+url+"\n")
}

func TestRetryEnabledShouldRetryTimeout(t *testing.T) {
t.Parallel()
requestCount := 0
server := NewHttpServerStub(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestCount++
if requestCount == 1 {
// for the first request, we sleep longer than it takes to timeout
time.Sleep(50 * time.Millisecond)
}
fmt.Fprint(w, "Request ", requestCount)
}))
defer server.Server.Close()

runResults, _ := RunApp([]string{"ganda", "--connect-timeout-ms", "10", "--retry", "1"}, server.stubStdinUrl("bar"))
url := server.urlFor("bar")

//assert.Equal(t, 2, requestCount, "expected a second request")
runResults.assert(t,
"Request 2\n",
url+" (1) Error: Get \""+url+"\": context deadline exceeded (Client.Timeout exceeded while awaiting headers)\nResponse: 200 "+url+"\n")
}

func TestAddHeadersToRequestCreatesCanonicalKeys(t *testing.T) {
t.Parallel()
server := NewHttpServerStub(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// turns to uppercase versions for header key when transmitted
assert.Equal(t, r.Header["Foo"][0], "bar", "foo header")
assert.Equal(t, r.Header["X-Baz"][0], "qux", "baz header")
fmt.Fprint(w, "Hello ", r.URL.Path)
}))
defer server.Server.Close()

runResults, _ := RunApp([]string{"ganda", "-H", "foo: bar", "-H", "x-baz: qux"}, server.stubStdinUrl("bar"))
url := server.urlFor("bar")

runResults.assert(t,
"Hello /bar\n",
"Response: 200 "+url+"\n")
}
Loading

0 comments on commit 600a167

Please sign in to comment.