Skip to content

Commit

Permalink
COSI-72: use-specTimeout-for-grpc-server-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag4DSB committed Dec 11, 2024
1 parent 6e58010 commit 495c5ff
Showing 1 changed file with 21 additions and 85 deletions.
106 changes: 21 additions & 85 deletions pkg/grpcfactory/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grpcfactory_test

import (
"context"
"errors"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -44,7 +45,10 @@ var _ = Describe("gRPC Factory Server", Ordered, func() {
})

AfterEach(func() {
os.Remove(strings.TrimPrefix(address, "unix://"))
socketPath := strings.TrimPrefix(address, "unix://")
if err := os.Remove(socketPath); err != nil && !os.IsNotExist(err) {
fmt.Printf("Warning: failed to remove socket file %s: %v\n", socketPath, err)
}
})

Describe("Run", func() {
Expand All @@ -54,93 +58,33 @@ var _ = Describe("gRPC Factory Server", Ordered, func() {
Expect(err).NotTo(HaveOccurred())
Expect(server).NotTo(BeNil())

errChan := make(chan error, 1)
go func() {
errChan <- server.Run(ctx)
err := server.Run(ctx)
if errors.Is(err, context.Canceled) {
return // Expected when the context is canceled
}
Expect(err).NotTo(HaveOccurred())
}()

// Allow time for the server to start
time.Sleep(100 * time.Millisecond)

select {
case err := <-errChan:
Expect(err).NotTo(HaveOccurred())
default:
// No errors
}
})
}, SpecTimeout(1*time.Second))

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 := strings.TrimPrefix(address, "unix://")

// Start a stub listener on the address to occupy it
listener, err := net.Listen("unix", socketPath)
Expect(err).NotTo(HaveOccurred())
defer listener.Close()

// Try to start the gRPC server on the same address
server2Ctx, server2Cancel := context.WithCancel(ctx) // Pass SpecContext here
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() {
errChan2 <- server2.Run(server2Ctx)
}()

// 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")
}

// Clean up the socket file for future tests
os.Remove(socketPath)
})

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 := strings.TrimPrefix(address, "unix://")

// Start a stub listener on the address to occupy it
listener, err := net.Listen("unix", socketPath)
Expect(err).NotTo(HaveOccurred())
defer listener.Close()

// Try to start the gRPC server on the same address
server2Ctx, server2Cancel := context.WithCancel(ctx)
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() {
errChan2 <- server2.Run(server2Ctx)
}()

// 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")
}

// Clean up the socket file for future tests
os.Remove(socketPath)
})
// Run the second server and expect it to fail
err = server2.Run(ctx)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("address already in use"))
}, SpecTimeout(1*time.Second))

It("should return an error for unsupported address schemes", func(ctx SpecContext) {
invalidAddress := "http://invalid-scheme-address" // Address with an unsupported scheme
Expand All @@ -149,18 +93,10 @@ var _ = Describe("gRPC Factory Server", Ordered, func() {
Expect(err).NotTo(HaveOccurred()) // Ensure server creation succeeds
Expect(server).NotTo(BeNil())

errChan := make(chan error, 1)
go func() {
errChan <- server.Run(ctx)
}()

select {
case err := <-errChan:
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("unsupported scheme: expected 'unix'"))
case <-time.After(1 * time.Second):
Fail("Expected an error for unsupported scheme, but none was received")
}
})
// Wait for server.Run to return an error
err = server.Run(ctx)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("unsupported scheme: expected 'unix'"))
}, SpecTimeout(1*time.Second))
})
})

0 comments on commit 495c5ff

Please sign in to comment.