git/githistory/log: teach Durable() to Task

This commit is contained in:
Taylor Blau 2017-06-16 14:42:43 -06:00
parent cd38fd5d87
commit e2fe5d084e
3 changed files with 55 additions and 1 deletions

@ -175,16 +175,28 @@ func (l *Logger) consume() {
// logTask logs the set of updates from a given task to the sink, then logs a
// "done" message, and then marks the task as done.
//
// By default, the *Logger throttles log entry updates to once per the duration
// of time specified by `l.throttle time.Duration`.
//
// If the duration if 0, or the task is "durable" (by implementing
// github.com/git-lfs/git-lfs/git/githistory/log#DurableTask), then all entries
// will be logged.
func (l *Logger) logTask(task Task) {
defer l.wg.Done()
var logAll bool
if durable, ok := task.(DurableTask); ok {
logAll = durable.Durable()
}
var last time.Time
var msg string
for msg = range task.Updates() {
now := time.Now()
if l.throttle == 0 || now.After(last.Add(l.throttle)) {
if logAll || l.throttle == 0 || now.After(last.Add(l.throttle)) {
l.logLine(msg)
last = now
}

@ -13,6 +13,12 @@ type ChanTask chan string
func (e ChanTask) Updates() <-chan string { return e }
type DurableChanTask chan string
func (e DurableChanTask) Updates() <-chan string { return e }
func (e DurableChanTask) Durable() bool { return true }
func TestLoggerLogsTasks(t *testing.T) {
var buf bytes.Buffer
@ -140,3 +146,27 @@ func TestLoggerThrottlesLastWrite(t *testing.T) {
"second, done\n",
}, ""), buf.String())
}
func TestLoggerLogsAllDurableUpdates(t *testing.T) {
var buf bytes.Buffer
l := NewLogger(&buf)
l.widthFn = func() int { return 0 }
l.throttle = 15 * time.Minute
t1 := make(chan string)
go func() {
t1 <- "first" // t = 0+ε ms, throttle is open
t1 <- "second" // t = 0+2ε ms, throttle is closed
close(t1) // t = 0+3ε ms, throttle is closed
}()
l.enqueue(DurableChanTask(t1))
l.Close()
assert.Equal(t, strings.Join([]string{
"first\r",
"second\r",
"second, done\n",
}, ""), buf.String())
}

@ -7,3 +7,15 @@ type Task interface {
// complete.
Updates() <-chan string
}
// DurableTask is a Task sub-interface which ensures that all activity is
// logged by disabling throttling behavior in the logger.
type DurableTask interface {
Task
// Durable returns whether or not this task should be treated as
// Durable.
//
// It is expected to return the same value for a given Task instance.
Durable() bool
}