github.com/polarismesh/polaris@v1.17.8/store/boltdb/config_file_release_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 boltdb 19 20 import ( 21 "fmt" 22 "testing" 23 "time" 24 25 "github.com/stretchr/testify/assert" 26 27 "github.com/polarismesh/polaris/common/model" 28 ) 29 30 func mockConfigFileRelease(total int) []*model.ConfigFileRelease { 31 32 ret := make([]*model.ConfigFileRelease, 0, total) 33 34 for i := 0; i < total; i++ { 35 ret = append(ret, &model.ConfigFileRelease{ 36 SimpleConfigFileRelease: &model.SimpleConfigFileRelease{ 37 ConfigFileReleaseKey: &model.ConfigFileReleaseKey{ 38 Name: fmt.Sprintf("config-file-release-%d", i), 39 Namespace: fmt.Sprintf("config-file-release-%d", i), 40 Group: fmt.Sprintf("config-file-release-%d", i), 41 FileName: fmt.Sprintf("config-file-release-%d", i), 42 }, 43 Comment: fmt.Sprintf("config-file-release-%d", i), 44 Md5: fmt.Sprintf("config-file-release-%d", i), 45 Version: 0, 46 Flag: 0, 47 CreateTime: time.Time{}, 48 CreateBy: "", 49 ModifyTime: time.Time{}, 50 ModifyBy: "", 51 Valid: false, 52 }, 53 Content: fmt.Sprintf("config-file-release-%d", i), 54 }) 55 } 56 57 return ret 58 } 59 60 func Test_configFileReleaseStore(t *testing.T) { 61 t.Run("创建配置Release", func(t *testing.T) { 62 CreateTableDBHandlerAndRun(t, tblConfigFileRelease, func(t *testing.T, handler BoltHandler) { 63 64 s := &configFileReleaseStore{handler: handler} 65 66 ret := mockConfigFileRelease(1) 67 68 for i := range ret { 69 func() { 70 tx, err := handler.StartTx() 71 assert.NoError(t, err, err) 72 defer tx.Rollback() 73 74 err = s.CreateConfigFileReleaseTx(tx, ret[i]) 75 assert.NoError(t, err, err) 76 err = tx.Commit() 77 assert.NoError(t, err, err) 78 }() 79 } 80 }) 81 }) 82 83 t.Run("删除配置Release", func(t *testing.T) { 84 CreateTableDBHandlerAndRun(t, tblConfigFileRelease, func(t *testing.T, handler BoltHandler) { 85 86 s := &configFileReleaseStore{handler: handler} 87 88 ret := mockConfigFileRelease(1) 89 90 for i := range ret { 91 tx, err := handler.StartTx() 92 assert.NoError(t, err, err) 93 defer tx.Rollback() 94 95 err = s.CreateConfigFileReleaseTx(tx, ret[i]) 96 assert.NoError(t, err, err) 97 98 searchKey := &model.ConfigFileReleaseKey{ 99 Namespace: ret[i].Namespace, 100 Group: ret[i].Group, 101 FileName: ret[i].FileName, 102 Name: ret[i].Name, 103 } 104 err = s.DeleteConfigFileReleaseTx(tx, searchKey) 105 assert.NoError(t, err, err) 106 107 oldCfr, err := s.GetConfigFileRelease(searchKey) 108 assert.NoError(t, err, err) 109 assert.Nil(t, oldCfr) 110 111 err = tx.Commit() 112 assert.NoError(t, err, err) 113 } 114 }) 115 }) 116 117 t.Run("查询Release信息-用于刷新Cache缓存", func(t *testing.T) { 118 CreateTableDBHandlerAndRun(t, tblConfigFileRelease, func(t *testing.T, handler BoltHandler) { 119 120 s := &configFileReleaseStore{handler: handler} 121 122 ret := mockConfigFileRelease(10) 123 124 save := make([]*model.ConfigFileRelease, 0, len(ret)) 125 126 for i := range ret { 127 tx, err := handler.StartTx() 128 assert.NoError(t, err, err) 129 defer tx.Rollback() 130 131 err = s.CreateConfigFileReleaseTx(tx, ret[i]) 132 assert.NoError(t, err, err) 133 err = tx.Commit() 134 assert.NoError(t, err, err) 135 save = append(save, ret[i]) 136 } 137 138 result, err := s.GetMoreReleaseFile(true, time.Time{}) 139 assert.NoError(t, err, err) 140 assert.Equal(t, int(len(save)), int(len(result))) 141 142 result, err = s.GetMoreReleaseFile(false, time.Now().Add(time.Duration(1*time.Hour))) 143 assert.NoError(t, err, err) 144 assert.Empty(t, result) 145 }) 146 }) 147 }