github.com/polarismesh/polaris@v1.17.8/admin/job/delete_empty_service_test.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package job 19 20 import ( 21 "testing" 22 "time" 23 24 "github.com/polarismesh/polaris/common/model" 25 ) 26 27 func Test_DeleteEmptyAutoCreatedServiceJobConfigInit(t *testing.T) { 28 expectValue := 1 * time.Minute 29 raw := map[string]interface{}{ 30 "serviceDeleteTimeout": "1m", 31 } 32 33 job := deleteEmptyServiceJob{} 34 err := job.init(raw) 35 if err != nil { 36 t.Errorf("init deleteEmptyServiceJob config, err: %v", err) 37 } 38 39 if job.cfg.ServiceDeleteTimeout != expectValue { 40 t.Errorf("init deleteEmptyServiceJob config. expect: %s, actual: %s", 41 expectValue, job.cfg.ServiceDeleteTimeout) 42 } 43 } 44 45 func Test_DeleteEmptyAutoCreatedServiceJobConfigInitErr(t *testing.T) { 46 raw := map[string]interface{}{ 47 "serviceDeleteTimeout": "xx", 48 } 49 50 job := deleteEmptyServiceJob{} 51 err := job.init(raw) 52 if err == nil { 53 t.Errorf("init deleteEmptyServiceJob config should err") 54 } 55 } 56 57 func Test_FilterToDeletedServices(t *testing.T) { 58 job := deleteEmptyServiceJob{} 59 t1, _ := time.Parse("2006-01-02 15:04:05", "2023-03-20 12:01:00") 60 t2, _ := time.Parse("2006-01-02 15:04:05", "2023-03-20 12:02:00") 61 job.emptyServices = map[string]time.Time{ 62 "a": t1, 63 "b": t2, 64 } 65 66 services := []*model.Service{ 67 { 68 ID: "a", 69 }, 70 { 71 ID: "b", 72 }, 73 { 74 ID: "c", 75 }, 76 } 77 78 now, _ := time.Parse("2006-01-02 15:04:05", "2023-03-20 12:03:00") 79 toDeleteServices := job.filterToDeletedServices(services, now, time.Minute) 80 if len(toDeleteServices) != 1 { 81 t.Errorf("one service should be deleted") 82 } 83 if toDeleteServices[0].ID != "a" { 84 t.Errorf("to deleted service. expect: %s, actual: %s", "a", toDeleteServices[0].ID) 85 } 86 87 if len(job.emptyServices) != 2 { 88 t.Errorf("two service should be candicated, actual: %v", job.emptyServices) 89 } 90 svcBTime := job.emptyServices["b"] 91 if svcBTime != t2 { 92 t.Errorf("empty service record time. expect: %s, actual: %s", t2, svcBTime) 93 } 94 }