From a993531e75034edbcd9139c1554af0fe59000448 Mon Sep 17 00:00:00 2001 From: Ming Deng <mindeng@ebay.com> Date: Thu, 14 Nov 2019 23:32:01 +0800 Subject: [PATCH] Fix UT --- common/constant/key.go | 4 ++-- common/extension/graceful_shutdown.go | 4 ++-- config/consumer_config.go | 12 ++++++------ config/graceful_shutdown_config.go | 17 +++++++++-------- config/graceful_shutdown_config_test.go | 4 ++-- config/graceful_shutdown_test.go | 10 ++++------ config/provider_config.go | 12 ++++++------ filter/impl/graceful_shutdown_filter_test.go | 2 +- 8 files changed, 32 insertions(+), 33 deletions(-) diff --git a/common/constant/key.go b/common/constant/key.go index 2bfebf8f0..122b8e505 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -71,8 +71,8 @@ const ( EXECUTE_LIMIT_KEY = "execute.limit" DEFAULT_EXECUTE_LIMIT = "-1" EXECUTE_REJECTED_EXECUTION_HANDLER_KEY = "execute.limit.rejected.handler" - PROVIDER_SHUTDOWN_FILTER = "pshutdown" - CONSUMER_SHUTDOWN_FILTER = "cshutdown" + PROVIDER_SHUTDOWN_FILTER = "pshutdown" + CONSUMER_SHUTDOWN_FILTER = "cshutdown" ) const ( diff --git a/common/extension/graceful_shutdown.go b/common/extension/graceful_shutdown.go index 02fedc683..c8807fcc2 100644 --- a/common/extension/graceful_shutdown.go +++ b/common/extension/graceful_shutdown.go @@ -22,7 +22,7 @@ import ( ) var ( - customShutdownCallbacks = list.New() + customShutdownCallbacks = list.New() ) /** @@ -44,7 +44,7 @@ var ( * the benefit of that mechanism is low. * And it may introduce much complication for another users. */ -func AddCustomShutdownCallback(callback func()) { +func AddCustomShutdownCallback(callback func()) { customShutdownCallbacks.PushBack(callback) } diff --git a/config/consumer_config.go b/config/consumer_config.go index 54b87e22f..72f60b5f7 100644 --- a/config/consumer_config.go +++ b/config/consumer_config.go @@ -52,12 +52,12 @@ type ConsumerConfig struct { ProxyFactory string `yaml:"proxy_factory" default:"default" json:"proxy_factory,omitempty" property:"proxy_factory"` Check *bool `yaml:"check" json:"check,omitempty" property:"check"` - Registry *RegistryConfig `yaml:"registry" json:"registry,omitempty" property:"registry"` - Registries map[string]*RegistryConfig `yaml:"registries" json:"registries,omitempty" property:"registries"` - References map[string]*ReferenceConfig `yaml:"references" json:"references,omitempty" property:"references"` - ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf"` - FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" ` - ShutdownConfig *ShutdownConfig `yaml:"shutdown_conf" json:"shutdown_conf,omitempty" property:"shutdown_conf" ` + Registry *RegistryConfig `yaml:"registry" json:"registry,omitempty" property:"registry"` + Registries map[string]*RegistryConfig `yaml:"registries" json:"registries,omitempty" property:"registries"` + References map[string]*ReferenceConfig `yaml:"references" json:"references,omitempty" property:"references"` + ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf"` + FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" ` + ShutdownConfig *ShutdownConfig `yaml:"shutdown_conf" json:"shutdown_conf,omitempty" property:"shutdown_conf" ` } func (c *ConsumerConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { diff --git a/config/graceful_shutdown_config.go b/config/graceful_shutdown_config.go index c16123624..d7d21c149 100644 --- a/config/graceful_shutdown_config.go +++ b/config/graceful_shutdown_config.go @@ -18,16 +18,17 @@ package config import ( - "strconv" "time" ) import ( "github.com/apache/dubbo-go/common/logger" ) + const ( - defaultTimeout = 60 * time.Second + defaultTimeout = 60 * time.Second defaultStepTimeout = 10 * time.Second ) + type ShutdownConfig struct { /* * Total timeout. Even though we don't release all resources, @@ -35,7 +36,7 @@ type ShutdownConfig struct { * default value is 60 * 1000 ms = 1 minutes * In general, it should be bigger than 3 * StepTimeout. */ - Timeout string `yaml:"timeout" json:"timeout,omitempty" property:"timeout"` + Timeout string `default:"60s" yaml:"timeout" json:"timeout,omitempty" property:"timeout"` /* * the timeout on each step. You should evaluate the response time of request * and the time that client noticed that server shutdown. @@ -43,7 +44,7 @@ type ShutdownConfig struct { * and the 99.9% requests will return response in 2s, so the StepTimeout will be bigger than(10+2) * 1000ms, * maybe (10 + 2*3) * 1000ms is a good choice. */ - StepTimeout string `yaml:"step_timeout" json:"step.timeout,omitempty" property:"step.timeout"` + StepTimeout string `default:"10s" yaml:"step_timeout" json:"step.timeout,omitempty" property:"step.timeout"` // when we try to shutdown the application, we will reject the new requests. In most cases, you don't need to configure this. RejectRequestHandler string `yaml:"reject_handler" json:"reject_handler,omitempty" property:"reject_handler"` // true -> new request will be rejected. @@ -55,21 +56,21 @@ type ShutdownConfig struct { } func (config *ShutdownConfig) GetTimeout() time.Duration { - result, err := strconv.ParseInt(config.Timeout, 0, 0) + result, err := time.ParseDuration(config.Timeout) if err != nil { logger.Errorf("The Timeout configuration is invalid: %s, and we will use the default value: %s", config.Timeout, defaultTimeout.String(), err) return defaultTimeout } - return time.Millisecond * time.Duration(result) + return result } func (config *ShutdownConfig) GetStepTimeout() time.Duration { - result, err := strconv.ParseInt(config.StepTimeout, 0, 0) + result, err := time.ParseDuration(config.StepTimeout) if err != nil { logger.Errorf("The StepTimeout configuration is invalid: %s, and we will use the default value: %s", config.Timeout, defaultStepTimeout.String(), err) return defaultStepTimeout } - return time.Millisecond * time.Duration(result) + return result } diff --git a/config/graceful_shutdown_config_test.go b/config/graceful_shutdown_config_test.go index 6e84f5869..4b6645db0 100644 --- a/config/graceful_shutdown_config_test.go +++ b/config/graceful_shutdown_config_test.go @@ -39,8 +39,8 @@ func TestShutdownConfig_GetTimeout(t *testing.T) { assert.Equal(t, 10*time.Second, config.GetStepTimeout()) config = ShutdownConfig{ - Timeout: "34", - StepTimeout: "79", + Timeout: "34ms", + StepTimeout: "79ms", } assert.Equal(t, 34*time.Millisecond, config.GetTimeout()) diff --git a/config/graceful_shutdown_test.go b/config/graceful_shutdown_test.go index 981de6b60..ca01f6814 100644 --- a/config/graceful_shutdown_test.go +++ b/config/graceful_shutdown_test.go @@ -42,8 +42,6 @@ func TestBeforeShutdown(t *testing.T) { return &mockRegistryProtocol{} }) - - protocolConfigs := make(map[interface{}]interface{}) protocolConfigs[constant.DUBBO] = "aaa" @@ -54,7 +52,7 @@ func TestBeforeShutdown(t *testing.T) { ProtocolConf: protocolConfigs, ShutdownConfig: &ShutdownConfig{ Timeout: "1", - StepTimeout: "1000", + StepTimeout: "1s", }, } @@ -66,7 +64,7 @@ func TestBeforeShutdown(t *testing.T) { providerConfig = &ProviderConfig{ ShutdownConfig: &ShutdownConfig{ Timeout: "1", - StepTimeout: "1000", + StepTimeout: "1s", }, ProtocolConf: providerProtocols, } @@ -76,7 +74,7 @@ func TestBeforeShutdown(t *testing.T) { providerConfig = &ProviderConfig{ ShutdownConfig: &ShutdownConfig{ Timeout: "1", - StepTimeout: "-1", + StepTimeout: "-1s", }, ProtocolConf: protocolConfigs, } @@ -85,7 +83,7 @@ func TestBeforeShutdown(t *testing.T) { ProtocolConf: protocolConfigs, ShutdownConfig: &ShutdownConfig{ Timeout: "1", - StepTimeout: "-1", + StepTimeout: "-1s", }, } diff --git a/config/provider_config.go b/config/provider_config.go index d1de691be..0fed44c81 100644 --- a/config/provider_config.go +++ b/config/provider_config.go @@ -44,12 +44,12 @@ type ProviderConfig struct { ApplicationConfig *ApplicationConfig `yaml:"application" json:"application,omitempty" property:"application"` Registry *RegistryConfig `yaml:"registry" json:"registry,omitempty" property:"registry"` - Registries map[string]*RegistryConfig `yaml:"registries" json:"registries,omitempty" property:"registries"` - Services map[string]*ServiceConfig `yaml:"services" json:"services,omitempty" property:"services"` - Protocols map[string]*ProtocolConfig `yaml:"protocols" json:"protocols,omitempty" property:"protocols"` - ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf" ` - FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" ` - ShutdownConfig *ShutdownConfig `yaml:"shutdown_conf" json:"shutdown_conf,omitempty" property:"shutdown_conf" ` + Registries map[string]*RegistryConfig `yaml:"registries" json:"registries,omitempty" property:"registries"` + Services map[string]*ServiceConfig `yaml:"services" json:"services,omitempty" property:"services"` + Protocols map[string]*ProtocolConfig `yaml:"protocols" json:"protocols,omitempty" property:"protocols"` + ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf" ` + FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" ` + ShutdownConfig *ShutdownConfig `yaml:"shutdown_conf" json:"shutdown_conf,omitempty" property:"shutdown_conf" ` } func (c *ProviderConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { diff --git a/filter/impl/graceful_shutdown_filter_test.go b/filter/impl/graceful_shutdown_filter_test.go index 61a301dc5..f541219b5 100644 --- a/filter/impl/graceful_shutdown_filter_test.go +++ b/filter/impl/graceful_shutdown_filter_test.go @@ -39,7 +39,7 @@ func TestGenericFilter_Invoke(t *testing.T) { invoc := invocation.NewRPCInvocation("GetUser", []interface{}{"OK"}, make(map[string]string, 0)) invokeUrl := common.NewURLWithOptions( - common.WithParams(url.Values{}), ) + common.WithParams(url.Values{})) shutdownFilter := extension.GetFilter(constant.PROVIDER_SHUTDOWN_FILTER).(*gracefulShutdownFilter) -- GitLab