0

Is there a way to disable the data race checking in a compiled test binary?

Sometimes I want to specifically exclude binaries in my test suite. Of course I could just not run it with go test -race but I'd have to rewrite part of our test pipeline and thus selectively disabling on a binary would make more sense.

kostix
  • 51,517
  • 14
  • 93
  • 176
hbogert
  • 4,198
  • 5
  • 24
  • 38
  • 2
    Why would you want to disable the race detector? – Jonathan Hall Oct 13 '20 at 11:55
  • @Flimzy, using it severely impacts performance. For this reason, here at my $dayjob we only run tests under race detector on beefy CI machines, nightly. Still, I can't really fathom a case where I would need to run the same set of tests with and without race detector during the cource of _a single_ test pipeline. – kostix Oct 13 '20 at 12:12
  • `go test` understands not only the `-c` command-line option you seem to be using, but also `-o`; would it suffice if you will just compile the binary twice—with and without the race detector enabled—each time producing an executable image with a different name? This will still require tweaking the pipeline but calling an image with a different flag is not too different with calling another image. – kostix Oct 13 '20 at 12:15
  • 1
    Wow... if the race detector has that much of an impact, it sounds like some more fundamental changes need to be made... – Jonathan Hall Oct 13 '20 at 12:23
  • 3
    " I can't really fathom a case where I would need to run the same set of tests with and without race detector" -- I can't either. I'd want to run them all only with the race detector. – Jonathan Hall Oct 13 '20 at 12:24
  • @Flimzy I appreciate asking the rationale, but the reason is convoluted. I could give a lot of context, but than I'd get minuses for not asking for a straight question. I asked if there is a mechanism, apparently there is none. – hbogert Oct 13 '20 at 14:13
  • @kostix "Still, I can't really fathom a case where I would need to run the same set of tests with and without race detector during the cource of a single test pipeline. " The question does not say that that the same set of tests should be run with and without race detection. – hbogert Oct 13 '20 at 14:15
  • Do you mean you're using custom flags to exercise different tests? – kostix Oct 13 '20 at 14:32
  • 1
    The context, I compile all tests in one stage of our CI, one of those tests is a large integration/bdd test. The unit tests however are well defined and if data races show up, that's something we'll have to fix immediately (duh). However, in the bdd test, 3rd party code is being checked for races as well. Unfortunately some 3rd party code has races, I do not want wait on fixes upstream before our CI passes again on the bdd test. In the future, races introduced by new 3rd party libraries will be immediately caught by CI, so basically this is techn. debt should've tested this sooner. – hbogert Oct 13 '20 at 14:42
  • 1
    Thanks. Actually I think this is an interesting question; wish I could have enough rep to protect it but oh well. – kostix Oct 13 '20 at 14:49
  • It's common to run all tests with race detection as `go test -race ...` But, sometimes you have tests that explicitly verify correctness in race cases. Those will be flagged as "bad" by this detection. There's no good way to say "run all tests except these three" in the `go test ...` formulation. So, if a test could sense whether race detection is baked in, that would be useful. – Jon Watte May 24 '21 at 21:13
  • See https://stackoverflow.com/questions/34936571/can-i-skip-a-specific-test-when-using-the-race-detector for a possible solution – Jeff Learman Jun 10 '22 at 13:55

2 Answers2

4

Is there a way to disable the data race checking in a compiled test binary?

No.

The race detector either is compiled in or not compiled in.

Volker
  • 40,468
  • 7
  • 81
  • 87
1

If the reason is that you want to not fail on the race detector, then use

$ GORACE="exitcode=0" [ go ... | ./compiled-test-bin ]
hbogert
  • 4,198
  • 5
  • 24
  • 38