Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EngHabu committed Apr 14, 2020
1 parent 110d52f commit 76c4dc8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
6 changes: 3 additions & 3 deletions storage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ type Config struct {
// inputs is accelerated. The size of the cache is large so understand how to configure the cache.
// TODO provide some default config choices
// If this section is skipped, Caching is disabled
Cache CachingConfig `json:"cache"`
Limits LimitsConfig `json:"limits" pflag:",Sets limits for stores."`
DefaultHTTPClient *HTTPClientConfig `json:"defaultHttpClient" pflag:",Sets the default http client config."`
Cache CachingConfig `json:"cache"`
Limits LimitsConfig `json:"limits" pflag:",Sets limits for stores."`
DefaultHTTPClient HTTPClientConfig `json:"defaultHttpClient" pflag:",Sets the default http client config."`
}

// HTTPClientConfig encapsulates common settings that can be applied to an HTTP Client.
Expand Down
26 changes: 11 additions & 15 deletions storage/rawstores.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,29 @@ func applyDefaultHeaders(r *http.Request, headers map[string][]string) {
}
}

func createHTTPClient(cfg *HTTPClientConfig) *http.Client {
if cfg == nil {
return &http.Client{}
}

func createHTTPClient(cfg HTTPClientConfig) *http.Client {
c := &http.Client{
Timeout: cfg.Timeout.Duration,
}

c.Transport = &proxyTransport{
RoundTripper: http.DefaultTransport,
defaultHeaders: cfg.Headers,
if len(cfg.Headers) > 0 {
c.Transport = &proxyTransport{
RoundTripper: http.DefaultTransport,
defaultHeaders: cfg.Headers,
}
}

return c
}

// Creates a new Data Store with the supplied config.
func NewDataStore(cfg *Config, metricsScope promutils.Scope) (s *DataStore, err error) {
if cfg.DefaultHTTPClient != nil {
defaultClient := http.DefaultClient
defer func() {
http.DefaultClient = defaultClient
}()
defaultClient := http.DefaultClient
defer func() {
http.DefaultClient = defaultClient
}()

http.DefaultClient = createHTTPClient(cfg.DefaultHTTPClient)
}
http.DefaultClient = createHTTPClient(cfg.DefaultHTTPClient)

var rawStore RawStore
if fn, found := stores[cfg.Type]; found {
Expand Down
10 changes: 5 additions & 5 deletions storage/rawstores_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

func Test_createHTTPClient(t *testing.T) {
t.Run("nil", func(t *testing.T) {
client := createHTTPClient(nil)
t.Run("empty", func(t *testing.T) {
client := createHTTPClient(HTTPClientConfig{})
assert.Nil(t, client.Transport)
})

Expand All @@ -21,7 +21,7 @@ func Test_createHTTPClient(t *testing.T) {
"Header1": {"val1", "val2"},
}

client := createHTTPClient(&HTTPClientConfig{
client := createHTTPClient(HTTPClientConfig{
Headers: m,
})

Expand All @@ -32,15 +32,15 @@ func Test_createHTTPClient(t *testing.T) {
})

t.Run("Set empty timeout", func(t *testing.T) {
client := createHTTPClient(&HTTPClientConfig{
client := createHTTPClient(HTTPClientConfig{
Timeout: config.Duration{},
})

assert.Zero(t, client.Timeout)
})

t.Run("Set timeout", func(t *testing.T) {
client := createHTTPClient(&HTTPClientConfig{
client := createHTTPClient(HTTPClientConfig{
Timeout: config.Duration{Duration: 2 * time.Second},
})

Expand Down

0 comments on commit 76c4dc8

Please sign in to comment.