-
Notifications
You must be signed in to change notification settings - Fork 348
feat: quick return tool-call request, send response via SSE in goroutine #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c886adb
3b13f79
8563a03
5bd00ad
73d36a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,13 +59,10 @@ func TestSSEServer(t *testing.T) { | |
defer sseResp.Body.Close() | ||
|
||
// Read the endpoint event | ||
buf := make([]byte, 1024) | ||
n, err := sseResp.Body.Read(buf) | ||
endpointEvent, err := readSeeEvent(sseResp) | ||
if err != nil { | ||
t.Fatalf("Failed to read SSE response: %v", err) | ||
} | ||
|
||
endpointEvent := string(buf[:n]) | ||
if !strings.Contains(endpointEvent, "event: endpoint") { | ||
t.Fatalf("Expected endpoint event, got: %s", endpointEvent) | ||
} | ||
|
@@ -107,19 +104,6 @@ func TestSSEServer(t *testing.T) { | |
if resp.StatusCode != http.StatusAccepted { | ||
t.Errorf("Expected status 202, got %d", resp.StatusCode) | ||
} | ||
|
||
// Verify response | ||
var response map[string]interface{} | ||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { | ||
t.Fatalf("Failed to decode response: %v", err) | ||
} | ||
|
||
if response["jsonrpc"] != "2.0" { | ||
t.Errorf("Expected jsonrpc 2.0, got %v", response["jsonrpc"]) | ||
} | ||
if response["id"].(float64) != 1 { | ||
t.Errorf("Expected id 1, got %v", response["id"]) | ||
} | ||
}) | ||
|
||
t.Run("Can handle multiple sessions", func(t *testing.T) { | ||
|
@@ -208,8 +192,17 @@ func TestSSEServer(t *testing.T) { | |
} | ||
defer resp.Body.Close() | ||
|
||
endpointEvent, err = readSeeEvent(sseResp) | ||
if err != nil { | ||
t.Fatalf("Failed to read SSE response: %v", err) | ||
} | ||
respFromSee := strings.TrimSpace( | ||
strings.Split(strings.Split(endpointEvent, "data: ")[1], "\n")[0], | ||
) | ||
|
||
fmt.Printf("========> %v", respFromSee) | ||
var response map[string]interface{} | ||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { | ||
if err := json.NewDecoder(strings.NewReader(respFromSee)).Decode(&response); err != nil { | ||
Comment on lines
+195
to
+205
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix: Testing function called from goroutine. The static analysis tool flagged that - t.Fatalf("Failed to read SSE response: %v", err)
+ t.Errorf("Session %d: Failed to read SSE response: %v", sessionNum, err)
+ return
🧰 Tools🪛 golangci-lint (1.64.8)197-197: testinggoroutine: call to (*testing.T).Fatalf from a non-test goroutine (govet) |
||
t.Errorf( | ||
"Session %d: Failed to decode response: %v", | ||
sessionNum, | ||
|
@@ -586,13 +579,10 @@ func TestSSEServer(t *testing.T) { | |
defer sseResp.Body.Close() | ||
|
||
// Read the endpoint event | ||
buf := make([]byte, 1024) | ||
n, err := sseResp.Body.Read(buf) | ||
endpointEvent, err := readSeeEvent(sseResp) | ||
if err != nil { | ||
t.Fatalf("Failed to read SSE response: %v", err) | ||
} | ||
|
||
endpointEvent := string(buf[:n]) | ||
messageURL := strings.TrimSpace( | ||
strings.Split(strings.Split(endpointEvent, "data: ")[1], "\n")[0], | ||
) | ||
|
@@ -632,8 +622,16 @@ func TestSSEServer(t *testing.T) { | |
} | ||
|
||
// Verify response | ||
endpointEvent, err = readSeeEvent(sseResp) | ||
if err != nil { | ||
t.Fatalf("Failed to read SSE response: %v", err) | ||
} | ||
respFromSee := strings.TrimSpace( | ||
strings.Split(strings.Split(endpointEvent, "data: ")[1], "\n")[0], | ||
) | ||
|
||
var response map[string]interface{} | ||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { | ||
if err := json.NewDecoder(strings.NewReader(respFromSee)).Decode(&response); err != nil { | ||
t.Fatalf("Failed to decode response: %v", err) | ||
} | ||
|
||
|
@@ -671,8 +669,17 @@ func TestSSEServer(t *testing.T) { | |
} | ||
defer resp.Body.Close() | ||
|
||
endpointEvent, err = readSeeEvent(sseResp) | ||
if err != nil { | ||
t.Fatalf("Failed to read SSE response: %v", err) | ||
} | ||
|
||
respFromSee = strings.TrimSpace( | ||
strings.Split(strings.Split(endpointEvent, "data: ")[1], "\n")[0], | ||
) | ||
|
||
response = make(map[string]interface{}) | ||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { | ||
if err := json.NewDecoder(strings.NewReader(respFromSee)).Decode(&response); err != nil { | ||
t.Fatalf("Failed to decode response: %v", err) | ||
} | ||
|
||
|
@@ -740,3 +747,12 @@ func TestSSEServer(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
func readSeeEvent(sseResp *http.Response) (string, error) { | ||
buf := make([]byte, 1024) | ||
n, err := sseResp.Body.Read(buf) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(buf[:n]), nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix test code to accommodate asynchronous response delivery.
The test code has been properly adapted to read and process responses from SSE events instead of directly from HTTP response bodies, which aligns with the server-side changes. However, there's a critical issue with using
t.Fatalf
in a goroutine.Using
t.Fatalf
from a non-test goroutine (line 197) can cause race conditions or panics in the test framework. This was previously flagged in code reviews but hasn't been fixed.Apply this fix to all instances of
t.Fatalf
used in goroutines in this file.Also applies to: 195-204, 582-632, 672-682