|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2024 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package testutils |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "errors" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | +) |
| 27 | + |
| 28 | +const testTimeout = 5 * time.Second |
| 29 | + |
| 30 | +func (s) TestBlockingDialer_NoHold(t *testing.T) { |
| 31 | + lis, err := LocalTCPListener() |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("failed to listen: %v", err) |
| 34 | + } |
| 35 | + defer lis.Close() |
| 36 | + |
| 37 | + d := NewBlockingDialer() |
| 38 | + |
| 39 | + // This should not block. |
| 40 | + ctx, cancel := context.WithTimeout(context.Background(), testTimeout) |
| 41 | + defer cancel() |
| 42 | + conn, err := d.DialContext(ctx, lis.Addr().String()) |
| 43 | + if err != nil { |
| 44 | + t.Errorf("unexpected error: %v", err) |
| 45 | + } |
| 46 | + conn.Close() |
| 47 | +} |
| 48 | + |
| 49 | +func (s) TestBlockingDialer_HoldWaitResume(t *testing.T) { |
| 50 | + lis, err := LocalTCPListener() |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("failed to listen: %v", err) |
| 53 | + } |
| 54 | + defer lis.Close() |
| 55 | + |
| 56 | + d := NewBlockingDialer() |
| 57 | + h := d.Hold(lis.Addr().String()) |
| 58 | + |
| 59 | + done := make(chan struct{}) |
| 60 | + ctx, cancel := context.WithTimeout(context.Background(), testTimeout) |
| 61 | + defer cancel() |
| 62 | + go func() { |
| 63 | + conn, err := d.DialContext(ctx, lis.Addr().String()) |
| 64 | + if err != nil { |
| 65 | + t.Errorf("BlockingDialer.DialContext() got error: %v, want success", err) |
| 66 | + } |
| 67 | + conn.Close() |
| 68 | + done <- struct{}{} |
| 69 | + }() |
| 70 | + |
| 71 | + // This should block until the goroutine above is scheduled. |
| 72 | + if !h.Wait(ctx) { |
| 73 | + t.Fatalf("Timeout while waiting for a connection attempt to " + h.addr) |
| 74 | + } |
| 75 | + select { |
| 76 | + case <-done: |
| 77 | + t.Errorf("Expected dialer to be blocked.") |
| 78 | + default: |
| 79 | + } |
| 80 | + |
| 81 | + h.Resume() // Unblock the above goroutine. |
| 82 | + |
| 83 | + select { |
| 84 | + case <-done: |
| 85 | + case <-ctx.Done(): |
| 86 | + t.Errorf("Timeout waiting for connection attempt to resume.") |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func (s) TestBlockingDialer_HoldWaitFail(t *testing.T) { |
| 91 | + lis, err := LocalTCPListener() |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("failed to listen: %v", err) |
| 94 | + } |
| 95 | + defer lis.Close() |
| 96 | + |
| 97 | + d := NewBlockingDialer() |
| 98 | + h := d.Hold(lis.Addr().String()) |
| 99 | + |
| 100 | + wantErr := errors.New("test error") |
| 101 | + |
| 102 | + done := make(chan struct{}) |
| 103 | + ctx, cancel := context.WithTimeout(context.Background(), testTimeout) |
| 104 | + defer cancel() |
| 105 | + go func() { |
| 106 | + _, err := d.DialContext(ctx, lis.Addr().String()) |
| 107 | + if !errors.Is(err, wantErr) { |
| 108 | + t.Errorf("BlockingDialer.DialContext() after Fail(): got error %v, want %v", err, wantErr) |
| 109 | + } |
| 110 | + done <- struct{}{} |
| 111 | + }() |
| 112 | + |
| 113 | + if !h.Wait(ctx) { |
| 114 | + t.Fatalf("Timeout while waiting for a connection attempt to " + h.addr) |
| 115 | + } |
| 116 | + select { |
| 117 | + case <-done: |
| 118 | + t.Errorf("Expected dialer to still be blocked after Wait()") |
| 119 | + default: |
| 120 | + } |
| 121 | + |
| 122 | + h.Fail(wantErr) |
| 123 | + |
| 124 | + select { |
| 125 | + case <-done: |
| 126 | + case <-ctx.Done(): |
| 127 | + t.Errorf("Timeout waiting for connection attempt to fail.") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func (s) TestBlockingDialer_ContextCanceled(t *testing.T) { |
| 132 | + lis, err := LocalTCPListener() |
| 133 | + if err != nil { |
| 134 | + t.Fatalf("failed to listen: %v", err) |
| 135 | + } |
| 136 | + defer lis.Close() |
| 137 | + |
| 138 | + d := NewBlockingDialer() |
| 139 | + h := d.Hold(lis.Addr().String()) |
| 140 | + |
| 141 | + done := make(chan struct{}) |
| 142 | + testCtx, cancel := context.WithTimeout(context.Background(), testTimeout) |
| 143 | + defer cancel() |
| 144 | + |
| 145 | + ctx, cancel := context.WithCancel(testCtx) |
| 146 | + defer cancel() |
| 147 | + go func() { |
| 148 | + _, err := d.DialContext(ctx, lis.Addr().String()) |
| 149 | + if !errors.Is(err, context.Canceled) { |
| 150 | + t.Errorf("BlockingDialer.DialContext() after context cancel: got error %v, want %v", err, context.Canceled) |
| 151 | + } |
| 152 | + done <- struct{}{} |
| 153 | + }() |
| 154 | + if !h.Wait(ctx) { |
| 155 | + t.Fatalf("Timeout while waiting for a connection attempt to " + h.addr) |
| 156 | + } |
| 157 | + cancel() |
| 158 | + |
| 159 | + select { |
| 160 | + case <-done: |
| 161 | + case <-testCtx.Done(): |
| 162 | + t.Errorf("Timeout while waiting for Wait to return.") |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func (s) TestBlockingDialer_CancelWait(t *testing.T) { |
| 167 | + lis, err := LocalTCPListener() |
| 168 | + if err != nil { |
| 169 | + t.Fatalf("failed to listen: %v", err) |
| 170 | + } |
| 171 | + defer lis.Close() |
| 172 | + |
| 173 | + d := NewBlockingDialer() |
| 174 | + h := d.Hold(lis.Addr().String()) |
| 175 | + |
| 176 | + testCtx, cancel := context.WithTimeout(context.Background(), testTimeout) |
| 177 | + defer cancel() |
| 178 | + |
| 179 | + ctx, cancel := context.WithTimeout(testCtx, 0) |
| 180 | + defer cancel() |
| 181 | + done := make(chan struct{}) |
| 182 | + go func() { |
| 183 | + if h.Wait(ctx) { |
| 184 | + t.Errorf("Expected cancel to return false when context expires") |
| 185 | + } |
| 186 | + done <- struct{}{} |
| 187 | + }() |
| 188 | + |
| 189 | + select { |
| 190 | + case <-done: |
| 191 | + case <-testCtx.Done(): |
| 192 | + t.Errorf("Timeout while waiting for Wait to return.") |
| 193 | + } |
| 194 | +} |
0 commit comments