Why does this program carry test flags once compiled?

166 views
Skip to first unread message

Howard Guo

unread,
Jul 22, 2017, 5:08:08 AM7/22/17
to golang-nuts
Good day fellow gophers.

Can anyone please help me to understand, why the command line help refers to test flags (e.g. test.bench) once this program is compiled?


Thanks & kind regards,
Howard

Jan Mercl

unread,
Jul 22, 2017, 5:48:13 AM7/22/17
to Howard Guo, golang-nuts

On Sat, Jul 22, 2017 at 11:08 AM Howard Guo <guoh...@gmail.com> wrote:

> Can anyone please help me to understand, why the command line help refers to test flags (e.g. test.bench) once this program is compiled?

jnml@4670:~/src/github.com/HouzuoGuo/laitos$ grep -nwrl '"testing"' * | grep -v _test.go
frontend/mailp/mailp.go
frontend/smtpd/smtpd.go
frontend/plain/tcp.go
frontend/plain/udp.go
frontend/httpd/httpd.go
frontend/sockd/sockd.go
frontend/dnsd/tcp.go
frontend/dnsd/udp.go
frontend/healthcheck/healthcheck.go
frontend/telegrambot/bot.go

--

-j

Tamás Gulácsi

unread,
Jul 22, 2017, 10:09:41 AM7/22/17
to golang-nuts
Just to clarify: Jan showed that you import the "testing" package, not just from ..._test.go files.

Nigel Tao

unread,
Jul 23, 2017, 12:54:28 AM7/23/17
to Howard Guo, golang-nuts
On Sat, Jul 22, 2017 at 7:08 PM, Howard Guo <guoh...@gmail.com> wrote:
> https://github.com/HouzuoGuo/laitos

It's tangential to your question, but out of curiousity, I skimmed the
source code. In things like func TestUDPServer in
https://github.com/HouzuoGuo/laitos/blob/master/frontend/plain/udp.go,
you say:

var stoppedNormally bool
go func() {
etc
stoppedNormally = true
}()
etc
server.Stop()
time.Sleep(1 * time.Second)
if !stoppedNormally { etc }

This is an example of incorrect synchronization as per
https://golang.org/ref/mem#tmp_10

There may be other such races in your code. I didn't do an exhaustive
review. It may be worthwhile to run your test suite under the race
detector.

Howard Guo

unread,
Jul 24, 2017, 4:12:56 AM7/24/17
to golang-nuts, guoh...@gmail.com
Thanks Nigel! I should definitely consider improving the test case.

Go compiler appears to automatically places test flags into executable whenever "testing" package is included - no matter the function using "testing" package is dead code or otherwise. So here's a bug report for go:

Kind regards,
Howard

roger peppe

unread,
Jul 24, 2017, 4:45:14 AM7/24/17
to Howard Guo, golang-nuts
On 24 July 2017 at 09:12, Howard Guo <guoh...@gmail.com> wrote:
Thanks Nigel! I should definitely consider improving the test case.

Go compiler appears to automatically places test flags into executable whenever "testing" package is included - no matter the function using "testing" package is dead code or otherwise. So here's a bug report for go:

Although this could be considered a bug, it's not one that can be fixed,
because people rely on this behaviour of the testing package.

Consider it a warning not to include testing dependencies in production code.
 

Kind regards,
Howard

On Sunday, 23 July 2017 06:54:28 UTC+2, Nigel Tao wrote:
On Sat, Jul 22, 2017 at 7:08 PM, Howard Guo <guoh...@gmail.com> wrote:
> https://github.com/HouzuoGuo/laitos

It's tangential to your question, but out of curiousity, I skimmed the
source code. In things like func TestUDPServer in
https://github.com/HouzuoGuo/laitos/blob/master/frontend/plain/udp.go,
you say:

var stoppedNormally bool
go func() {
  etc
  stoppedNormally = true
}()
etc
server.Stop()
time.Sleep(1 * time.Second)
if !stoppedNormally { etc }

This is an example of incorrect synchronization as per
https://golang.org/ref/mem#tmp_10

There may be other such races in your code. I didn't do an exhaustive
review. It may be worthwhile to run your test suite under the race
detector.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Broadfoot

unread,
Jul 24, 2017, 11:47:30 PM7/24/17
to roger peppe, Howard Guo, golang-nuts
One way around the flag problem is to accept an interface, instead of using testing.T.

Watch Mitchell Hashimoto's talk from GopherCon from about 39m30s:

Rob Pike

unread,
Jul 25, 2017, 1:01:24 AM7/25/17
to Chris Broadfoot, roger peppe, Howard Guo, golang-nuts
I haven't got the context but I agree that importing the testing package outside of a test is a very bad idea. If I could, I would disallow it altogether.

-rob

Howard Guo

unread,
Jul 25, 2017, 10:59:32 AM7/25/17
to golang-nuts, rogp...@gmail.com, guoh...@gmail.com
Many thanks Chris, that's a wonderful idea and I've successfully got rid of the test mode flags!
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages