Skip to content

Commit

Permalink
Updates server tests and example
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Apr 25, 2024
1 parent d2e01e9 commit d201c64
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
6 changes: 3 additions & 3 deletions examples/http_backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

const (
Port = 8080
Addr = ":8080"
)

func main() {
Expand All @@ -42,10 +42,10 @@ func main() {

channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

server := server.NewServer(Port)
server := server.NewServer(Addr)
server.AddChannel(channel)

if err := server.Run(context.Background()); err != nil {
if err := server.Run(); err != nil {
slog.Error("Fail to start app server", "error", err)
os.Exit(1)
}
Expand Down
80 changes: 50 additions & 30 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,19 @@ package server

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

"github.com/ksysoev/wasabi/mocks"
)

func TestServer_Shutdown_with_context(t *testing.T) {
// Create a new Server instance
server := NewServer(0)

// Create a new context
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)

doneChan := make(chan struct{})
// Start the server in a separate goroutine
go func() {
_ = server.Run(ctx)

close(doneChan)
}()

cancel()

select {
case <-doneChan:
case <-time.After(1 * time.Second):
t.Error("Server did not stop")
}
}

func TestNewServer(t *testing.T) {
port := uint16(8080)
server := NewServer(port)
addr := ":8080"
server := NewServer(addr)

if server.port != port {
t.Errorf("Expected port %d, but got %d", port, server.port)
if server.addr != addr {
t.Errorf("Expected port %s, but got %s", addr, server.addr)
}

if len(server.channels) != 0 {
Expand All @@ -51,7 +27,7 @@ func TestNewServer(t *testing.T) {
}
func TestServer_AddChannel(t *testing.T) {
// Create a new Server instance
server := NewServer(0)
server := NewServer(":0")

// Create a new channel
channel := mocks.NewMockChannel(t)
Expand All @@ -69,3 +45,47 @@ func TestServer_AddChannel(t *testing.T) {
t.Errorf("Expected channel name 'test', but got '%s'", server.channels[0].Path())
}
}

func TestServer_WithBaseContext(t *testing.T) {
// Create a new Server instance with a base context
//lint:ignore SA1029 This is a test
ctx := context.WithValue(context.Background(), "test", "test")

Check warning on line 52 in server/server_test.go

View workflow job for this annotation

GitHub Actions / tests

context-keys-type: should not use basic type string as key in context.WithValue (revive)

server := NewServer(":0", WithBaseContext(ctx))

// Check if the base context was set correctly
if server.baseCtx == nil {
t.Error("Expected non-nil base context")
}

if server.baseCtx.Value("test") != "test" {
t.Errorf("Expected context value 'test', but got '%s'", server.baseCtx.Value("test"))
}
}

func TestServer_Run(t *testing.T) {
// Create a new Server instance
server := NewServer(":0")

// Run the server
done := make(chan struct{})
go func() {
err := server.Run()
if err != http.ErrServerClosed {
t.Errorf("Expected error %v, but got %v", http.ErrServerClosed, err)
}
close(done)
}()

<-time.After(50 * time.Millisecond)
err := server.http.Close()
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}

select {
case <-done:
case <-time.After(1 * time.Second):
t.Error("Expected server to stop")
}
}

0 comments on commit d201c64

Please sign in to comment.