|
| 1 | +package tasklog |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestSimpleTaskLogLogsUpdates(t *testing.T) { |
| 12 | + task := NewSimpleTask() |
| 13 | + |
| 14 | + var updates []*Update |
| 15 | + |
| 16 | + wait := new(sync.WaitGroup) |
| 17 | + wait.Add(1) |
| 18 | + go func() { |
| 19 | + for update := range task.Updates() { |
| 20 | + updates = append(updates, update) |
| 21 | + } |
| 22 | + wait.Done() |
| 23 | + }() |
| 24 | + |
| 25 | + task.Log("Hello, world") |
| 26 | + task.Complete() |
| 27 | + |
| 28 | + require.Len(t, updates, 1) |
| 29 | + assert.Equal(t, "Hello, world", updates[0].S) |
| 30 | +} |
| 31 | + |
| 32 | +func TestSimpleTaskLogfLogsFormattedUpdates(t *testing.T) { |
| 33 | + task := NewSimpleTask() |
| 34 | + |
| 35 | + var updates []*Update |
| 36 | + |
| 37 | + wait := new(sync.WaitGroup) |
| 38 | + wait.Add(1) |
| 39 | + go func() { |
| 40 | + for update := range task.Updates() { |
| 41 | + updates = append(updates, update) |
| 42 | + } |
| 43 | + wait.Done() |
| 44 | + }() |
| 45 | + |
| 46 | + task.Logf("Hello, world (%d)", 3+4) |
| 47 | + task.Complete() |
| 48 | + |
| 49 | + require.Len(t, updates, 1) |
| 50 | + assert.Equal(t, "Hello, world (7)", updates[0].S) |
| 51 | +} |
| 52 | + |
| 53 | +func TestSimpleTaskCompleteClosesUpdates(t *testing.T) { |
| 54 | + task := NewSimpleTask() |
| 55 | + |
| 56 | + select { |
| 57 | + case <-task.Updates(): |
| 58 | + t.Fatalf("tasklog: unexpected update from *SimpleTask") |
| 59 | + default: |
| 60 | + } |
| 61 | + |
| 62 | + task.Complete() |
| 63 | + |
| 64 | + if _, ok := <-task.Updates(); ok { |
| 65 | + t.Fatalf("tasklog: expected (*SimpleTask).Updates() to be closed") |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestSimpleTaskIsNotThrottled(t *testing.T) { |
| 70 | + task := NewSimpleTask() |
| 71 | + |
| 72 | + throttled := task.Throttled() |
| 73 | + |
| 74 | + assert.False(t, throttled, |
| 75 | + "tasklog: expected *SimpleTask not to be Throttle()-d") |
| 76 | +} |
0 commit comments