Browse Source

Improve test for sdk/config

Jackson Tian 7 years ago
parent
commit
645fae3d04
2 changed files with 66 additions and 8 deletions
  1. 14 8
      sdk/config.go
  2. 52 0
      sdk/config_test.go

+ 14 - 8
sdk/config.go

@@ -15,9 +15,10 @@
 package sdk
 
 import (
-	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
 	"net/http"
 	"time"
+
+	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
 )
 
 type Config struct {
@@ -39,11 +40,6 @@ func NewConfig() (config *Config) {
 	return
 }
 
-func (c *Config) WithTimeout(timeout time.Duration) *Config {
-	c.Timeout = timeout
-	return c
-}
-
 func (c *Config) WithAutoRetry(isAutoRetry bool) *Config {
 	c.AutoRetry = isAutoRetry
 	return c
@@ -59,6 +55,16 @@ func (c *Config) WithUserAgent(userAgent string) *Config {
 	return c
 }
 
+func (c *Config) WithDebug(isDebug bool) *Config {
+	c.Debug = isDebug
+	return c
+}
+
+func (c *Config) WithTimeout(timeout time.Duration) *Config {
+	c.Timeout = timeout
+	return c
+}
+
 func (c *Config) WithHttpTransport(httpTransport *http.Transport) *Config {
 	c.HttpTransport = httpTransport
 	return c
@@ -79,7 +85,7 @@ func (c *Config) WithGoRoutinePoolSize(goRoutinePoolSize int) *Config {
 	return c
 }
 
-func (c *Config) WithDebug(isDebug bool) *Config {
-	c.Debug = isDebug
+func (c *Config) WithScheme(scheme string) *Config {
+	c.Scheme = scheme
 	return c
 }

+ 52 - 0
sdk/config_test.go

@@ -0,0 +1,52 @@
+package sdk
+
+import (
+  "net/http"
+  "testing"
+  "time"
+
+  "github.com/stretchr/testify/assert"
+)
+
+func Test_Config(t *testing.T) {
+  config := NewConfig()
+  assert.NotNil(t, config, "NewConfig failed")
+  assert.Equal(t, true, config.AutoRetry, "Default AutoRetry should be true")
+  assert.Equal(t, 3, config.MaxRetryTime, "Default MaxRetryTime should be 3")
+  assert.Equal(t, "", config.UserAgent, "Default UserAgent should be empty")
+  assert.Equal(t, false, config.Debug, "Default AutoRetry should be false")
+  assert.Equal(t, time.Duration(10000000000), config.Timeout, "Default Timeout should be 10000000000")
+  assert.Equal(t, (*http.Transport)(nil), config.HttpTransport, "Default HttpTransport should be nil")
+  assert.Equal(t, false, config.EnableAsync, "Default EnableAsync should be false")
+  assert.Equal(t, 1000, config.MaxTaskQueueSize, "Default MaxTaskQueueSize should be 1000")
+  assert.Equal(t, 5, config.GoRoutinePoolSize, "Default GoRoutinePoolSize should be 5")
+  assert.Equal(t, "HTTP", config.Scheme, "Default Scheme should be HTTP")
+
+  transport := &http.Transport{
+    MaxIdleConns:       10,
+    IdleConnTimeout:    30 * time.Second,
+    DisableCompression: true,
+  }
+  config.
+    WithAutoRetry(false).
+    WithMaxRetryTime(0).
+    WithUserAgent("new user agent").
+    WithDebug(true).
+    WithTimeout(time.Duration(500000)).
+    WithHttpTransport(transport).
+    WithEnableAsync(true).
+    WithMaxTaskQueueSize(1).
+    WithGoRoutinePoolSize(10).
+    WithScheme("HTTPS")
+
+  assert.Equal(t, 0, config.MaxRetryTime)
+  assert.Equal(t, false, config.AutoRetry)
+  assert.Equal(t, "new user agent", config.UserAgent)
+  assert.Equal(t, true, config.Debug)
+  assert.Equal(t, time.Duration(500000), config.Timeout)
+  assert.Equal(t, transport, config.HttpTransport)
+  assert.Equal(t, true, config.EnableAsync)
+  assert.Equal(t, 1, config.MaxTaskQueueSize)
+  assert.Equal(t, 10, config.GoRoutinePoolSize)
+  assert.Equal(t, "HTTPS", config.Scheme)
+}