github.com/cloudwego/kitex@v0.9.0/pkg/generic/thriftidl_provider_test.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package generic 18 19 import ( 20 "strings" 21 "testing" 22 23 dthrift "github.com/cloudwego/dynamicgo/thrift" 24 25 "github.com/cloudwego/kitex/internal/test" 26 ) 27 28 func TestAbsPath(t *testing.T) { 29 t.Run("relative include path", func(t *testing.T) { 30 path := "/a/b/c.thrift" 31 includePath := "../d.thrift" 32 result := "/a/d.thrift" 33 p := absPath(path, includePath) 34 test.DeepEqual(t, result, p) 35 }) 36 t.Run("abs include path", func(t *testing.T) { 37 path := "/a/b/c.thrift" 38 includePath := "/a/d.thrift" 39 result := "/a/d.thrift" 40 p := absPath(path, includePath) 41 test.DeepEqual(t, result, p) 42 }) 43 } 44 45 func TestThriftFileProvider(t *testing.T) { 46 path := "http_test/idl/binary_echo.thrift" 47 p, err := NewThriftFileProvider(path) 48 test.Assert(t, err == nil) 49 defer p.Close() 50 pd, ok := p.(GetProviderOption) 51 test.Assert(t, ok) 52 test.Assert(t, !pd.Option().DynamicGoEnabled) 53 tree := <-p.Provide() 54 test.Assert(t, tree != nil) 55 test.Assert(t, tree.DynamicGoDsc == nil) 56 57 p, err = NewThriftFileProviderWithDynamicGo(path) 58 test.Assert(t, err == nil) 59 defer p.Close() 60 pd, ok = p.(GetProviderOption) 61 test.Assert(t, ok) 62 test.Assert(t, pd.Option().DynamicGoEnabled) 63 tree = <-p.Provide() 64 test.Assert(t, tree != nil) 65 test.Assert(t, tree.DynamicGoDsc != nil) 66 } 67 68 func TestThriftContentWithAbsIncludePathProvider(t *testing.T) { 69 path := "a/b/main.thrift" 70 content := ` 71 namespace go kitex.test.server 72 include "x.thrift" 73 include "../y.thrift" 74 75 service InboxService {} 76 ` 77 newContent := ` 78 namespace go kitex.test.server 79 include "x.thrift" 80 include "../y.thrift" 81 82 service UpdateService {} 83 ` 84 includes := map[string]string{ 85 path: content, 86 "a/b/x.thrift": "namespace go kitex.test.server", 87 "a/y.thrift": ` 88 namespace go kitex.test.server 89 include "z.thrift" 90 `, 91 "a/z.thrift": "namespace go kitex.test.server", 92 } 93 94 p, err := NewThriftContentWithAbsIncludePathProvider(path, includes) 95 test.DeepEqual(t, nil, err) 96 defer p.Close() 97 test.Assert(t, !p.opts.DynamicGoEnabled) 98 tree := <-p.Provide() 99 test.Assert(t, tree != nil) 100 test.Assert(t, tree.Name == "InboxService") 101 test.Assert(t, tree.DynamicGoDsc == nil) 102 includes[path] = newContent 103 err = p.UpdateIDL(path, includes) 104 test.Assert(t, err == nil) 105 defer p.Close() 106 tree = <-p.Provide() 107 test.Assert(t, tree != nil) 108 test.Assert(t, tree.Name == "UpdateService") 109 test.Assert(t, tree.DynamicGoDsc == nil) 110 111 includes[path] = content 112 p, err = NewThriftContentWithAbsIncludePathProviderWithDynamicGo(path, includes) 113 test.Assert(t, err == nil) 114 defer p.Close() 115 test.Assert(t, p.opts.DynamicGoEnabled) 116 tree = <-p.Provide() 117 test.Assert(t, tree != nil) 118 test.Assert(t, tree.Name == "InboxService") 119 test.Assert(t, tree.DynamicGoDsc != nil) 120 test.Assert(t, tree.DynamicGoDsc.Name() == "InboxService") 121 includes[path] = newContent 122 err = p.UpdateIDL(path, includes) 123 test.Assert(t, err == nil) 124 defer p.Close() 125 tree = <-p.Provide() 126 test.Assert(t, tree != nil) 127 test.Assert(t, tree.Name == "UpdateService") 128 test.Assert(t, tree.DynamicGoDsc != nil) 129 test.Assert(t, tree.DynamicGoDsc.Name() == "UpdateService") 130 } 131 132 func TestCircularDependency(t *testing.T) { 133 main := "a.thrift" 134 content := ` 135 include "b.thrift" 136 include "c.thrift" 137 138 service a {} 139 ` 140 includes := map[string]string{ 141 main: content, 142 "b.thrift": ` 143 include "c.thrift" 144 include "d.thrift" 145 `, 146 "c.thrift": ` 147 include "d.thrift" 148 `, 149 "d.thrift": `include "a.thrift"`, 150 } 151 p, err := NewThriftContentWithAbsIncludePathProvider(main, includes) 152 test.Assert(t, err != nil && p == nil) 153 test.Assert(t, strings.HasPrefix(err.Error(), "IDL circular dependency:")) 154 } 155 156 func TestThriftContentProvider(t *testing.T) { 157 p, err := NewThriftContentProvider("test", nil) 158 test.Assert(t, err != nil) 159 test.Assert(t, p == nil) 160 161 content := ` 162 namespace go kitex.test.server 163 164 service InboxService {} 165 ` 166 newContent := ` 167 namespace go kitex.test.server 168 169 service UpdateService {} 170 ` 171 172 p, err = NewThriftContentProvider(content, nil) 173 test.Assert(t, err == nil, err) 174 defer p.Close() 175 test.Assert(t, !p.opts.DynamicGoEnabled) 176 tree := <-p.Provide() 177 test.Assert(t, tree != nil) 178 test.Assert(t, tree.Name == "InboxService") 179 test.Assert(t, tree.DynamicGoDsc == nil) 180 err = p.UpdateIDL(newContent, nil) 181 test.Assert(t, err == nil) 182 defer p.Close() 183 tree = <-p.Provide() 184 test.Assert(t, tree != nil) 185 test.Assert(t, tree.Name == "UpdateService") 186 test.Assert(t, tree.DynamicGoDsc == nil) 187 188 p, err = NewThriftContentProviderWithDynamicGo(content, nil) 189 test.Assert(t, err == nil, err) 190 defer p.Close() 191 test.Assert(t, p.opts.DynamicGoEnabled) 192 tree = <-p.Provide() 193 test.Assert(t, tree != nil) 194 test.Assert(t, tree.DynamicGoDsc != nil) 195 err = p.UpdateIDL(newContent, nil) 196 test.Assert(t, err == nil) 197 defer p.Close() 198 tree = <-p.Provide() 199 test.Assert(t, tree != nil) 200 test.Assert(t, tree.Name == "UpdateService") 201 test.Assert(t, tree.DynamicGoDsc != nil) 202 test.Assert(t, tree.DynamicGoDsc.Name() == "UpdateService") 203 } 204 205 func TestFallback(t *testing.T) { 206 path := "http_test/idl/dynamicgo_go_tag_error.thrift" 207 p, err := NewThriftFileProviderWithDynamicGo(path) 208 test.Assert(t, err == nil) 209 defer p.Close() 210 pd, ok := p.(GetProviderOption) 211 test.Assert(t, ok) 212 test.Assert(t, pd.Option().DynamicGoEnabled) 213 tree := <-p.Provide() 214 test.Assert(t, tree != nil) 215 test.Assert(t, tree.Functions["BinaryEcho"].Request.Struct.FieldsByName["req"].Type.Struct.FieldsByID[int32(1)].Alias == "STR") 216 test.Assert(t, tree.DynamicGoDsc != nil) 217 } 218 219 func TestDisableGoTagForDynamicGo(t *testing.T) { 220 path := "http_test/idl/binary_echo.thrift" 221 p, err := NewThriftFileProviderWithDynamicGo(path) 222 test.Assert(t, err == nil) 223 defer p.Close() 224 tree := <-p.Provide() 225 test.Assert(t, tree != nil) 226 test.Assert(t, tree.DynamicGoDsc != nil) 227 test.Assert(t, tree.DynamicGoDsc.Functions()["BinaryEcho"].Request().Struct().FieldByKey("req").Type().Struct().FieldById(4).Alias() == "STR") 228 229 dthrift.RemoveAnnotationMapper(dthrift.AnnoScopeField, "go.tag") 230 p, err = NewThriftFileProviderWithDynamicGo(path) 231 test.Assert(t, err == nil) 232 defer p.Close() 233 tree = <-p.Provide() 234 test.Assert(t, tree != nil) 235 test.Assert(t, tree.DynamicGoDsc != nil) 236 test.Assert(t, tree.DynamicGoDsc.Functions()["BinaryEcho"].Request().Struct().FieldByKey("req").Type().Struct().FieldById(4).Alias() == "str") 237 }