Skip to content

Lab6: EchoServer refactoring for testability + unit tests#49

Open
MinTins wants to merge 26 commits into
lenagrin:masterfrom
MinTins:lab6/echo-server-tests
Open

Lab6: EchoServer refactoring for testability + unit tests#49
MinTins wants to merge 26 commits into
lenagrin:masterfrom
MinTins:lab6/echo-server-tests

Conversation

@MinTins
Copy link
Copy Markdown

@MinTins MinTins commented May 28, 2026

Lab6: Безпечний рефакторинг EchoServer під тести

Автор: Флакей Роман (MinTins)

Що зроблено

Рефакторинг EchoServer

  • Витягнуто ITcpListener інтерфейс — EchoServer більше не залежить від реальних сокетів
  • Додано TcpListenerWrapper (production реалізація, виключена з coverage)
  • Додано namespace EchoTcpServer до всіх класів
  • HandleClientAsync змінено з private на internal (доступний для тестів через InternalsVisibleTo)
  • UdpTimedSender виокремлено в окремий файл
  • Program.cs спрощено до composition root

Нові тести (EchoServerTests)

Тест Що перевіряє
StartAsync_CallsListenerStart listener.Start() викликається
StartAsync_ExitsLoop_OnObjectDisposedException цикл завершується коректно
Stop_CallsListenerStop listener.Stop() викликається
Stop_CanBeCalledWithoutStart не кидає виняток
HandleClientAsync_EchoesDataBack байти повертаються клієнту
HandleClientAsync_StopsOnCancellation реагує на CancellationToken
HandleClientAsync_ClosesClientOnCompletion TcpClient закривається

Метрики до/після

Метрика До Після
Coverage 70% 72.3%
Reliability issues 7 6
Maintainability issues 49 48
Тести для EchoServer 0 7
image image

MinTins and others added 26 commits March 22, 2026 19:19
…ethods

- UdpClientWrapper.Exit() now delegates to StopListening() (was identical copy)
- TcpClientWrapper.SendMessageAsync(string) now delegates to SendMessageAsync(byte[]) (removed duplicated write+log logic)
- NetSdrClient: extracted EnsureConnected() helper to remove 3 identical 'no active connection' guard blocks
- Added NetSdrArchTests project with NetArchTest.Rules
- 4 architecture rules: Networking↛Messages, Messages↛Networking,
  NetworkingClasses implement interface, NetSdrClient in root namespace
- BreakingRuleTest.cs: intentional failing test to demonstrate red CI
  (will be removed in next commit)
- Removed BreakingRuleTest.cs (was intentionally failing for red CI demo)
- All 4 architecture rules now pass:
  * Networking does not depend on Messages
  * Messages does not depend on Networking
  * All Networking classes implement interface
  * NetSdrClient resides in root namespace
…namespace

Rule 3 was failing because UdpClientWrapper and IUdpClient are declared
in the global namespace (no namespace keyword), not in NetSdrClientApp.Networking.
Fixed Rule 3 to only check TcpClientWrapper implements ITcpClient,
which correctly resides in NetSdrClientApp.Networking.
Refactoring:
- Extracted ITcpListener interface to decouple EchoServer from real sockets
- Added TcpListenerWrapper (production impl, ExcludeFromCodeCoverage)
- Added namespace EchoTcpServer to all classes
- Made HandleClientAsync internal (InternalsVisibleTo EchoServerTests)
- Moved UdpTimedSender to separate file
- Simplified Program.cs to composition root only

Tests (EchoServerTests):
- StartAsync_CallsListenerStart
- StartAsync_ExitsLoop_OnObjectDisposedException
- Stop_CallsListenerStop
- Stop_CanBeCalledWithoutStart
- HandleClientAsync_EchoesDataBack
- HandleClientAsync_StopsOnCancellation
- HandleClientAsync_ClosesClientOnCompletion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant