-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
111 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,166 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestLoadTest(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
LoadTest() | ||
}) | ||
} | ||
} | ||
|
||
func Test_createRequestJobs(t *testing.T) { | ||
func TestCreateRequestJobs(t *testing.T) { | ||
type args struct { | ||
resPool chan *http.Request | ||
url string | ||
reqPool chan *http.Request | ||
url string | ||
totalReq int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
name string | ||
args args | ||
expected int | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "Test Request Pool of 100", | ||
args: args{ | ||
reqPool: make(chan *http.Request, 100), | ||
url: "http://localhost:12345", | ||
totalReq: 100, | ||
}, | ||
expected: 100, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
createRequestJobs(tt.args.resPool, tt.args.url) | ||
createRequestJobs(tt.args.reqPool, tt.args.url, tt.args.totalReq) | ||
}) | ||
|
||
if len(tt.args.reqPool) != tt.expected { | ||
t.Fatalf("createRequestJobs did not create a request pool of %d.\n"+ | ||
"Expected: %d Actual: %d", tt.expected, tt.args.totalReq, len(tt.args.reqPool)) | ||
} | ||
} | ||
|
||
} | ||
|
||
func Test_evaluateResponses(t *testing.T) { | ||
func TestWorker(t *testing.T) { | ||
type args struct { | ||
responseChannel <-chan *Response | ||
requestChannel <-chan *http.Request | ||
responseChannel chan *Response | ||
} | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, err := fmt.Fprintln(w, "Hello, client") | ||
if err != nil { | ||
println("Could not write response from httptest server") | ||
log.Fatal(err.Error()) | ||
} | ||
})) | ||
defer server.Close() | ||
client = http.DefaultClient | ||
|
||
reqChannelWithSampleReq := make(chan *http.Request, 10) | ||
createRequestJobs(reqChannelWithSampleReq, server.URL, 10) | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "Test worker with 10 request jobs", | ||
args: args{ | ||
requestChannel: reqChannelWithSampleReq, | ||
responseChannel: make(chan *Response, 10), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
evaluateResponses(tt.args.responseChannel) | ||
worker(tt.args.requestChannel, tt.args.responseChannel) | ||
}) | ||
if len(tt.args.responseChannel) != 10 { | ||
t.Fatalf("Not enough responses received in response channel."+"\n"+ | ||
"Expected: %d, Actual: %d", 10, len(tt.args.responseChannel)) | ||
} | ||
// TODO: 10 should be a constant declared somewhere | ||
for i := 0; i < 10; i++ { | ||
res := <-tt.args.responseChannel | ||
if res.response.StatusCode != 200 || res.traceInfo.total <= 0 { | ||
t.Fatalf("Did not get expected response.") | ||
} | ||
} | ||
} | ||
} | ||
|
||
func Test_startRequestWorkers(t *testing.T) { | ||
func TestStartRequestWorkers(t *testing.T) { | ||
type args struct { | ||
requestChannel <-chan *http.Request | ||
responseChannel chan<- *Response | ||
responseChannel chan *Response | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "Test worker with 10 request jobs", | ||
args: args{ | ||
requestChannel: make(chan *http.Request, 10), | ||
responseChannel: make(chan *Response, 10), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
startRequestWorkers(tt.args.requestChannel, tt.args.responseChannel) | ||
startRequestWorkers(tt.args.requestChannel, tt.args.responseChannel, 2) | ||
}) | ||
if workers != 2 { | ||
t.Fatalf("Did not start enough workers") | ||
} | ||
} | ||
} | ||
|
||
func Test_worker(t *testing.T) { | ||
func TestEvaluateResponses(t *testing.T) { | ||
type args struct { | ||
requestChannel <-chan *http.Request | ||
responseChannel chan<- *Response | ||
resPool chan *Response | ||
} | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, err := fmt.Fprintln(w, "Hello, client") | ||
if err != nil { | ||
println("Could not write response from httptest server") | ||
log.Fatal(err.Error()) | ||
} | ||
})) | ||
defer server.Close() | ||
client = http.DefaultClient | ||
|
||
TotalReq = 10 | ||
reqChannelWithSampleReq := make(chan *http.Request, TotalReq) | ||
createRequestJobs(reqChannelWithSampleReq, server.URL, TotalReq) | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
name string | ||
args args | ||
expected int | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "Test evaluateResponses with 10 responses", | ||
args: args{ | ||
resPool: make(chan *Response, TotalReq), | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
worker(tt.args.requestChannel, tt.args.responseChannel) | ||
startRequestWorkers(reqChannelWithSampleReq, tt.args.resPool, TotalReq) | ||
evaluateResponses(tt.args.resPool) | ||
}) | ||
if results[succeeded] != fmt.Sprint(TotalReq) && results[failed] != fmt.Sprint(0) { | ||
t.Fatal("Not enough requests passed") | ||
} | ||
} | ||
|
||
} |