我有这个单元测试:
func TestServer(t *testing.T) {
db := prepareDBConn(t)
defer db.Close()
lis := bufconn.Listen(1024 * 1024)
t.Logf("Opened listener: %v", lis)
grpcServer := grpc.NewServer(
withUnaryInterceptor(),
)
t.Logf("Opened grpc server: %v", grpcServer)
signKey := getSignKey()
if signKey == nil {
t.Fatal("Failed to find or parse RSA private key")
}
verifyKeyErr := setVerifyKey()
if verifyKeyErr != nil {
t.Fatal("Failed to find or parse RSA public key")
}
pb.RegisterAuthServiceServer(grpcServer, newAuthServiceServer(db, signKey))
err := grpcServer.Serve(lis)
if err != nil {
t.Fatalf("Failed to listen: %+v", err)
}
}
如果我注释掉最后 4 行:
// err := grpcServer.Serve(lis)
// if err != nil {
// t.Fatalf("Failed to listen: %+v", err)
// }
它每次都会通过,但无论出于何种原因,它都会在 err := grpcServer.Server(lis) 行变得糟糕。我试过使用一个真正的 TCP 监听器(交换 lis := bufconn.Listen 和 lis := net.Listen("tcp")。测试的输出(当它失败时)又长又模棱两可:
panic: test timed out after 2m0s
goroutine 34 [running]:
testing.(*M).startAlarm.func1()
/usr/local/Cellar/go/1.12.7/libexec/src/testing/testing.go:1334 +0xdf
created by time.goFunc
/usr/local/Cellar/go/1.12.7/libexec/src/time/sleep.go:169 +0x44
goroutine 1 [chan receive]:
testing.(*T).Run(0xc000156100, 0x15cb741, 0xa, 0x15ed1a8, 0x10b38a6)
/usr/local/Cellar/go/1.12.7/libexec/src/testing/testing.go:917 +0x381
testing.runTests.func1(0xc000156000)
/usr/local/Cellar/go/1.12.7/libexec/src/testing/testing.go:1157 +0x78
testing.tRunner(0xc000156000, 0xc0000c1e10)
/usr/local/Cellar/go/1.12.7/libexec/src/testing/testing.go:865 +0xc0
testing.runTests(0xc00000e0c0, 0x1a53d60, 0x3, 0x3, 0x0)
/usr/local/Cellar/go/1.12.7/libexec/src/testing/testing.go:1155 +0x2a9
testing.(*M).Run(0xc00013e000, 0x0)
/usr/local/Cellar/go/1.12.7/libexec/src/testing/testing.go:1072 +0x162
_/path/to/my/server.TestMain(0xc00013e000)
/path/to/my/server/main_test.go:102 +0x2b
main.main()
_testmain.go:44 +0x13e
goroutine 19 [chan receive]:
github.com/golang/glog.(*loggingT).flushDaemon(0x1a5f540)
/my/gopath/src/github.com/golang/glog/glog.go:882 +0x8b
created by github.com/golang/glog.init.0
/my/gopath/src/github.com/golang/glog/glog.go:410 +0x272
goroutine 5 [select]:
google.golang.org/grpc/test/bufconn.(*Listener).Accept(0xc00000e0e0, 0x15edf50, 0xc000176000, 0xc00000e420, 0x0)
/my/gopath/src/google.golang.org/grpc/test/bufconn/bufconn.go:51 +0xaf
google.golang.org/grpc.(*Server).Serve(0xc000176000, 0x1684880, 0xc00000e0e0, 0x0, 0x0)
/my/gopath/src/google.golang.org/grpc/server.go:586 +0x1f2
_/path/to/my/server.TestServer(0xc000156100)
/path/to/my/server/main_test.go:67 +0x309
testing.tRunner(0xc000156100, 0x15ed1a8)
/usr/local/Cellar/go/1.12.7/libexec/src/testing/testing.go:865 +0xc0
created by testing.(*T).Run
/usr/local/Cellar/go/1.12.7/libexec/src/testing/testing.go:916 +0x35a
goroutine 6 [select]:
database/sql.(*DB).connectionOpener(0xc00013a0c0, 0x1685780, 0xc00006a300)
/usr/local/Cellar/go/1.12.7/libexec/src/database/sql/sql.go:1000 +0xe8
created by database/sql.OpenDB
/usr/local/Cellar/go/1.12.7/libexec/src/database/sql/sql.go:670 +0x15e
goroutine 7 [select]:
database/sql.(*DB).connectionResetter(0xc00013a0c0, 0x1685780, 0xc00006a300)
/usr/local/Cellar/go/1.12.7/libexec/src/database/sql/sql.go:1013 +0xfb
created by database/sql.OpenDB
/usr/local/Cellar/go/1.12.7/libexec/src/database/sql/sql.go:671 +0x194
exit status 2
它没有告诉我出了什么问题(除了它超时)而且似乎也引用了其他库的测试?我通过 vs-code 调用测试,它使用这个命令:
/usr/local/bin/go test -timeout 30s -run ^(TestServer)$
我可以构建并运行这个包,它完全按照我的预期工作,并且不会在那一行失败,所以我认为我在单元测试方面做错了什么,但我在这一点上没有太多可做的我对去测试不太了解。
最佳答案
如我所见,您正在尝试对 gRPC 服务器进行单元测试。您尝试使用的策略对于单元测试来说有点矫枉过正。
为简单起见,您应该有一个服务器结构来保存相互连接的部分,例如:数据库连接。
因此您只需初始化数据库连接并将其传递给服务器结构。这是模拟数据库的正确位置。 之后,您将拥有启动的 gRPC 服务器实例,因为您不需要为单元测试目的调用 gRPC。
然后您可以在初始化的服务器实例上调用预期的 rpc 函数。它看起来更清洁和可维护。
关于unit-testing - 尝试在 TCP 监听器上提供服务时,由于超时,Go 测试模棱两可地失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57207275/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
使用rails4,ruby2。我在rails配置中为我的cookiesession设置了30分钟的超时时间。问题是,如果我转到表单,让session超时,然后提交表单,我会收到此ActionController::InvalidAuthenticityToken错误。如何在Rails中优雅地处理这个错误?比如说,重定向到登录屏幕? 最佳答案 在您的ApplicationController:rescue_fromActionController::InvalidAuthenticityTokendoredirect_tosome_p
在Ruby中,我需要在n毫秒秒后暂停一段代码的执行。我知道RubyTimeout库支持秒的超时:http://ruby-doc.org/stdlib/libdoc/timeout/rdoc/index.html这可能吗? 最佳答案 只需为超时使用十进制值。n毫秒的示例:Timeout::timeout(n/1000.0){sleep(100)} 关于Ruby在n*milli*秒后超时一段代码,我们在StackOverflow上找到一个类似的问题: https:
在Test::Unit中的ruby单元测试断言失败后,在执行teardown之前,是否有一些简洁优雅的方法来立即执行我的代码?我正在做一些自动化的GUI测试,并希望在出现问题后立即截图。 最佳答案 如果您使用的是1.9,请不要使用Test::Unit::TestCase作为您的基类。对其进行子类化并覆盖#run_test以进行救援,截取屏幕截图并重新提出:classMyAbstractTestCase或者,我认为这实际上是最简洁的方法,您可以使用before_teardownHook:classMyTestCase这不适用于1.
在Railcasts上,我注意到一个非常有趣的功能“转到符号”窗口。它像Command-T一样工作,但显示当前文件中可用的类和方法。如何在vim中获取它? 最佳答案 尝试:helptags有各种程序和脚本可以生成标记文件。此外,标记文件格式非常简单,因此很容易将sed(1)或类似的脚本组合在一起,无论您使用何种语言,它们都可以生成标记文件。轻松获取标记文件(除了下载生成器之外)的关键在于格式化样式而不是实际解析语法。 关于ruby-on-rails-Textmate'Gotosymbol
显然在Test::Unit中没有assert_false。您将如何通过扩展断言并添加文件config/initializers/assertions_helper.rb来添加它?这是最好的方法吗?我不想修改test/unit/assertions.rb。顺便说一句,我不认为这是多余的。我使用的是assert_equalfalse,something_to_evaluate。这种方法的问题是很容易意外使用assertfalse,something_to_evaluate。这将始终失败,不会引发错误或警告,并且会在测试中引入错误。 最佳答案
我有一个基于1.8.7构建的应用程序,我正尝试在1.9.3的系统上启动它当我运行脚本/服务器时,我得到:/usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in`require':cannotloadsuchfile--test/unit/error(LoadError)from/usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in`require'我的服务器脚本如下所示:#!/usr/bin/envrubyrequireFile.expand_path('../.
我希望我的后台作业能够内联运行某些标记测试。我可以通过用perform_enqueueddo包装测试来做到这一点,但我希望能够用元数据标记它们,如果可能的话,它会自动发生。我试过以下方法:it"doeseverythinginthejobtoo",perform_enqueued:truedoendconfig.around(:each)do|example|ifexample.metadata[:perform_enqueued]perform_enqueued_jobsdoexample.runendendend但它会导致错误:undefinedmethod`perform_enq
我最近正在进行Rails5升级,当我尝试启动Rails控制台时遇到了这个错误:/actionpack-5.0.0/lib/action_controller/test_case.rb:49:ininitialize':wrongnumberofarguments(0for2)(ArgumentError)当前bundleupdaterails已经完成了gem依赖项的解决,足以更新到5.0.0,rspec正在运行(尽管我正在修复很多中断)。我也可以运行railss没有错误。这里是代码中断行:https://github.com/rails/rails/blob/master/action