Skip to content

Commit 4a32e08

Browse files
committed
Merge branch 'issue-6072-part-2' into issue-7363
2 parents ec2bf88 + 3926a09 commit 4a32e08

File tree

3 files changed

+80
-175
lines changed

3 files changed

+80
-175
lines changed

internal/testutils/blocking_context_dialer.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ var logger = grpclog.Component("testutils")
3131
// BlockingDialer is a dialer that waits for Resume() to be called before
3232
// dialing.
3333
type BlockingDialer struct {
34-
mu sync.Mutex // protects holds
34+
// mu protects holds.
35+
mu sync.Mutex
36+
// holds maps network addresses to a list of holds for that address.
3537
holds map[string][]*Hold
36-
38+
// dialer dials connections when they are not blocked.
3739
dialer *net.Dialer
3840
}
3941

@@ -77,11 +79,16 @@ func (d *BlockingDialer) DialContext(ctx context.Context, addr string) (net.Conn
7779
// Hold is a handle to a single connection attempt. It can be used to block,
7880
// fail and succeed connection attempts.
7981
type Hold struct {
80-
dialer *BlockingDialer
82+
// dialer is the dialer that created this hold.
83+
dialer *BlockingDialer
84+
// waitCh is closed when a connection attempt is received.
85+
waitCh chan struct{}
86+
// blockCh is closed when the connection attempt should resume.
8187
blockCh chan error
82-
waitCh chan struct{}
83-
err error
84-
addr string
88+
// err is the error to return when the connection attempt is failed.
89+
err error
90+
// addr is the address that this hold is for.
91+
addr string
8592
}
8693

8794
// Hold blocks the dialer when a connection attempt is made to the given addr.

internal/testutils/blocking_context_dialer_test.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ import (
2525
"time"
2626
)
2727

28-
const testTimeout = 5 * time.Second
28+
const (
29+
testTimeout = 5 * time.Second
30+
testShortTimeout = 10 * time.Millisecond
31+
)
2932

3033
func (s) TestBlockingDialer_NoHold(t *testing.T) {
3134
lis, err := LocalTCPListener()
3235
if err != nil {
33-
t.Fatalf("failed to listen: %v", err)
36+
t.Fatalf("Failed to listen: %v", err)
3437
}
3538
defer lis.Close()
3639

@@ -88,8 +91,8 @@ func (s) TestBlockingDialer_HoldWaitResume(t *testing.T) {
8891

8992
select {
9093
case <-done:
91-
t.Errorf("Expected dialer to be blocked.")
92-
default:
94+
t.Fatalf("Expected dialer to be blocked.")
95+
case <-time.After(testShortTimeout):
9396
}
9497

9598
h.Resume() // Unblock the above goroutine.
@@ -104,7 +107,7 @@ func (s) TestBlockingDialer_HoldWaitResume(t *testing.T) {
104107
func (s) TestBlockingDialer_HoldWaitFail(t *testing.T) {
105108
lis, err := LocalTCPListener()
106109
if err != nil {
107-
t.Fatalf("failed to listen: %v", err)
110+
t.Fatalf("Failed to listen: %v", err)
108111
}
109112
defer lis.Close()
110113

@@ -113,30 +116,30 @@ func (s) TestBlockingDialer_HoldWaitFail(t *testing.T) {
113116

114117
wantErr := errors.New("test error")
115118

116-
done := make(chan struct{})
119+
dialError := make(chan error)
117120
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
118121
defer cancel()
119122
go func() {
120123
_, err := d.DialContext(ctx, lis.Addr().String())
121-
if !errors.Is(err, wantErr) {
122-
t.Errorf("BlockingDialer.DialContext() after Fail(): got error %v, want %v", err, wantErr)
123-
}
124-
done <- struct{}{}
124+
dialError <- err
125125
}()
126126

127127
if !h.Wait(ctx) {
128128
t.Fatalf("Timeout while waiting for a connection attempt to " + h.addr)
129129
}
130130
select {
131-
case <-done:
132-
t.Errorf("Expected dialer to still be blocked after Wait()")
133-
default:
131+
case err = <-dialError:
132+
t.Errorf("DialContext got unblocked with err %v. Want DialContext to still be blocked after Wait()", err)
133+
case <-time.After(testShortTimeout):
134134
}
135135

136136
h.Fail(wantErr)
137137

138138
select {
139-
case <-done:
139+
case err = <-dialError:
140+
if !errors.Is(err, wantErr) {
141+
t.Errorf("BlockingDialer.DialContext() after Fail(): got error %v, want %v", err, wantErr)
142+
}
140143
case <-ctx.Done():
141144
t.Errorf("Timeout waiting for connection attempt to fail.")
142145
}
@@ -145,25 +148,22 @@ func (s) TestBlockingDialer_HoldWaitFail(t *testing.T) {
145148
func (s) TestBlockingDialer_ContextCanceled(t *testing.T) {
146149
lis, err := LocalTCPListener()
147150
if err != nil {
148-
t.Fatalf("failed to listen: %v", err)
151+
t.Fatalf("Failed to listen: %v", err)
149152
}
150153
defer lis.Close()
151154

152155
d := NewBlockingDialer()
153156
h := d.Hold(lis.Addr().String())
154157

155-
done := make(chan struct{})
158+
dialErr := make(chan error)
156159
testCtx, cancel := context.WithTimeout(context.Background(), testTimeout)
157160
defer cancel()
158161

159162
ctx, cancel := context.WithCancel(testCtx)
160163
defer cancel()
161164
go func() {
162165
_, err := d.DialContext(ctx, lis.Addr().String())
163-
if !errors.Is(err, context.Canceled) {
164-
t.Errorf("BlockingDialer.DialContext() after context cancel: got error %v, want %v", err, context.Canceled)
165-
}
166-
done <- struct{}{}
166+
dialErr <- err
167167
}()
168168
if !h.Wait(testCtx) {
169169
t.Errorf("Timeout while waiting for a connection attempt to %q", h.addr)
@@ -172,7 +172,10 @@ func (s) TestBlockingDialer_ContextCanceled(t *testing.T) {
172172
cancel()
173173

174174
select {
175-
case <-done:
175+
case err = <-dialErr:
176+
if !errors.Is(err, context.Canceled) {
177+
t.Errorf("BlockingDialer.DialContext() after context cancel: got error %v, want %v", err, context.Canceled)
178+
}
176179
case <-testCtx.Done():
177180
t.Errorf("Timeout while waiting for Wait to return.")
178181
}
@@ -183,7 +186,7 @@ func (s) TestBlockingDialer_ContextCanceled(t *testing.T) {
183186
func (s) TestBlockingDialer_CancelWait(t *testing.T) {
184187
lis, err := LocalTCPListener()
185188
if err != nil {
186-
t.Fatalf("failed to listen: %v", err)
189+
t.Fatalf("Failed to listen: %v", err)
187190
}
188191
defer lis.Close()
189192

0 commit comments

Comments
 (0)