Skip to content

Commit

Permalink
(fixup 6 posyt review) updated server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag4DSB committed Nov 21, 2024
1 parent d9577e1 commit 9b077b9
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions pkg/grpcfactory/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -75,18 +70,18 @@ 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)
Expect(err).NotTo(HaveOccurred())
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)
Expand All @@ -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)
})
})
})

0 comments on commit 9b077b9

Please sign in to comment.