Skip to content

Commit

Permalink
Add Speedtest struct which contains the global variables used
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-linch committed Nov 15, 2021
1 parent a83af84 commit a1f1d5b
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 10 deletions.
2 changes: 1 addition & 1 deletion speedtest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (s *Server) PingTestContext(ctx context.Context) error {
return err
}

resp, err := http.DefaultClient.Do(req)
resp, err := s.doer.Do(req)
if err != nil {
return err
}
Expand Down
25 changes: 21 additions & 4 deletions speedtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Server struct {
Latency time.Duration `json:"latency"`
DLSpeed float64 `json:"dl_speed"`
ULSpeed float64 `json:"ul_speed"`

doer *http.Client
}

// ServerList list of Server
Expand Down Expand Up @@ -61,18 +63,23 @@ func (b ByDistance) Less(i, j int) bool {
}

// FetchServerList retrieves a list of available servers
func FetchServerList(user *User) (ServerList, error) {
func (client *Speedtest) FetchServerList(user *User) (ServerList, error) {
return FetchServerListContext(context.Background(), user)
}

// FetchServerList retrieves a list of available servers
func FetchServerList(user *User) (ServerList, error) {
return defaultClient.FetchServerList(user)
}

// FetchServerListContext retrieves a list of available servers, observing the given context.
func FetchServerListContext(ctx context.Context, user *User) (ServerList, error) {
func (client *Speedtest) FetchServerListContext(ctx context.Context, user *User) (ServerList, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, speedTestServersUrl, nil)
if err != nil {
return ServerList{}, err
}

resp, err := http.DefaultClient.Do(req)
resp, err := client.doer.Do(req)
if err != nil {
return ServerList{}, err
}
Expand All @@ -85,7 +92,7 @@ func FetchServerListContext(ctx context.Context, user *User) (ServerList, error)
return ServerList{}, err
}

resp, err = http.DefaultClient.Do(req)
resp, err = client.doer.Do(req)
if err != nil {
return ServerList{}, err
}
Expand All @@ -101,6 +108,11 @@ func FetchServerListContext(ctx context.Context, user *User) (ServerList, error)
return list, err
}

// set doer of server
for _, s := range list.Servers {
s.doer = client.doer
}

// Calculate distance
for i := range list.Servers {
server := list.Servers[i]
Expand All @@ -121,6 +133,11 @@ func FetchServerListContext(ctx context.Context, user *User) (ServerList, error)
return list, nil
}

// FetchServerListContext retrieves a list of available servers, observing the given context.
func FetchServerListContext(ctx context.Context, user *User) (ServerList, error) {
return defaultClient.FetchServerListContext(ctx, user)
}

func distance(lat1 float64, lon1 float64, lat2 float64, lon2 float64) float64 {
radius := 6378.137

Expand Down
5 changes: 4 additions & 1 deletion speedtest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ func TestFetchServerList(t *testing.T) {
Lon: "138.44",
Isp: "Hello",
}
serverList, err := FetchServerList(&user)

client := New()

serverList, err := client.FetchServerList(&user)
if err != nil {
t.Errorf(err.Error())
}
Expand Down
33 changes: 33 additions & 0 deletions speedtest/speedtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package speedtest

import "net/http"

// Speedtest is a speedtest client.
type Speedtest struct {
doer *http.Client
}

// Option is a function that can be passed to New to modify the Client.
type Option func(*Speedtest)

// WithDoer sets the http.Client used to make requests.
func WithDoer(doer *http.Client) Option {
return func(s *Speedtest) {
s.doer = doer
}
}

// New creates a new speedtest client.
func New(opts ...Option) *Speedtest {
s := &Speedtest{
doer: http.DefaultClient,
}

for _, opt := range opts {
opt(s)
}

return s
}

var defaultClient = New()
26 changes: 26 additions & 0 deletions speedtest/speedtest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package speedtest

import (
"net/http"
"testing"
)

func TestNew(t *testing.T) {
t.Run("DefaultDoer", func(t *testing.T) {
c := New()

if c.doer == nil {
t.Error("doer is nil by")
}
})

t.Run("CustomDoer", func(t *testing.T) {
doer := &http.Client{}

c := New(WithDoer(doer))
if c.doer != doer {
t.Error("doer is not the same")
}
})

}
16 changes: 13 additions & 3 deletions speedtest/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@ type Users struct {
}

// FetchUserInfo returns information about caller determined by speedtest.net
func FetchUserInfo() (*User, error) {
func (client *Speedtest) FetchUserInfo() (*User, error) {
return FetchUserInfoContext(context.Background())
}

// FetchUserInfo returns information about caller determined by speedtest.net
func FetchUserInfo() (*User, error) {
return defaultClient.FetchUserInfo()
}

// FetchUserInfoContext returns information about caller determined by speedtest.net, observing the given context.
func FetchUserInfoContext(ctx context.Context) (*User, error) {
func (client *Speedtest) FetchUserInfoContext(ctx context.Context) (*User, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, speedTestConfigUrl, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
resp, err := client.doer.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -57,6 +62,11 @@ func FetchUserInfoContext(ctx context.Context) (*User, error) {
return &users.Users[0], nil
}

// FetchUserInfoContext returns information about caller determined by speedtest.net, observing the given context.
func FetchUserInfoContext(ctx context.Context) (*User, error) {
return defaultClient.FetchUserInfoContext(ctx)
}

// String representation of User
func (u *User) String() string {
return fmt.Sprintf("%s, (%s) [%s, %s]", u.IP, u.Isp, u.Lat, u.Lon)
Expand Down
4 changes: 3 additions & 1 deletion speedtest/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
)

func TestFetchUserInfo(t *testing.T) {
user, err := FetchUserInfo()
client := New()

user, err := client.FetchUserInfo()
if err != nil {
t.Errorf(err.Error())
}
Expand Down

0 comments on commit a1f1d5b

Please sign in to comment.