-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix failing ServerInUseHostPort test on MacOS #2477
Fix failing ServerInUseHostPort test on MacOS #2477
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2477 +/- ##
==========================================
- Coverage 95.52% 95.18% -0.35%
==========================================
Files 208 208
Lines 10759 9158 -1601
==========================================
- Hits 10278 8717 -1561
+ Misses 405 365 -40
Partials 76 76
Continue to review full report at Codecov.
|
cc @rjs211 |
cmd/query/app/server_test.go
Outdated
@@ -26,6 +27,7 @@ import ( | |||
"github.com/stretchr/testify/mock" | |||
"go.uber.org/zap" | |||
"go.uber.org/zap/zaptest/observer" | |||
"golang.org/x/sys/unix" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it wise to introduce OS dependency without a build tag? How critical is it that we check for specific OS error code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair question, it's not critical to check the specific OS error code. However, given we are trying to trigger an error that stems from a fairly deep call stack and originating from outside this project, I was also conscious there could be a potentially larger pool of error types returned and so wanted to be as specific as possible to avoid false positives.
Perhaps we can continue to perform type assertions (and assert their success) on the error interfaces until we reach the most specific error, but remove the check on the specific error requiring the unix dependency; so something like the following instead?:
err = server.Start()
assert.Error(t, err)
_, ok := err.(*net.OpError).Err.(*os.SyscallError)
assert.True(t, ok)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concern is that it just makes the test dependent on some implementation details of the OS, which can change over time, or Go may change its implementation.
Stepping back to what the test is even about - why would we care about port reuse? I think the objective of the test was to check the code path when listening to a port causes an error. It doesn't matter what the error is, just that our code handles it. So the test may do whatever it needs to introduce the error condition. For example, is there any difference in our code behavior (different path) if the port is already take or is completely invalid?
cmd/query/app/server_test.go
Outdated
@@ -74,7 +76,7 @@ func TestServerBadHostPort(t *testing.T) { | |||
|
|||
func TestServerInUseHostPort(t *testing.T) { | |||
|
|||
for _, hostPort := range [2]string{":8080", ":8081"} { | |||
for _, hostPort := range [2]string{"127.0.0.1:8080", "127.0.0.1:8081"} { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a cleaner way to write tests is to listen on port 0 and let the OS assign the unused port. Can we do it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the goal of this test is to intentionally have two listeners listening on the same host:port to cause a "Bind address already in use" error, and so there's a need for the port numbers to be deterministic rather than dynamically allocated by the OS.
Please let me know if I've misunderstood your question.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't read the test carefully, but from it's name it's trying to test for port conflict, which does not require listening on a specific port, it just requires trying to bind twice to the same port. The first time it can bind to a random port and use it for the second attempt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's neat! I didn't think it was possible to retrieve the OS allocated port. Thanks!
8669b26
to
7aa9daa
Compare
@yurishkuro are there any outstanding concerns that need addressing in this PR? |
Signed-off-by: albertteoh <[email protected]>
7aa9daa
to
b1d0e13
Compare
cmd/query/app/server_test.go
Outdated
httpHostPort string | ||
grpcHostPort string | ||
}{ | ||
{"HTTP host port clash on " + conn.Addr().String(), conn.Addr().String(), availableHostPort}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"HTTP host port clash on " + conn.Addr().String()
- I think it's better if the test names are stable, this one will include random port number on every run, which is not informative anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, aside from minor formatting
Signed-off-by: albertteoh <[email protected]>
Signed-off-by: albertteoh <[email protected]>
ff5e0fe
to
32e5ab6
Compare
Co-authored-by: Yuri Shkuro <[email protected]> Signed-off-by: albertteoh <[email protected]>
4af8db0
to
62a4971
Compare
Signed-off-by: albertteoh <[email protected]>
Signed-off-by: albertteoh [email protected]
Which problem is this PR solving?
TestServerInUseHostPort
, seemingly only for MacOS (okay for Linux builds).Start()
; and expecting an error to be returned.127.0.0.1:8080
while:8080
is bound, causing the assertion to fail. Interestingly, this behaviour differs between MacOS and Linux (at least on my Centos 8 VM).0.0.0.0
address to be bindable to the loopback network interface....and here are the results:
MacOS
Centos 8
Short description of the changes