| 287 | } |
| 288 | |
| 289 | func Test_MsgQueue_Full(t *testing.T) { |
| 290 | t.Parallel() |
| 291 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 292 | defer cancel() |
| 293 | |
| 294 | firstDequeue := make(chan struct{}) |
| 295 | allowRead := make(chan struct{}) |
| 296 | n := 0 |
| 297 | errors := make(chan error) |
| 298 | uut := pubsub.NewMsgQueue(ctx, nil, func(ctx context.Context, msg []byte, err error) { |
| 299 | if n == 0 { |
| 300 | close(firstDequeue) |
| 301 | } |
| 302 | <-allowRead |
| 303 | if err == nil { |
| 304 | require.Equal(t, fmt.Sprintf("%d", n), string(msg)) |
| 305 | n++ |
| 306 | return |
| 307 | } |
| 308 | errors <- err |
| 309 | }) |
| 310 | defer uut.Close() |
| 311 | |
| 312 | // we send 2 more than the capacity. One extra because the call to the ListenerFunc blocks |
| 313 | // but only after we've dequeued a message, and then another extra because we want to exceed |
| 314 | // the capacity, not just reach it. |
| 315 | for i := 0; i < pubsub.BufferSize+2; i++ { |
| 316 | uut.Enqueue([]byte(fmt.Sprintf("%d", i))) |
| 317 | // ensure the first dequeue has happened before proceeding, so that this function isn't racing |
| 318 | // against the goroutine that dequeues items. |
| 319 | <-firstDequeue |
| 320 | } |
| 321 | close(allowRead) |
| 322 | |
| 323 | select { |
| 324 | case <-ctx.Done(): |
| 325 | t.Fatal("timed out") |
| 326 | case err := <-errors: |
| 327 | require.ErrorIs(t, err, pubsub.ErrDroppedMessages) |
| 328 | } |
| 329 | // Ok, so we sent 2 more than capacity, but we only read the capacity, that's because the last |
| 330 | // message we send doesn't get queued, AND, it bumps a message out of the queue to make room |
| 331 | // for the error, so we read 2 less than we sent. |
| 332 | require.Equal(t, pubsub.BufferSize, n) |
| 333 | } |