|
7 | 7 | "strings" |
8 | 8 | "sync" |
9 | 9 | "testing" |
| 10 | + "time" |
10 | 11 |
|
11 | 12 | "github.com/google/go-cmp/cmp" |
12 | 13 | "github.com/google/go-cmp/cmp/cmpopts" |
@@ -493,3 +494,62 @@ func TestConcurrentHubClone(t *testing.T) { |
493 | 494 | t.Errorf("Events mismatch (-want +got):\n%s", diff) |
494 | 495 | } |
495 | 496 | } |
| 497 | + |
| 498 | +func TestHub_Flush(t *testing.T) { |
| 499 | + hub, client, _ := setupHubTest() |
| 500 | + transport := &MockTransport{} |
| 501 | + client.Transport = transport |
| 502 | + |
| 503 | + wantEvent := Event{Message: "something"} |
| 504 | + hub.CaptureEvent(&wantEvent) |
| 505 | + hub.Flush(20 * time.Millisecond) |
| 506 | + |
| 507 | + gotEvents := transport.Events() |
| 508 | + if len(gotEvents) != 1 { |
| 509 | + t.Fatalf("expected 1 event, got %d", len(gotEvents)) |
| 510 | + } |
| 511 | + if gotEvents[0].Message != wantEvent.Message { |
| 512 | + t.Fatalf("expected message to be %v, got %v", wantEvent.Message, gotEvents[0].Message) |
| 513 | + } |
| 514 | +} |
| 515 | + |
| 516 | +func TestHub_Flush_NoClient(t *testing.T) { |
| 517 | + hub := NewHub(nil, nil) |
| 518 | + flushed := hub.Flush(20 * time.Millisecond) |
| 519 | + |
| 520 | + if flushed != false { |
| 521 | + t.Fatalf("expected flush to be false, got %v", flushed) |
| 522 | + } |
| 523 | +} |
| 524 | + |
| 525 | +func TestHub_FlushWithCtx_NoClient(t *testing.T) { |
| 526 | + hub := NewHub(nil, nil) |
| 527 | + cancelCtx, cancel := context.WithCancel(context.Background()) |
| 528 | + defer cancel() |
| 529 | + flushed := hub.FlushWithContext(cancelCtx) |
| 530 | + |
| 531 | + if flushed != false { |
| 532 | + t.Fatalf("expected flush to be false, got %v", flushed) |
| 533 | + } |
| 534 | +} |
| 535 | + |
| 536 | +func TestHub_FlushWithContext(t *testing.T) { |
| 537 | + hub, client, _ := setupHubTest() |
| 538 | + transport := &MockTransport{} |
| 539 | + client.Transport = transport |
| 540 | + |
| 541 | + wantEvent := Event{Message: "something"} |
| 542 | + hub.CaptureEvent(&wantEvent) |
| 543 | + |
| 544 | + cancelCtx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) |
| 545 | + hub.FlushWithContext(cancelCtx) |
| 546 | + defer cancel() |
| 547 | + |
| 548 | + gotEvents := transport.Events() |
| 549 | + if len(gotEvents) != 1 { |
| 550 | + t.Fatalf("expected 1 event, got %d", len(gotEvents)) |
| 551 | + } |
| 552 | + if gotEvents[0].Message != wantEvent.Message { |
| 553 | + t.Fatalf("expected message to be %v, got %v", wantEvent.Message, gotEvents[0].Message) |
| 554 | + } |
| 555 | +} |
0 commit comments