DRA: Fix socket binding error in unit tests

Fixes TestConnectionHandling/no-wipe-on-reconnect which was failing with
"Only one usage of each socket address is normally permitted" because the
teardown function wasn't waiting for the gRPC server to fully shut down
before returning, causing socket cleanup issues in subsequent test runs.

The fix ensures proper synchronization during server shutdown so that the
Unix socket is fully released before the teardown completes. This prevents
socket binding conflicts when tests run in sequence.
This commit is contained in:
Ed Bartosh
2025-10-14 15:58:23 +03:00
parent 9b9cd768a0
commit 4c8b434779

View File

@@ -84,13 +84,10 @@ type tearDown func()
func setupFakeGRPCServer(service, addr string) (tearDown, error) {
ctx, cancel := context.WithCancel(context.Background())
teardown := func() {
cancel()
}
listener, err := net.Listen("unix", addr)
if err != nil {
teardown()
cancel()
return nil, err
}
@@ -110,20 +107,31 @@ func setupFakeGRPCServer(service, addr string) (tearDown, error) {
drapbv1beta1.RegisterDRAPluginServer(s, drapbv1beta1.V1ServerWrapper{DRAPluginServer: fakeGRPCServer})
drahealthv1alpha1.RegisterDRAResourceHealthServer(s, fakeGRPCServer)
} else {
cancel()
return nil, err
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := s.Serve(listener); err != nil && !errors.Is(err, grpc.ErrServerStopped) {
panic(err)
}
}()
go func() {
go func() {
if err := s.Serve(listener); err != nil && !errors.Is(err, grpc.ErrServerStopped) {
panic(err)
}
}()
<-ctx.Done()
s.GracefulStop()
}()
teardown := func() {
cancel()
wg.Wait() // wait for Serve() to finish
}
return teardown, nil
}