Skip to content

Commit

Permalink
tasklog: introduce *SimpleTask for logging un-related messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ttaylorr committed Nov 28, 2017
1 parent 3bea566 commit 7a760b6
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tasklog/simple_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tasklog

import (
"fmt"
"time"
)

// SimpleTask is in an implementation of tasklog.Task which prints out messages
// verbatim.
type SimpleTask struct {
// ch is used to transmit task updates.
ch chan *Update
}

// NewSimpleTask returns a new *SimpleTask instance.
func NewSimpleTask() *SimpleTask {
return &SimpleTask{
ch: make(chan *Update),
}
}

// Log logs a string with no formatting verbs.
func (s *SimpleTask) Log(str string) {
s.Logf(str)
}

// Logf logs some formatted string, which is interpreted according to the rules
// defined in package "fmt".
func (s *SimpleTask) Logf(str string, vals ...interface{}) {
s.ch <- &Update{
S: fmt.Sprintf(str, vals...),
At: time.Now(),
}
}

// Complete notes that the task is completed by closing the Updates channel and
// yields the logger to the next Task.
func (s *SimpleTask) Complete() {
close(s.ch)
}

// Updates implements Task.Updates and returns a channel of updates which is
// closed when Complete() is called.
func (s *SimpleTask) Updates() <-chan *Update {
return s.ch
}

// Throttled implements Task.Throttled and returns false, indicating that this
// task is not throttled.
func (s *SimpleTask) Throttled() bool { return false }
76 changes: 76 additions & 0 deletions tasklog/simple_task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package tasklog

import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSimpleTaskLogLogsUpdates(t *testing.T) {
task := NewSimpleTask()

var updates []*Update

wait := new(sync.WaitGroup)
wait.Add(1)
go func() {
for update := range task.Updates() {
updates = append(updates, update)
}
wait.Done()
}()

task.Log("Hello, world")
task.Complete()

require.Len(t, updates, 1)
assert.Equal(t, "Hello, world", updates[0].S)
}

func TestSimpleTaskLogfLogsFormattedUpdates(t *testing.T) {
task := NewSimpleTask()

var updates []*Update

wait := new(sync.WaitGroup)
wait.Add(1)
go func() {
for update := range task.Updates() {
updates = append(updates, update)
}
wait.Done()
}()

task.Logf("Hello, world (%d)", 3+4)
task.Complete()

require.Len(t, updates, 1)
assert.Equal(t, "Hello, world (7)", updates[0].S)
}

func TestSimpleTaskCompleteClosesUpdates(t *testing.T) {
task := NewSimpleTask()

select {
case <-task.Updates():
t.Fatalf("tasklog: unexpected update from *SimpleTask")
default:
}

task.Complete()

if _, ok := <-task.Updates(); ok {
t.Fatalf("tasklog: expected (*SimpleTask).Updates() to be closed")
}
}

func TestSimpleTaskIsNotThrottled(t *testing.T) {
task := NewSimpleTask()

throttled := task.Throttled()

assert.False(t, throttled,
"tasklog: expected *SimpleTask not to be Throttle()-d")
}

0 comments on commit 7a760b6

Please sign in to comment.