unit tests

This commit is contained in:
raf 2024-06-13 11:10:41 +03:00
commit a0955a4f6b
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
4 changed files with 95 additions and 3 deletions

15
main.go
View file

@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/signal"
@ -12,6 +13,12 @@ import (
"time"
)
type HttpClient interface {
Get(url string) (*http.Response, error)
}
var httpClient HttpClient = &http.Client{}
func main() {
urlPtr := flag.String("url", "http://example.com", "URL to fetch")
maxRequestsPtr := flag.Int("max", 100, "Maximum number of parallel requests")
@ -45,7 +52,7 @@ func main() {
return
default:
time.Sleep(delay)
makeRequest(url)
makeRequest(httpClient, url)
}
}()
}
@ -60,11 +67,13 @@ func main() {
fmt.Printf("\nOptimal Number of Parallel TCP Requests: %d\n", optimal)
}
func makeRequest(url string) {
resp, err := http.Get(url)
func makeRequest(client HttpClient, url string) {
resp, err := client.Get(url)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
_ = body
}