|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package managedwriter |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "sync/atomic" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "golang.org/x/sync/errgroup" |
| 26 | +) |
| 27 | + |
| 28 | +func TestFlowControllerCancel(t *testing.T) { |
| 29 | + // Test canceling a flow controller's context. |
| 30 | + t.Parallel() |
| 31 | + wantInsertBytes := 10 |
| 32 | + fc := newFlowController(3, wantInsertBytes) |
| 33 | + if fc.maxInsertBytes != 10 { |
| 34 | + t.Fatalf("maxInsertBytes mismatch, got %d want %d", fc.maxInsertBytes, wantInsertBytes) |
| 35 | + } |
| 36 | + if err := fc.acquire(context.Background(), 5); err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + // Experiment: a context that times out should always return an error. |
| 40 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond) |
| 41 | + defer cancel() |
| 42 | + if err := fc.acquire(ctx, 6); err != context.DeadlineExceeded { |
| 43 | + t.Fatalf("got %v, expected DeadlineExceeded", err) |
| 44 | + } |
| 45 | + // Control: a context that is not done should always return nil. |
| 46 | + go func() { |
| 47 | + time.Sleep(5 * time.Millisecond) |
| 48 | + fc.release(5) |
| 49 | + }() |
| 50 | + if err := fc.acquire(context.Background(), 6); err != nil { |
| 51 | + t.Errorf("got %v, expected nil", err) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestFlowControllerLargeRequest(t *testing.T) { |
| 56 | + // Large requests succeed, consuming the entire allotment. |
| 57 | + t.Parallel() |
| 58 | + fc := newFlowController(3, 10) |
| 59 | + err := fc.acquire(context.Background(), 11) |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestFlowControllerNoStarve(t *testing.T) { |
| 66 | + // A large request won't starve, because the flowController is |
| 67 | + // (best-effort) FIFO. |
| 68 | + t.Parallel() |
| 69 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 70 | + defer cancel() |
| 71 | + fc := newFlowController(10, 10) |
| 72 | + first := make(chan int) |
| 73 | + for i := 0; i < 20; i++ { |
| 74 | + go func() { |
| 75 | + for { |
| 76 | + if err := fc.acquire(ctx, 1); err != nil { |
| 77 | + if err != context.Canceled { |
| 78 | + t.Error(err) |
| 79 | + } |
| 80 | + return |
| 81 | + } |
| 82 | + select { |
| 83 | + case first <- 1: |
| 84 | + default: |
| 85 | + } |
| 86 | + fc.release(1) |
| 87 | + } |
| 88 | + }() |
| 89 | + } |
| 90 | + <-first // Wait until the flowController's state is non-zero. |
| 91 | + if err := fc.acquire(ctx, 11); err != nil { |
| 92 | + t.Errorf("got %v, want nil", err) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestFlowControllerSaturation(t *testing.T) { |
| 97 | + t.Parallel() |
| 98 | + const ( |
| 99 | + maxCount = 6 |
| 100 | + maxSize = 10 |
| 101 | + ) |
| 102 | + for _, test := range []struct { |
| 103 | + acquireSize int |
| 104 | + wantCount, wantSize int64 |
| 105 | + }{ |
| 106 | + { |
| 107 | + // Many small acquires cause the flow controller to reach its max count. |
| 108 | + acquireSize: 1, |
| 109 | + wantCount: 6, |
| 110 | + wantSize: 6, |
| 111 | + }, |
| 112 | + { |
| 113 | + // Five acquires of size 2 will cause the flow controller to reach its max size, |
| 114 | + // but not its max count. |
| 115 | + acquireSize: 2, |
| 116 | + wantCount: 5, |
| 117 | + wantSize: 10, |
| 118 | + }, |
| 119 | + { |
| 120 | + // If the requests are the right size (relatively prime to maxSize), |
| 121 | + // the flow controller will not saturate on size. (In this case, not on count either.) |
| 122 | + acquireSize: 3, |
| 123 | + wantCount: 3, |
| 124 | + wantSize: 9, |
| 125 | + }, |
| 126 | + } { |
| 127 | + fc := newFlowController(maxCount, maxSize) |
| 128 | + // Atomically track flow controller state. |
| 129 | + // The flowController itself tracks count. |
| 130 | + var curSize int64 |
| 131 | + success := errors.New("") |
| 132 | + // Time out if wantSize or wantCount is never reached. |
| 133 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 134 | + defer cancel() |
| 135 | + g, ctx := errgroup.WithContext(ctx) |
| 136 | + for i := 0; i < 10; i++ { |
| 137 | + g.Go(func() error { |
| 138 | + var hitCount, hitSize bool |
| 139 | + // Run at least until we hit the expected values, and at least |
| 140 | + // for enough iterations to exceed them if the flow controller |
| 141 | + // is broken. |
| 142 | + for i := 0; i < 100 || !hitCount || !hitSize; i++ { |
| 143 | + select { |
| 144 | + case <-ctx.Done(): |
| 145 | + return ctx.Err() |
| 146 | + default: |
| 147 | + } |
| 148 | + if err := fc.acquire(ctx, test.acquireSize); err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + c := int64(fc.count()) |
| 152 | + if c > test.wantCount { |
| 153 | + return fmt.Errorf("count %d exceeds want %d", c, test.wantCount) |
| 154 | + } |
| 155 | + if c == test.wantCount { |
| 156 | + hitCount = true |
| 157 | + } |
| 158 | + s := atomic.AddInt64(&curSize, int64(test.acquireSize)) |
| 159 | + if s > test.wantSize { |
| 160 | + return fmt.Errorf("size %d exceeds want %d", s, test.wantSize) |
| 161 | + } |
| 162 | + if s == test.wantSize { |
| 163 | + hitSize = true |
| 164 | + } |
| 165 | + time.Sleep(5 * time.Millisecond) // Let other goroutines make progress. |
| 166 | + if atomic.AddInt64(&curSize, -int64(test.acquireSize)) < 0 { |
| 167 | + return errors.New("negative size") |
| 168 | + } |
| 169 | + fc.release(test.acquireSize) |
| 170 | + } |
| 171 | + return success |
| 172 | + }) |
| 173 | + } |
| 174 | + if err := g.Wait(); err != success { |
| 175 | + t.Errorf("%+v: %v", test, err) |
| 176 | + continue |
| 177 | + } |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +func TestFlowControllerTryAcquire(t *testing.T) { |
| 182 | + t.Parallel() |
| 183 | + fc := newFlowController(3, 10) |
| 184 | + |
| 185 | + // Successfully tryAcquire 4 bytes. |
| 186 | + if !fc.tryAcquire(4) { |
| 187 | + t.Error("got false, wanted true") |
| 188 | + } |
| 189 | + |
| 190 | + // Fail to tryAcquire 7 bytes. |
| 191 | + if fc.tryAcquire(7) { |
| 192 | + t.Error("got true, wanted false") |
| 193 | + } |
| 194 | + |
| 195 | + // Successfully tryAcquire 6 byte. |
| 196 | + if !fc.tryAcquire(6) { |
| 197 | + t.Error("got false, wanted true") |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +func TestFlowControllerUnboundedCount(t *testing.T) { |
| 202 | + t.Parallel() |
| 203 | + ctx := context.Background() |
| 204 | + fc := newFlowController(0, 10) |
| 205 | + |
| 206 | + // Successfully acquire 4 bytes. |
| 207 | + if err := fc.acquire(ctx, 4); err != nil { |
| 208 | + t.Errorf("got %v, wanted no error", err) |
| 209 | + } |
| 210 | + |
| 211 | + // Successfully tryAcquire 4 bytes. |
| 212 | + if !fc.tryAcquire(4) { |
| 213 | + t.Error("got false, wanted true") |
| 214 | + } |
| 215 | + |
| 216 | + // Fail to tryAcquire 3 bytes. |
| 217 | + if fc.tryAcquire(3) { |
| 218 | + t.Error("got true, wanted false") |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +func TestFlowControllerUnboundedCount2(t *testing.T) { |
| 223 | + t.Parallel() |
| 224 | + ctx := context.Background() |
| 225 | + fc := newFlowController(0, 0) |
| 226 | + // Successfully acquire 4 bytes. |
| 227 | + if err := fc.acquire(ctx, 4); err != nil { |
| 228 | + t.Errorf("got %v, wanted no error", err) |
| 229 | + } |
| 230 | + fc.release(1) |
| 231 | + fc.release(1) |
| 232 | + fc.release(1) |
| 233 | + wantCount := int64(-2) |
| 234 | + c := int64(fc.count()) |
| 235 | + if c != wantCount { |
| 236 | + t.Fatalf("got count %d, want %d", c, wantCount) |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +func TestFlowControllerUnboundedBytes(t *testing.T) { |
| 241 | + t.Parallel() |
| 242 | + ctx := context.Background() |
| 243 | + fc := newFlowController(2, 0) |
| 244 | + |
| 245 | + // Successfully acquire 4GB. |
| 246 | + if err := fc.acquire(ctx, 4e9); err != nil { |
| 247 | + t.Errorf("got %v, wanted no error", err) |
| 248 | + } |
| 249 | + |
| 250 | + // Successfully tryAcquire 4GB bytes. |
| 251 | + if !fc.tryAcquire(4e9) { |
| 252 | + t.Error("got false, wanted true") |
| 253 | + } |
| 254 | + |
| 255 | + // Fail to tryAcquire a third message. |
| 256 | + if fc.tryAcquire(3) { |
| 257 | + t.Error("got true, wanted false") |
| 258 | + } |
| 259 | +} |
0 commit comments