dubbo.apache.org/dubbo-go/v3@v3.1.1/config/graceful_shutdown_config_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package config 19 20 import ( 21 "testing" 22 "time" 23 ) 24 25 import ( 26 "github.com/stretchr/testify/assert" 27 ) 28 29 import ( 30 "dubbo.apache.org/dubbo-go/v3/common/constant" 31 ) 32 33 func TestShutdownConfigGetTimeout(t *testing.T) { 34 config := ShutdownConfig{} 35 assert.False(t, config.RejectRequest.Load()) 36 37 config = ShutdownConfig{ 38 Timeout: "60s", 39 StepTimeout: "10s", 40 OfflineRequestWindowTimeout: "30s", 41 } 42 43 assert.Equal(t, 60*time.Second, config.GetTimeout()) 44 assert.Equal(t, 10*time.Second, config.GetStepTimeout()) 45 assert.Equal(t, 30*time.Second, config.GetOfflineRequestWindowTimeout()) 46 config = ShutdownConfig{ 47 Timeout: "34ms", 48 StepTimeout: "79ms", 49 OfflineRequestWindowTimeout: "13ms", 50 } 51 52 assert.Equal(t, 34*time.Millisecond, config.GetTimeout()) 53 assert.Equal(t, 79*time.Millisecond, config.GetStepTimeout()) 54 assert.Equal(t, 13*time.Millisecond, config.GetOfflineRequestWindowTimeout()) 55 56 // test default 57 config = ShutdownConfig{} 58 59 assert.Equal(t, defaultTimeout, config.GetTimeout()) 60 assert.Equal(t, defaultStepTimeout, config.GetStepTimeout()) 61 assert.Equal(t, defaultOfflineRequestWindowTimeout, config.GetOfflineRequestWindowTimeout()) 62 } 63 64 func TestNewShutDownConfigBuilder(t *testing.T) { 65 config := NewShutDownConfigBuilder(). 66 SetTimeout("10s"). 67 SetStepTimeout("15s"). 68 SetOfflineRequestWindowTimeout("13s"). 69 SetRejectRequestHandler("handler"). 70 SetRejectRequest(true). 71 SetInternalSignal(false). 72 Build() 73 74 assert.Equal(t, config.Prefix(), constant.ShutdownConfigPrefix) 75 76 timeout := config.GetTimeout() 77 assert.Equal(t, timeout, 10*time.Second) 78 79 stepTimeout := config.GetStepTimeout() 80 assert.Equal(t, stepTimeout, 15*time.Second) 81 82 offlineRequestWindowTimeout := config.GetOfflineRequestWindowTimeout() 83 assert.Equal(t, offlineRequestWindowTimeout, 13*time.Second) 84 err := config.Init() 85 assert.NoError(t, err) 86 87 waitTime := config.GetConsumerUpdateWaitTime() 88 assert.Equal(t, waitTime, 3*time.Second) 89 90 assert.Equal(t, config.GetInternalSignal(), false) 91 } 92 93 func TestGetInternalSignal(t *testing.T) { 94 config := NewShutDownConfigBuilder(). 95 SetTimeout("10s"). 96 SetStepTimeout("15s"). 97 SetOfflineRequestWindowTimeout("13s"). 98 SetRejectRequestHandler("handler"). 99 SetRejectRequest(true). 100 Build() 101 102 assert.Equal(t, config.GetInternalSignal(), true) 103 }