mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-22 17:51:28 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user