Skip to content

Commit 2b762e1

Browse files
Add test comments
1 parent e9c1312 commit 2b762e1

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

protocol_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import (
88
"time"
99
)
1010

11+
// TestProtocol_Connect verifies the basic connection functionality of the Protocol.
12+
// This is a critical test as connection establishment is required for all other operations.
13+
// It ensures that:
14+
// 1. The protocol can successfully connect to a transport
15+
// 2. The message handler is properly registered with the transport
16+
// 3. The protocol is ready to send and receive messages after connection
1117
func TestProtocol_Connect(t *testing.T) {
1218
p := NewProtocol(nil)
1319
transport := newMockTransport()
@@ -21,6 +27,13 @@ func TestProtocol_Connect(t *testing.T) {
2127
}
2228
}
2329

30+
// TestProtocol_Close tests the proper cleanup of resources when closing the protocol.
31+
// Proper cleanup is essential to prevent resource leaks and ensure graceful shutdown.
32+
// It verifies:
33+
// 1. All handlers are properly deregistered
34+
// 2. The transport is closed
35+
// 3. No messages can be sent after closing
36+
// 4. Multiple closes are handled safely
2437
func TestProtocol_Close(t *testing.T) {
2538
p := NewProtocol(nil)
2639
transport := newMockTransport()
@@ -47,6 +60,14 @@ func TestProtocol_Close(t *testing.T) {
4760
}
4861
}
4962

63+
// TestProtocol_Request tests the core request-response functionality of the protocol.
64+
// This is the most important test as it covers the primary use case of the protocol.
65+
// It includes subtests for:
66+
// 1. Successful request/response with proper correlation
67+
// 2. Request timeout handling
68+
// 3. Request cancellation via context
69+
// These scenarios ensure the protocol can handle both successful and error cases
70+
// while maintaining proper message correlation and resource cleanup.
5071
func TestProtocol_Request(t *testing.T) {
5172
p := NewProtocol(nil)
5273
transport := newMockTransport()
@@ -121,6 +142,12 @@ func TestProtocol_Request(t *testing.T) {
121142
})
122143
}
123144

145+
// TestProtocol_Notification tests the handling of one-way notifications.
146+
// Notifications are important for events that don't require responses.
147+
// The test verifies:
148+
// 1. Notifications can be sent successfully
149+
// 2. The transport receives the correct notification format
150+
// 3. No response handling is attempted for notifications
124151
func TestProtocol_Notification(t *testing.T) {
125152
p := NewProtocol(nil)
126153
transport := newMockTransport()
@@ -150,6 +177,13 @@ func TestProtocol_Notification(t *testing.T) {
150177
}
151178
}
152179

180+
// TestProtocol_RequestHandler tests the registration and invocation of request handlers.
181+
// Request handlers are crucial for servers implementing RPC methods.
182+
// It ensures:
183+
// 1. Handlers can be registered for specific methods
184+
// 2. Handlers receive the correct request parameters
185+
// 3. Handler responses are properly sent back to clients
186+
// 4. Handler errors are properly propagated
153187
func TestProtocol_RequestHandler(t *testing.T) {
154188
p := NewProtocol(nil)
155189
transport := newMockTransport()
@@ -195,6 +229,13 @@ func TestProtocol_RequestHandler(t *testing.T) {
195229
}
196230
}
197231

232+
// TestProtocol_NotificationHandler tests the handling of incoming notifications.
233+
// This is important for asynchronous events and status updates.
234+
// It verifies:
235+
// 1. Notification handlers can be registered
236+
// 2. Handlers are called with correct notification data
237+
// 3. Multiple handlers can be registered for different methods
238+
// 4. Unknown notifications are handled gracefully
198239
func TestProtocol_NotificationHandler(t *testing.T) {
199240
p := NewProtocol(nil)
200241
transport := newMockTransport()
@@ -224,6 +265,13 @@ func TestProtocol_NotificationHandler(t *testing.T) {
224265
}
225266
}
226267

268+
// TestProtocol_Progress tests the progress tracking functionality.
269+
// Progress tracking is essential for long-running operations.
270+
// The test covers:
271+
// 1. Progress notifications can be sent and received
272+
// 2. Progress tokens are properly correlated with requests
273+
// 3. Progress callbacks are invoked with correct values
274+
// 4. Progress handling works alongside normal request processing
227275
func TestProtocol_Progress(t *testing.T) {
228276
p := NewProtocol(nil)
229277
transport := newMockTransport()
@@ -295,6 +343,13 @@ func TestProtocol_Progress(t *testing.T) {
295343
}
296344
}
297345

346+
// TestProtocol_ErrorHandling tests various error conditions in the protocol.
347+
// Proper error handling is crucial for reliability and debugging.
348+
// It verifies:
349+
// 1. Transport errors are properly propagated
350+
// 2. Protocol-level errors are handled correctly
351+
// 3. Error responses include appropriate error codes and messages
352+
// 4. Resources are cleaned up after errors
298353
func TestProtocol_ErrorHandling(t *testing.T) {
299354
p := NewProtocol(nil)
300355
transport := newMockTransport()

stdio_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import (
66
"testing"
77
)
88

9+
// TestReadBuffer tests the buffering functionality for JSON-RPC messages.
10+
// The ReadBuffer is crucial for handling streaming input and properly framing messages.
11+
// It verifies:
12+
// 1. Empty buffer handling returns nil message
13+
// 2. Incomplete messages are properly buffered
14+
// 3. Complete messages are correctly extracted
15+
// 4. Multiple message fragments are handled correctly
16+
// 5. Buffer clearing works as expected
17+
// This is a critical test as message framing is fundamental to the protocol.
918
func TestReadBuffer(t *testing.T) {
1019
rb := NewReadBuffer()
1120

@@ -50,6 +59,14 @@ func TestReadBuffer(t *testing.T) {
5059
}
5160
}
5261

62+
// TestStdioTransport tests the stdio-based transport implementation.
63+
// The stdio transport is the primary means of communication for the protocol.
64+
// It ensures:
65+
// 1. Messages can be sent correctly with proper framing
66+
// 2. Transport properly handles closing
67+
// 3. Sending after close returns appropriate error
68+
// 4. Output includes required newline termination
69+
// This test is essential as the transport layer must be reliable for the protocol to function.
5370
func TestStdioTransport(t *testing.T) {
5471
// Create a transport with a buffer instead of actual stdin/stdout
5572
var input bytes.Buffer
@@ -88,6 +105,14 @@ func TestStdioTransport(t *testing.T) {
88105
}
89106
}
90107

108+
// TestMessageDeserialization tests the parsing of different JSON-RPC message types.
109+
// Proper message type detection and parsing is critical for protocol operation.
110+
// It tests:
111+
// 1. Request messages (with method and ID)
112+
// 2. Notification messages (with method, no ID)
113+
// 3. Error responses (with error object)
114+
// 4. Success responses (with result)
115+
// Each message type must be correctly identified and parsed to maintain protocol integrity.
91116
func TestMessageDeserialization(t *testing.T) {
92117
tests := []struct {
93118
name string

0 commit comments

Comments
 (0)