forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tasklog: introduce
*SimpleTask
for logging un-related messages
- Loading branch information
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |