-
Notifications
You must be signed in to change notification settings - Fork 17
[flaky] Fix flaky tests caused by port binding #149
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ package deliver | |
|
|
||
| import ( | ||
| "context" | ||
| "net" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
@@ -50,7 +51,7 @@ func TestBroadcastDeliver(t *testing.T) { | |
| require.NoError(t, err) | ||
| t.Cleanup(client.CloseConnections) | ||
|
|
||
| ctx, cancel := context.WithTimeout(t.Context(), time.Minute) | ||
| ctx, cancel := context.WithTimeout(t.Context(), 5*time.Minute) | ||
| t.Cleanup(cancel) | ||
|
|
||
| outputBlocksChan := make(chan *common.Block, 100) | ||
|
|
@@ -77,20 +78,21 @@ func TestBroadcastDeliver(t *testing.T) { | |
|
|
||
| t.Log("One server down") | ||
| servers[2].Servers[0].Stop() | ||
| waitUntilGrpcServerIsDown(ctx, t, &servers[2].Configs[0].Endpoint) | ||
| listener1 := holdPort(ctx, t, servers[2].Configs[0]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Earlier, we only checked whether the server was down or not. Now, we create another fake listener and explicitly close it. Can you please explain the reason as I might be missing the core issue?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the following comment to // holdPort attempts to bind to the specified server port and holds it until the listener is closed.
// It serves two purposes:
// 1. A successful bind indicates the port is free, meaning the server previously using it is down.
// 2. It prevents other tests from binding to the same port, ensuring this test correctly detects the server as
// unavailable. |
||
| submit(t, &conf, outputBlocks, expectedSubmit{ | ||
| success: 3, | ||
| }) | ||
|
|
||
| t.Log("Two servers down") | ||
| servers[2].Servers[1].Stop() | ||
| waitUntilGrpcServerIsDown(ctx, t, &servers[2].Configs[1].Endpoint) | ||
| listener2 := holdPort(ctx, t, servers[2].Configs[1]) | ||
| submit(t, &conf, outputBlocks, expectedSubmit{ | ||
| success: 2, | ||
| unavailable: 1, | ||
| }) | ||
|
|
||
| t.Log("One incorrect server") | ||
| _ = listener1.Close() | ||
| fakeServer := test.RunGrpcServerForTest(ctx, t, servers[2].Configs[0], nil) | ||
| waitUntilGrpcServerIsReady(ctx, t, &servers[2].Configs[0].Endpoint) | ||
| submit(t, &conf, outputBlocks, expectedSubmit{ | ||
|
|
@@ -100,22 +102,22 @@ func TestBroadcastDeliver(t *testing.T) { | |
|
|
||
| t.Log("All good again") | ||
| fakeServer.Stop() | ||
| waitUntilGrpcServerIsDown(ctx, t, &servers[2].Configs[0].Endpoint) | ||
| servers[2].Servers[0] = test.RunGrpcServerForTest(ctx, t, servers[2].Configs[0], ordererService.RegisterService) | ||
| waitUntilGrpcServerIsReady(ctx, t, &servers[2].Configs[0].Endpoint) | ||
| submit(t, &conf, outputBlocks, expectedSubmit{ | ||
| success: 3, | ||
| }) | ||
|
|
||
| t.Log("Insufficient quorum") | ||
| _ = listener2.Close() | ||
| for _, gs := range servers[:2] { | ||
| for _, s := range gs.Servers[:2] { | ||
| s.Stop() | ||
| } | ||
| } | ||
| for _, gs := range servers[:2] { | ||
| for _, c := range gs.Configs[:2] { | ||
| waitUntilGrpcServerIsDown(ctx, t, &c.Endpoint) | ||
| holdPort(ctx, t, c) | ||
| } | ||
| } | ||
| submit(t, &conf, outputBlocks, expectedSubmit{ | ||
|
|
@@ -231,11 +233,17 @@ func waitUntilGrpcServerIsReady(ctx context.Context, t *testing.T, endpoint *con | |
| t.Logf("%v is ready", endpoint) | ||
| } | ||
|
|
||
| func waitUntilGrpcServerIsDown(ctx context.Context, t *testing.T, endpoint *connection.Endpoint) { | ||
| // holdPort attempts to bind to the specified server port and holds it until the listener is closed. | ||
| // It serves two purposes: | ||
| // 1. A successful bind indicates the port is free, meaning the server previously using it is down. | ||
| // 2. It prevents other tests from binding to the same port, ensuring this test correctly detects the server as | ||
| // unavailable. | ||
| func holdPort(ctx context.Context, t *testing.T, c *connection.ServerConfig) net.Listener { | ||
| t.Helper() | ||
| newConn, err := connection.Connect(test.NewInsecureDialConfig(endpoint)) | ||
| listener, err := c.Listener(ctx) | ||
| require.NoError(t, err) | ||
| defer connection.CloseConnectionsLog(newConn) | ||
| test.WaitUntilGrpcServerIsDown(ctx, t, newConn) | ||
| t.Logf("%v is down", endpoint) | ||
| t.Cleanup(func() { | ||
| _ = listener.Close() | ||
| }) | ||
| return listener | ||
| } | ||
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.
Do we plan to pass the ctx as an argument to this PreallocateListener() in the future?
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.
No. PreallocateListener should only be used with
port=0. If such attempt fails, there is no reason to retry.