(t *testing.T)
| 240 | } |
| 241 | |
| 242 | func TestStreamableConcurrentHandling(t *testing.T) { |
| 243 | // This test checks that the streamable server and client transports can |
| 244 | // communicate. |
| 245 | type count struct { |
| 246 | Count int |
| 247 | } |
| 248 | |
| 249 | var mu sync.Mutex |
| 250 | counts := make(map[string]int) |
| 251 | |
| 252 | server := NewServer(testImpl, nil) |
| 253 | AddTool(server, &Tool{Name: "inc"}, func(ctx context.Context, req *CallToolRequest, _ any) (*CallToolResult, count, error) { |
| 254 | id := req.Session.ID() |
| 255 | mu.Lock() |
| 256 | defer mu.Unlock() |
| 257 | c := counts[id] |
| 258 | counts[id] = c + 1 |
| 259 | return nil, count{c}, nil |
| 260 | }) |
| 261 | handler := NewStreamableHTTPHandler(func(req *http.Request) *Server { return server }, nil) |
| 262 | httpServer := httptest.NewServer(mustNotPanic(t, handler)) |
| 263 | defer httpServer.Close() |
| 264 | |
| 265 | ctx := context.Background() |
| 266 | client := NewClient(testImpl, nil) |
| 267 | var wg sync.WaitGroup |
| 268 | for range 100 { |
| 269 | wg.Go(func() { |
| 270 | clientSession, err := client.Connect(ctx, &StreamableClientTransport{Endpoint: httpServer.URL}, nil) |
| 271 | if err != nil { |
| 272 | t.Errorf("Connect failed: %v", err) |
| 273 | return |
| 274 | } |
| 275 | defer clientSession.Close() |
| 276 | for i := range 10 { |
| 277 | res, err := clientSession.CallTool(ctx, &CallToolParams{Name: "inc"}) |
| 278 | if err != nil { |
| 279 | t.Errorf("CallTool failed: %v", err) |
| 280 | return |
| 281 | } |
| 282 | if got := int(res.StructuredContent.(map[string]any)["Count"].(float64)); got != i { |
| 283 | t.Errorf("got count %d, want %d", got, i) |
| 284 | } |
| 285 | } |
| 286 | }) |
| 287 | } |
| 288 | wg.Wait() |
| 289 | } |
| 290 | |
| 291 | func TestStreamableServerShutdown(t *testing.T) { |
| 292 | ctx := context.Background() |
nothing calls this directly
no test coverage detected
searching dependent graphs…