From 9b077b9f61181688087d75e35669e5e43818bf8c Mon Sep 17 00:00:00 2001 From: Anurag Mittal Date: Thu, 21 Nov 2024 12:36:58 +0100 Subject: [PATCH] (fixup 6 posyt review) updated server tests --- pkg/grpcfactory/server_test.go | 53 ++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/pkg/grpcfactory/server_test.go b/pkg/grpcfactory/server_test.go index 9f5d392..89d8a89 100644 --- a/pkg/grpcfactory/server_test.go +++ b/pkg/grpcfactory/server_test.go @@ -29,8 +29,6 @@ func generateUniqueAddress() string { var _ = Describe("gRPC Factory Server", func() { var ( - ctx context.Context - cancel context.CancelFunc address string identityServer cosi.IdentityServer provisionerServer cosi.ProvisionerServer @@ -41,19 +39,16 @@ var _ = Describe("gRPC Factory Server", func() { // Generate a unique socket address for this test run address = generateUniqueAddress() - ctx, cancel = context.WithCancel(context.Background()) identityServer = &mockIdentityServer{} provisionerServer = &mockProvisionerServer{} }) AfterEach(func() { - cancel() - // Clean up the socket file after each test os.Remove(strings.TrimPrefix(address, "unix://")) }) Describe("Run", func() { - It("should start the server and return no error", func() { + It("should start the server and return no error", func(ctx SpecContext) { var err error server, err = grpcfactory.NewCOSIProvisionerServer(address, identityServer, provisionerServer, nil) Expect(err).NotTo(HaveOccurred()) @@ -75,10 +70,10 @@ var _ = Describe("gRPC Factory Server", func() { } }) - It("should return an error when reusing the same address", func() { + It("should return an error when reusing the same address", func(ctx SpecContext) { // Use a fixed address to simulate reuse address := "unix:///tmp/test.sock" - socketPath := address[7:] // Strip the `unix://` prefix + socketPath := strings.TrimPrefix(address, "unix://") // Start a stub listener on the address to occupy it listener, err := net.Listen("unix", socketPath) @@ -86,7 +81,7 @@ var _ = Describe("gRPC Factory Server", func() { defer listener.Close() // Try to start the gRPC server on the same address - server2Ctx, server2Cancel := context.WithCancel(context.Background()) + server2Ctx, server2Cancel := context.WithCancel(ctx) // Pass SpecContext here defer server2Cancel() server2, err := grpcfactory.NewCOSIProvisionerServer(address, identityServer, provisionerServer, nil) @@ -111,24 +106,40 @@ var _ = Describe("gRPC Factory Server", func() { os.Remove(socketPath) }) - It("should handle unexpected shutdowns by canceling the context", func() { - server, err := grpcfactory.NewCOSIProvisionerServer(address, identityServer, provisionerServer, nil) + It("should return an error when reusing the same address", func() { + // Use a fixed address to simulate reuse + address := "unix:///tmp/test.sock" + socketPath := strings.TrimPrefix(address, "unix://") + + // Start a stub listener on the address to occupy it + listener, err := net.Listen("unix", socketPath) Expect(err).NotTo(HaveOccurred()) - Expect(server).NotTo(BeNil()) + defer listener.Close() - errChan := make(chan error, 1) + // Try to start the gRPC server on the same address + server2Ctx, server2Cancel := context.WithCancel(context.Background()) + defer server2Cancel() + + server2, err := grpcfactory.NewCOSIProvisionerServer(address, identityServer, provisionerServer, nil) + Expect(err).NotTo(HaveOccurred()) + Expect(server2).NotTo(BeNil()) + + errChan2 := make(chan error, 1) go func() { - errChan <- server.Run(ctx) + errChan2 <- server2.Run(server2Ctx) }() - // server startup time - time.Sleep(100 * time.Millisecond) - - // Cancel the context to simulate an abrupt shutdown - cancel() + // Expect the second server to fail immediately due to address reuse + select { + case err := <-errChan2: + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("address already in use")) + case <-time.After(1 * time.Second): + Fail("Expected an 'address already in use' error, but none was received") + } - // Expect the Run function to exit cleanly or with context.Canceled error - Eventually(errChan, 1*time.Second).Should(Receive(MatchError(context.Canceled))) + // Clean up the socket file for future tests + os.Remove(socketPath) }) }) })