MCPcopy
hub / github.com/keploy/keploy / TestV2_MultipleExchanges

Function TestV2_MultipleExchanges

pkg/agent/proxy/integrations/generic/v2_test.go:172–233  ·  view source on GitHub ↗

TestV2_MultipleExchanges feeds two full req/resp pairs and expects two mocks, each anchored to the timestamps of its own exchange.

(t *testing.T)

Source from the content-addressed store, hash-verified

170// TestV2_MultipleExchanges feeds two full req/resp pairs and expects
171// two mocks, each anchored to the timestamps of its own exchange.
172func TestV2_MultipleExchanges(t *testing.T) {
173 t.Parallel()
174
175 clientCh := make(chan fakeconn.Chunk, 4)
176 destCh := make(chan fakeconn.Chunk, 4)
177
178 req1At := time.Unix(2000, 0)
179 resp1WrittenAt := time.Unix(2001, 0)
180 req2At := time.Unix(3000, 0)
181 resp2WrittenAt := time.Unix(3001, 0)
182
183 // Push first exchange, then second. Closing the dest channel after
184 // the second response signals EOF so the loop drains and exits.
185 clientCh <- fakeconn.Chunk{Dir: fakeconn.FromClient, Bytes: []byte("req1"), ReadAt: req1At}
186 destCh <- fakeconn.Chunk{Dir: fakeconn.FromDest, Bytes: []byte("resp1"), WrittenAt: resp1WrittenAt}
187
188 // Small pause to let the parser observe the first response before
189 // the second request arrives. Using a tiny sleep here is OK because
190 // we're driving synthetic chunks, not measuring real time.
191 go func() {
192 time.Sleep(10 * time.Millisecond)
193 clientCh <- fakeconn.Chunk{Dir: fakeconn.FromClient, Bytes: []byte("req2"), ReadAt: req2At}
194 time.Sleep(10 * time.Millisecond)
195 destCh <- fakeconn.Chunk{Dir: fakeconn.FromDest, Bytes: []byte("resp2"), WrittenAt: resp2WrittenAt}
196 close(clientCh)
197 close(destCh)
198 }()
199
200 sess, mocks := newV2Session(t, clientCh, destCh)
201 if err := dispatch(t, sess); err != nil {
202 t.Fatalf("RecordOutgoing: %v", err)
203 }
204 close(mocks)
205 got := drainMocks(mocks)
206 if len(got) != 2 {
207 t.Fatalf("expected 2 mocks, got %d", len(got))
208 }
209 if got[0].Spec.GenericRequests[0].Message[0].Data != "req1" {
210 t.Errorf("mock[0] req = %q, want req1",
211 got[0].Spec.GenericRequests[0].Message[0].Data)
212 }
213 if got[1].Spec.GenericRequests[0].Message[0].Data != "req2" {
214 t.Errorf("mock[1] req = %q, want req2",
215 got[1].Spec.GenericRequests[0].Message[0].Data)
216 }
217 if !got[0].Spec.ReqTimestampMock.Equal(req1At) {
218 t.Errorf("mock[0] ReqTimestampMock = %v, want %v",
219 got[0].Spec.ReqTimestampMock, req1At)
220 }
221 if !got[1].Spec.ReqTimestampMock.Equal(req2At) {
222 t.Errorf("mock[1] ReqTimestampMock = %v, want %v",
223 got[1].Spec.ReqTimestampMock, req2At)
224 }
225 if !got[0].Spec.ResTimestampMock.Equal(resp1WrittenAt) {
226 t.Errorf("mock[0] ResTimestampMock = %v, want %v",
227 got[0].Spec.ResTimestampMock, resp1WrittenAt)
228 }
229 if !got[1].Spec.ResTimestampMock.Equal(resp2WrittenAt) {

Callers

nothing calls this directly

Calls 3

newV2SessionFunction · 0.85
dispatchFunction · 0.85
drainMocksFunction · 0.85

Tested by

no test coverage detected