@@ -100,9 +100,9 @@ func TestTestTransport(t *testing.T) {
100100 })
101101}
102102func testTestTransport (t testing.TB , mode roundTripTestMode ) {
103- tt := newTestTransport (t )
103+ tt := newTestTransport (t , mode )
104104
105- req := Must (http .NewRequest ("GET" , "https ://dummy.tld/" , nil ))
105+ req := Must (http .NewRequest ("GET" , "http ://dummy.tld/" , nil ))
106106 rt := tt .roundTrip (req )
107107 tc := tt .getConn ()
108108 tc .wantFrameType (FrameSettings )
@@ -461,6 +461,16 @@ func (rt *testRoundTrip) err() error {
461461 return err
462462}
463463
464+ // wantErr indicates the expected RoundTrip error (as compared with Is).
465+ func (rt * testRoundTrip ) wantErr (want error ) {
466+ t := rt .t
467+ t .Helper ()
468+ _ , got := rt .result ()
469+ if ! errors .Is (got , want ) {
470+ t .Fatalf ("got error:\n %v\n want:\n %v" , got , want )
471+ }
472+ }
473+
464474// wantStatus indicates the expected response StatusCode.
465475func (rt * testRoundTrip ) wantStatus (want int ) {
466476 t := rt .t
@@ -491,6 +501,21 @@ func (rt *testRoundTrip) wantBody(want []byte) {
491501 }
492502}
493503
504+ // wantPartialBody indicates the next expected chunk of response body.
505+ func (rt * testRoundTrip ) wantPartialBody (want []byte ) {
506+ t := rt .t
507+ t .Helper ()
508+ got := make ([]byte , len (want ))
509+ n , err := io .ReadFull (rt .response ().Body , got )
510+ got = got [:n ]
511+ if err != nil {
512+ t .Fatalf ("unexpected error reading response body: %v" , err )
513+ }
514+ if ! bytes .Equal (got , want ) {
515+ t .Fatalf ("unexpected partial response body:\n got: %q\n want: %q" , got , want )
516+ }
517+ }
518+
494519// wantHeaders indicates the expected response headers.
495520func (rt * testRoundTrip ) wantHeaders (want http.Header ) {
496521 t := rt .t
@@ -559,17 +584,30 @@ type testTransport struct {
559584 ccqueue []* testClientConn
560585 ccs map [* synctestNetConn ]* testClientConn
561586
587+ controlledDials bool
588+ dialsMu sync.Mutex
589+ dials []* testPendingDial
590+
562591 ccpending []* testPendingClientConn
563592
564593 useTLS bool
565594}
566595
596+ type testPendingDial struct {
597+ address string
598+ errc chan error
599+ }
600+
601+ func (d * testPendingDial ) finish (err error ) { d .errc <- err }
602+
567603type testPendingClientConn struct {
568604 nc * synctestNetConn
569605 cc * ClientConn
570606 tc * testClientConn
571607}
572608
609+ type testControlledDials bool
610+
573611func newTestTransport (t testing.TB , opts ... any ) * testTransport {
574612 tt := & testTransport {
575613 t : t ,
@@ -593,6 +631,9 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
593631 case roundTripXNetHTTP2 :
594632 tr = & Transport {
595633 DialTLSContext : func (ctx context.Context , network , address string , tlsConf * tls.Config ) (net.Conn , error ) {
634+ if err := tt .startDial (address ); err != nil {
635+ return nil , err
636+ }
596637 if tt .useTLS {
597638 return tls .Client (tt .li .newConn (), tlsConf ), nil
598639 }
@@ -607,6 +648,9 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
607648 }
608649
609650 tr1 .DialContext = func (ctx context.Context , network , address string ) (net.Conn , error ) {
651+ if err := tt .startDial (address ); err != nil {
652+ return nil , err
653+ }
610654 return tt .li .newConn (), nil
611655 }
612656 tr1 .TLSClientConfig = testTLSClientConfig
@@ -633,6 +677,8 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
633677 tr = o
634678 case roundTripTestMode :
635679 tt .mode = o
680+ case testControlledDials :
681+ tt .controlledDials = bool (o )
636682 case nil :
637683 default :
638684 t .Fatalf ("unsupported option %T" , o )
@@ -648,6 +694,11 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
648694 tt .maybeAddNewClientConnHook ()
649695
650696 t .Cleanup (func () {
697+ tt .dialsMu .Lock ()
698+ for _ , dial := range tt .dials {
699+ dial .finish (errors .New ("test is done" ))
700+ }
701+ tt .dialsMu .Unlock ()
651702 tt .li .Close ()
652703 synctest .Wait ()
653704 if len (tt .ccqueue ) > 0 {
@@ -664,7 +715,7 @@ func (tt *testTransport) addPending(nc *synctestNetConn, cc *ClientConn, tc *tes
664715
665716 for i , p := range tt .ccpending {
666717 if p .nc != nc {
667- break
718+ continue
668719 }
669720 if p .tc != nil {
670721 p .tc .cc = cc
@@ -696,11 +747,67 @@ func (tt *testTransport) accept() {
696747 }
697748}
698749
750+ func (tt * testTransport ) startDial (address string ) error {
751+ if ! tt .controlledDials {
752+ return nil
753+ }
754+ dial := & testPendingDial {
755+ address : address ,
756+ errc : make (chan error , 1 ),
757+ }
758+ tt .dialsMu .Lock ()
759+ tt .dials = append (tt .dials , dial )
760+ tt .dialsMu .Unlock ()
761+ select {
762+ case err := <- dial .errc :
763+ return err
764+ case <- tt .t .Context ().Done ():
765+ return tt .t .Context ().Err ()
766+ }
767+ }
768+
769+ func (tt * testTransport ) wantDial (address string ) * testPendingDial {
770+ tt .t .Helper ()
771+ synctest .Wait ()
772+ tt .dialsMu .Lock ()
773+ defer tt .dialsMu .Unlock ()
774+ idx := slices .IndexFunc (tt .dials , func (dial * testPendingDial ) bool {
775+ return dial .address == address
776+ })
777+ if idx < 0 {
778+ var dials []string
779+ for _ , d := range tt .dials {
780+ dials = append (dials , d .address )
781+ }
782+ tt .t .Fatalf ("no dial to %q in progress (have %q); wanted one" , address , dials )
783+ }
784+ dial := tt .dials [idx ]
785+ tt .dials = slices .Delete (tt .dials , idx , idx + 1 )
786+ return dial
787+ }
788+
789+ func (tt * testTransport ) wantIdle () {
790+ tt .t .Helper ()
791+ synctest .Wait ()
792+ tt .dialsMu .Lock ()
793+ defer tt .dialsMu .Unlock ()
794+ if len (tt .dials ) > 0 {
795+ tt .t .Fatalf ("%v dials in progress, want idle" , len (tt .dials ))
796+ }
797+ }
798+
699799func (tt * testTransport ) hasConn () bool {
700800 return len (tt .ccqueue ) > 0
701801}
702802
703803func (tt * testTransport ) getConn () * testClientConn {
804+ tt .t .Helper ()
805+ tc := tt .getConnNoPreface ()
806+ tc .readClientPreface ()
807+ return tc
808+ }
809+
810+ func (tt * testTransport ) getConnNoPreface () * testClientConn {
704811 tt .t .Helper ()
705812 synctest .Wait ()
706813 tt .ccMu .Lock ()
@@ -711,7 +818,6 @@ func (tt *testTransport) getConn() *testClientConn {
711818 tc := tt .ccqueue [0 ]
712819 tt .ccqueue = tt .ccqueue [1 :]
713820 tt .ccMu .Unlock ()
714- tc .readClientPreface ()
715821 return tc
716822}
717823
0 commit comments