Skip to content

Commit 7a760b6

Browse files
committed
tasklog: introduce *SimpleTask for logging un-related messages
1 parent 3bea566 commit 7a760b6

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

tasklog/simple_task.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package tasklog
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// SimpleTask is in an implementation of tasklog.Task which prints out messages
9+
// verbatim.
10+
type SimpleTask struct {
11+
// ch is used to transmit task updates.
12+
ch chan *Update
13+
}
14+
15+
// NewSimpleTask returns a new *SimpleTask instance.
16+
func NewSimpleTask() *SimpleTask {
17+
return &SimpleTask{
18+
ch: make(chan *Update),
19+
}
20+
}
21+
22+
// Log logs a string with no formatting verbs.
23+
func (s *SimpleTask) Log(str string) {
24+
s.Logf(str)
25+
}
26+
27+
// Logf logs some formatted string, which is interpreted according to the rules
28+
// defined in package "fmt".
29+
func (s *SimpleTask) Logf(str string, vals ...interface{}) {
30+
s.ch <- &Update{
31+
S: fmt.Sprintf(str, vals...),
32+
At: time.Now(),
33+
}
34+
}
35+
36+
// Complete notes that the task is completed by closing the Updates channel and
37+
// yields the logger to the next Task.
38+
func (s *SimpleTask) Complete() {
39+
close(s.ch)
40+
}
41+
42+
// Updates implements Task.Updates and returns a channel of updates which is
43+
// closed when Complete() is called.
44+
func (s *SimpleTask) Updates() <-chan *Update {
45+
return s.ch
46+
}
47+
48+
// Throttled implements Task.Throttled and returns false, indicating that this
49+
// task is not throttled.
50+
func (s *SimpleTask) Throttled() bool { return false }

tasklog/simple_task_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)