Skip to content

Commit

Permalink
Adds tests for closing channel and connection registry
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Apr 27, 2024
1 parent f6de6c1 commit d11d48d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
14 changes: 14 additions & 0 deletions channel/channel_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package channel

import (
"context"
"net/http"
"testing"

Expand Down Expand Up @@ -132,3 +133,16 @@ func TestChannel_WithOriginPatterns(t *testing.T) {
t.Errorf("Unexpected to get default origin pattern: got %s, expected %s", channel.config.originPatterns[1], "test2")
}
}
func TestChannel_Shutdown(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher, NewConnectionRegistry())

// Call the Shutdown method
err := channel.Shutdown(context.Background())

if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
2 changes: 1 addition & 1 deletion channel/connection_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func (r *ConnectionRegistry) Shutdown(ctx context.Context) error {
wg.Add(1)

go func() {
defer wg.Done()
c.Close(ctx, websocket.StatusServiceRestart, "")
wg.Done()
}()
}

Expand Down
29 changes: 29 additions & 0 deletions channel/connection_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,32 @@ func TestConnectionRegistry_WithMaxFrameLimit(t *testing.T) {
t.Errorf("Unexpected frame size limit: got %d, expected %d", registry.frameSizeLimit, 100)
}
}
func TestConnectionRegistry_Shutdown(t *testing.T) {
ctx := context.Background()
registry := NewConnectionRegistry()

// Add some mock connections to the registry
conn1 := mocks.NewMockConnection(t)
conn2 := mocks.NewMockConnection(t)

conn1.EXPECT().ID().Return("conn1")
conn2.EXPECT().ID().Return("conn2")

registry.connections[conn1.ID()] = conn1
registry.connections[conn2.ID()] = conn2

// Set up expectations for the Close method
conn1.EXPECT().Close(ctx, websocket.StatusServiceRestart, "").Return(nil)
conn2.EXPECT().Close(ctx, websocket.StatusServiceRestart, "").Return(nil)

err := registry.Shutdown(ctx)

if err != nil {
t.Errorf("Unexpected error: %v", err)
}

// Verify that the registry is closed
if !registry.isClosed {
t.Error("Expected registry to be closed")
}
}

0 comments on commit d11d48d

Please sign in to comment.