登录
首页 >  Golang >  Go问答

Golang测试失败

来源:stackoverflow

时间:2024-03-24 10:27:44 295浏览 收藏

在修改应用程序后,测试开始挂起。问题追溯到依赖项 `github.com/mattn/go-sqlite3`。如果注释掉该依赖项,测试可以正常运行。当使用 `go run` 运行时,应用程序可以正常工作,但使用 `go test` 时,测试会挂起。这是因为 `go-sqlite3` 包使用 CGO,第一次引用时需要编译关联的 C 代码。

问题内容

我最近修改了我的应用程序,发现测试开始挂起。这是精简的测试代码:

package app_test                                                                    
                                                                                    
import (                                                                            
    "testing"                                                                   
    "github.com/kargirwar/prosql-go/db"                                         
)                                                                                   
                                                                                    
func testapp(t *testing.t) {                                                        
    db.setdbpath("")                                                            
}

db包如下:

package db                                                                          
                                                                                    
import (                                                                            
        "os"                                                                        
        "context"                                                                   
        "database/sql"                                                              
        _ "github.com/mattn/go-sqlite3"                                             
        "path/filepath"                                                             
)                                                                                   
                                                                                    
var dbpath string                                                                   
                                                                                    
func setdbpath(path string) {                                                       
        dbpath = path                                                               
}                                                                                   
                                                                                    
func opendb(ctx context.context, db string) (*sql.db, error) {                      
        db = filepath.join(dbpath, db)                                              
                                                                                    
        _, err := os.openfile(db, os.o_rdwr, 0600)                                  
                                                                                    
        if err != nil {                                                             
                return nil, err                                                     
        }                                                                           
                                                                                    
        return sql.open("sqlite3", "file:"+db+"?_foreign_keys=true")                
}

我将问题追溯到这个依赖项:

_ "github.com/mattn/go-sqlite3"

如果我将其注释掉,那么测试就会正常运行,否则就会挂起。

奇怪的是, go run 工作得很好。 google 说 go-sqlite3 需要时间来编译,但为什么 go run 可以正常工作?


正确答案


sqlite3 包使用 cgo。第一次引用 sqlite3 包时,会编译并缓存关联的 sqlite3 c 代码以供以后使用。

$ cat x.go
package main

import _ "github.com/mattn/go-sqlite3" 

func main(){}
$ cat x_test.go
package main

import "testing"

func testsqlite(t *testing.t) {}
$ go clean -cache
$ time go run -v x.go
github.com/mattn/go-sqlite3
command-line-arguments
real    0m41.378s
user    0m41.176s
sys     0m1.353s
$ time go run -v x.go
real    0m0.506s
user    0m0.571s
sys     0m0.209s
$ go clean -cache
$ time go build -v x.go 
github.com/mattn/go-sqlite3
real    0m41.038s
user    0m40.779s
sys     0m1.280s
$ time go build -v x.go 
real    0m0.239s
user    0m0.379s
sys     0m0.101s
$ go clean -cache
$ time go test -v x.go x_test.go
=== RUN   TestSQLite
--- PASS: TestSQLite (0.00s)
PASS
ok      command-line-arguments  0.003s
real    0m42.751s
user    0m44.702s
sys     0m2.097s
$ time go test -v x.go x_test.go
=== RUN   TestSQLite
--- PASS: TestSQLite (0.00s)
PASS
ok      command-line-arguments  0.002s
real    0m0.618s
user    0m0.752s
sys     0m0.306s
$

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>