4 回答

TA貢獻2051條經(jīng)驗 獲得超10個贊
我懷疑錯誤信息:
Unable to start NATS Server in Go Routine
具有誤導(dǎo)性。關(guān)于 Go 例程的部分可能只是它如何嘗試啟動 NATS 服務(wù)器的實現(xiàn)細節(jié)。我懷疑服務(wù)器無法啟動,因為您在選項中對端口進行了硬編碼,并且因為多個測試包可能正在并行運行。只有一個測試包能夠綁定到端口來啟動 NATS 服務(wù)器,所有其他測試包都將失敗。
標志上的文檔go test說:
-parallel n
Allow parallel execution of test functions that call t.Parallel.
The value of this flag is the maximum number of tests to run
simultaneously; by default, it is set to the value of GOMAXPROCS.
Note that -parallel only applies within a single test binary.
The 'go test' command may run tests for different packages
in parallel as well, according to the setting of the -p flag
(see 'go help build').
除非您在給定的測試套件中有并行測試(我假設(shè)您沒有),否則問題可能是并行運行的不同包。按照上面末尾的線索,讓我們看看構(gòu)建標志:
-p n
the number of programs, such as build commands or
test binaries, that can be run in parallel.
The default is the number of CPUs available.
這是來自“編譯包和依賴項”文檔。請注意,您可以將構(gòu)建標志和測試標志傳遞給go test:
$ go test -h
usage: go test [build/test flags] [packages] [build/test flags & test binary flags]
Run 'go help test' for details.
所以考慮跑步go test -p 1 ./...?;蛘?,將選項傳遞給RunServer函數(shù),允許多個選項安全地并行發(fā)生。

TA貢獻1765條經(jīng)驗 獲得超5個贊
每次測試它可能已經(jīng)啟動了一個服務(wù)器。沒有 goroutine 就無法同時運行。這個錯誤實際上沒有任何意義 - Go 中的任何東西都必須能夠在 goroutine 中運行,事實上一切都在goroutine 中運行(甚至main
),所以不清楚他們在這里真正想說的是什么(我會絕對建議打開有關(guān)它的錯誤報告,因為該錯誤消息很糟糕)。同時,您可以避免go test
同時運行-parallel 1
(如 中所示go help testflag
),這可能會有所幫助。
- 4 回答
- 0 關(guān)注
- 261 瀏覽
添加回答
舉報