-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves server implementation to separate package and cover server logi…
…c with tests
- Loading branch information
Showing
6 changed files
with
286 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ packages: | |
RequestHandler: | ||
Connection: | ||
Request: | ||
Channel: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"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) | ||
|
||
if server.port != port { | ||
t.Errorf("Expected port %d, but got %d", port, server.port) | ||
} | ||
|
||
if len(server.channels) != 0 { | ||
t.Errorf("Expected empty channels slice, but got %d channels", len(server.channels)) | ||
} | ||
|
||
if server.mutex == nil { | ||
t.Error("Expected non-nil mutex") | ||
} | ||
} | ||
func TestServer_AddChannel(t *testing.T) { | ||
// Create a new Server instance | ||
server := NewServer(0) | ||
|
||
// Create a new channel | ||
channel := mocks.NewMockChannel(t) | ||
channel.EXPECT().Path().Return("test") | ||
|
||
// Add the channel to the server | ||
server.AddChannel(channel) | ||
|
||
// Check if the channel was added correctly | ||
if len(server.channels) != 1 { | ||
t.Errorf("Expected 1 channel, but got %d channels", len(server.channels)) | ||
} | ||
|
||
if server.channels[0].Path() != "test" { | ||
t.Errorf("Expected channel name 'test', but got '%s'", server.channels[0].Path()) | ||
} | ||
} |