| 332 | } |
| 333 | |
| 334 | func TestChunkQueue_Next(t *testing.T) { |
| 335 | queue, teardown := setupChunkQueue(t) |
| 336 | defer teardown() |
| 337 | |
| 338 | // Next should block waiting for the next chunks, even when given out of order. |
| 339 | chNext := make(chan *chunk, 10) |
| 340 | go func() { |
| 341 | for { |
| 342 | c, err := queue.Next() |
| 343 | if err == errDone { |
| 344 | close(chNext) |
| 345 | break |
| 346 | } |
| 347 | require.NoError(t, err) |
| 348 | chNext <- c |
| 349 | } |
| 350 | }() |
| 351 | |
| 352 | assert.Empty(t, chNext) |
| 353 | _, err := queue.Add(&chunk{Height: 3, Format: 1, Index: 1, Chunk: []byte{3, 1, 1}, Sender: p2p.ID("b")}) |
| 354 | require.NoError(t, err) |
| 355 | select { |
| 356 | case <-chNext: |
| 357 | assert.Fail(t, "channel should be empty") |
| 358 | default: |
| 359 | } |
| 360 | |
| 361 | _, err = queue.Add(&chunk{Height: 3, Format: 1, Index: 0, Chunk: []byte{3, 1, 0}, Sender: p2p.ID("a")}) |
| 362 | require.NoError(t, err) |
| 363 | |
| 364 | assert.Equal(t, |
| 365 | &chunk{Height: 3, Format: 1, Index: 0, Chunk: []byte{3, 1, 0}, Sender: p2p.ID("a")}, |
| 366 | <-chNext) |
| 367 | assert.Equal(t, |
| 368 | &chunk{Height: 3, Format: 1, Index: 1, Chunk: []byte{3, 1, 1}, Sender: p2p.ID("b")}, |
| 369 | <-chNext) |
| 370 | |
| 371 | _, err = queue.Add(&chunk{Height: 3, Format: 1, Index: 4, Chunk: []byte{3, 1, 4}, Sender: p2p.ID("e")}) |
| 372 | require.NoError(t, err) |
| 373 | select { |
| 374 | case <-chNext: |
| 375 | assert.Fail(t, "channel should be empty") |
| 376 | default: |
| 377 | } |
| 378 | |
| 379 | _, err = queue.Add(&chunk{Height: 3, Format: 1, Index: 2, Chunk: []byte{3, 1, 2}, Sender: p2p.ID("c")}) |
| 380 | require.NoError(t, err) |
| 381 | _, err = queue.Add(&chunk{Height: 3, Format: 1, Index: 3, Chunk: []byte{3, 1, 3}, Sender: p2p.ID("d")}) |
| 382 | require.NoError(t, err) |
| 383 | |
| 384 | assert.Equal(t, |
| 385 | &chunk{Height: 3, Format: 1, Index: 2, Chunk: []byte{3, 1, 2}, Sender: p2p.ID("c")}, |
| 386 | <-chNext) |
| 387 | assert.Equal(t, |
| 388 | &chunk{Height: 3, Format: 1, Index: 3, Chunk: []byte{3, 1, 3}, Sender: p2p.ID("d")}, |
| 389 | <-chNext) |
| 390 | assert.Equal(t, |
| 391 | &chunk{Height: 3, Format: 1, Index: 4, Chunk: []byte{3, 1, 4}, Sender: p2p.ID("e")}, |