Skip to content

Commit

Permalink
添加测试
Browse files Browse the repository at this point in the history
  • Loading branch information
juniaoshaonian committed Jun 3, 2024
1 parent e738459 commit 20b31b3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion spi/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func LoadService[T any](dir string, symName string) ([]T, error) {
// 尝试将符号断言为接口类型
service, ok := sym.(T)
if !ok {
return errors.New("插件非该接口类型")
return ErrInvalidSo
}
// 收集服务
services = append(services, service)
Expand Down
8 changes: 8 additions & 0 deletions spi/spi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ func (l *LoadServiceSuite) Test_LoadService() {
return assert.ErrorIs(t, err, ErrSymbolNameNotFound)
},
},
{
name: "加载的对象未实现对应的抽象",
dir: "./testdata/user_service3",
svcName: "UserSvc",
assertFunc: func(t assert.TestingT, err error, i ...interface{}) bool {
return assert.ErrorIs(t, err, ErrInvalidSo)
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
27 changes: 27 additions & 0 deletions spi/testdata/user_service3/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

// 测试用

//go:generate go build -race --buildmode=plugin -o a.so ./a.go

type UserService struct{}

func (u UserService) GetV1() string {
return "Get"
}

var UserSvc UserService

0 comments on commit 20b31b3

Please sign in to comment.