2017-11-22 22:07:24 +00:00
|
|
|
package tasklog
|
2017-06-14 21:55:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-06-16 16:39:17 +00:00
|
|
|
"time"
|
2017-06-14 21:55:50 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-08-09 21:45:18 +00:00
|
|
|
type ChanTask chan *Update
|
2017-06-14 21:55:50 +00:00
|
|
|
|
2017-08-09 21:45:18 +00:00
|
|
|
func (e ChanTask) Updates() <-chan *Update { return e }
|
2017-06-14 21:55:50 +00:00
|
|
|
|
2017-06-19 20:19:19 +00:00
|
|
|
func (e ChanTask) Throttled() bool { return true }
|
2017-06-19 20:14:19 +00:00
|
|
|
|
2017-08-09 21:45:18 +00:00
|
|
|
type UnthrottledChanTask chan *Update
|
2017-06-16 20:42:43 +00:00
|
|
|
|
2017-08-09 21:45:18 +00:00
|
|
|
func (e UnthrottledChanTask) Updates() <-chan *Update { return e }
|
2017-06-16 20:42:43 +00:00
|
|
|
|
2017-06-19 20:19:19 +00:00
|
|
|
func (e UnthrottledChanTask) Throttled() bool { return false }
|
2017-06-16 20:42:43 +00:00
|
|
|
|
2017-06-14 21:55:50 +00:00
|
|
|
func TestLoggerLogsTasks(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2017-08-09 21:45:18 +00:00
|
|
|
task := make(chan *Update)
|
2017-06-14 21:55:50 +00:00
|
|
|
go func() {
|
2017-09-22 18:37:42 +00:00
|
|
|
task <- &Update{"first", time.Now(), false}
|
|
|
|
task <- &Update{"second", time.Now(), false}
|
2017-06-14 21:55:50 +00:00
|
|
|
close(task)
|
|
|
|
}()
|
|
|
|
|
2018-10-30 08:06:55 +00:00
|
|
|
l := NewLogger(&buf, ForceProgress(true))
|
2017-06-16 16:39:17 +00:00
|
|
|
l.throttle = 0
|
2017-06-14 21:55:50 +00:00
|
|
|
l.widthFn = func() int { return 0 }
|
2017-11-16 16:31:30 +00:00
|
|
|
l.Enqueue(ChanTask(task))
|
2017-06-14 21:55:50 +00:00
|
|
|
l.Close()
|
|
|
|
|
|
|
|
assert.Equal(t, "first\rsecond\rsecond, done\n", buf.String())
|
|
|
|
}
|
|
|
|
|
2018-10-30 08:06:55 +00:00
|
|
|
func TestLoggerLogsSuppressesProgress(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
task := make(chan *Update)
|
|
|
|
go func() {
|
|
|
|
task <- &Update{"first", time.Now(), false}
|
|
|
|
task <- &Update{"second", time.Now(), false}
|
|
|
|
close(task)
|
|
|
|
}()
|
|
|
|
|
|
|
|
l := NewLogger(&buf, ForceProgress(false))
|
|
|
|
l.throttle = 0
|
|
|
|
l.widthFn = func() int { return 0 }
|
|
|
|
l.Enqueue(ChanTask(task))
|
|
|
|
l.Close()
|
|
|
|
|
|
|
|
assert.Equal(t, "second, done\n", buf.String())
|
|
|
|
}
|
|
|
|
|
2017-06-14 21:55:50 +00:00
|
|
|
func TestLoggerLogsMultipleTasksInOrder(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2017-08-09 21:45:18 +00:00
|
|
|
t1 := make(chan *Update)
|
2017-06-14 21:55:50 +00:00
|
|
|
go func() {
|
2017-09-22 18:37:42 +00:00
|
|
|
t1 <- &Update{"first", time.Now(), false}
|
|
|
|
t1 <- &Update{"second", time.Now(), false}
|
2017-06-14 21:55:50 +00:00
|
|
|
close(t1)
|
|
|
|
}()
|
2017-08-09 21:45:18 +00:00
|
|
|
t2 := make(chan *Update)
|
2017-06-14 21:55:50 +00:00
|
|
|
go func() {
|
2017-09-22 18:37:42 +00:00
|
|
|
t2 <- &Update{"third", time.Now(), false}
|
|
|
|
t2 <- &Update{"fourth", time.Now(), false}
|
2017-06-14 21:55:50 +00:00
|
|
|
close(t2)
|
|
|
|
}()
|
|
|
|
|
2018-10-30 08:06:55 +00:00
|
|
|
l := NewLogger(&buf, ForceProgress(true))
|
2017-06-16 16:39:17 +00:00
|
|
|
l.throttle = 0
|
2017-06-14 21:55:50 +00:00
|
|
|
l.widthFn = func() int { return 0 }
|
2017-11-16 16:31:30 +00:00
|
|
|
l.Enqueue(ChanTask(t1), ChanTask(t2))
|
2017-06-14 21:55:50 +00:00
|
|
|
l.Close()
|
|
|
|
|
|
|
|
assert.Equal(t, strings.Join([]string{
|
|
|
|
"first\r",
|
|
|
|
"second\r",
|
|
|
|
"second, done\n",
|
|
|
|
"third\r",
|
|
|
|
"fourth\r",
|
|
|
|
"fourth, done\n",
|
|
|
|
}, ""), buf.String())
|
|
|
|
}
|
2017-06-16 16:17:12 +00:00
|
|
|
|
|
|
|
func TestLoggerLogsMultipleTasksWithoutBlocking(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2018-10-30 08:06:55 +00:00
|
|
|
l := NewLogger(&buf, ForceProgress(true))
|
2017-06-16 16:39:17 +00:00
|
|
|
l.throttle = 0
|
2017-08-09 21:45:18 +00:00
|
|
|
t1, t2 := make(chan *Update), make(chan *Update)
|
2017-06-16 16:17:12 +00:00
|
|
|
|
|
|
|
l.widthFn = func() int { return 0 }
|
2017-11-16 16:31:30 +00:00
|
|
|
l.Enqueue(ChanTask(t1))
|
2017-06-16 16:17:12 +00:00
|
|
|
|
2017-09-22 18:37:42 +00:00
|
|
|
t1 <- &Update{"first", time.Now(), false}
|
2017-11-16 16:31:30 +00:00
|
|
|
l.Enqueue(ChanTask(t2))
|
2017-06-16 16:17:12 +00:00
|
|
|
close(t1)
|
2017-09-22 18:37:42 +00:00
|
|
|
t2 <- &Update{"second", time.Now(), false}
|
2017-06-16 16:17:12 +00:00
|
|
|
close(t2)
|
|
|
|
|
|
|
|
l.Close()
|
|
|
|
|
|
|
|
assert.Equal(t, strings.Join([]string{
|
|
|
|
"first\r",
|
|
|
|
"first, done\n",
|
|
|
|
"second\r",
|
|
|
|
"second, done\n",
|
|
|
|
}, ""), buf.String())
|
|
|
|
}
|
2017-06-16 16:39:17 +00:00
|
|
|
|
|
|
|
func TestLoggerThrottlesWrites(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2017-08-09 21:45:18 +00:00
|
|
|
t1 := make(chan *Update)
|
2017-06-16 16:39:17 +00:00
|
|
|
go func() {
|
2017-08-09 21:45:18 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
2017-09-22 18:37:42 +00:00
|
|
|
t1 <- &Update{"first", start, false} // t = 0 ms, throttle was open
|
|
|
|
t1 <- &Update{"forced", start.Add(10 * time.Millisecond), true} // t = 10+ε ms, throttle is closed
|
|
|
|
t1 <- &Update{"second", start.Add(10 * time.Millisecond), false} // t = 10+ε ms, throttle is closed
|
|
|
|
t1 <- &Update{"third", start.Add(26 * time.Millisecond), false} // t = 20+ε ms, throttle was open
|
|
|
|
close(t1) // t = 20+2ε ms, throttle is closed
|
2017-06-16 16:39:17 +00:00
|
|
|
}()
|
|
|
|
|
2018-10-30 08:06:55 +00:00
|
|
|
l := NewLogger(&buf, ForceProgress(true))
|
2017-06-16 16:39:17 +00:00
|
|
|
l.widthFn = func() int { return 0 }
|
2017-06-16 17:01:52 +00:00
|
|
|
l.throttle = 15 * time.Millisecond
|
2017-06-16 16:39:17 +00:00
|
|
|
|
2017-11-16 16:31:30 +00:00
|
|
|
l.Enqueue(ChanTask(t1))
|
2017-06-16 16:39:17 +00:00
|
|
|
l.Close()
|
|
|
|
|
|
|
|
assert.Equal(t, strings.Join([]string{
|
|
|
|
"first\r",
|
2017-09-22 18:37:42 +00:00
|
|
|
"forced\r",
|
2017-06-16 16:39:17 +00:00
|
|
|
"third\r",
|
|
|
|
"third, done\n",
|
|
|
|
}, ""), buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoggerThrottlesLastWrite(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2017-08-09 21:45:18 +00:00
|
|
|
t1 := make(chan *Update)
|
2017-06-16 16:39:17 +00:00
|
|
|
go func() {
|
2017-08-09 21:45:18 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
2017-09-22 18:37:42 +00:00
|
|
|
t1 <- &Update{"first", start, false} // t = 0 ms, throttle was open
|
|
|
|
t1 <- &Update{"second", start.Add(10 * time.Millisecond), false} // t = 10+ε ms, throttle is closed
|
|
|
|
close(t1) // t = 10+2ε ms, throttle is closed
|
2017-06-16 16:39:17 +00:00
|
|
|
}()
|
|
|
|
|
2018-10-30 08:06:55 +00:00
|
|
|
l := NewLogger(&buf, ForceProgress(true))
|
2017-06-16 16:39:17 +00:00
|
|
|
l.widthFn = func() int { return 0 }
|
2017-06-16 17:01:52 +00:00
|
|
|
l.throttle = 15 * time.Millisecond
|
2017-06-16 16:39:17 +00:00
|
|
|
|
2017-11-16 16:31:30 +00:00
|
|
|
l.Enqueue(ChanTask(t1))
|
2017-06-16 16:39:17 +00:00
|
|
|
l.Close()
|
|
|
|
|
|
|
|
assert.Equal(t, strings.Join([]string{
|
|
|
|
"first\r",
|
|
|
|
"second, done\n",
|
|
|
|
}, ""), buf.String())
|
|
|
|
}
|
2017-06-16 20:42:43 +00:00
|
|
|
|
|
|
|
func TestLoggerLogsAllDurableUpdates(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2018-10-30 08:06:55 +00:00
|
|
|
l := NewLogger(&buf, ForceProgress(true))
|
2017-06-16 20:42:43 +00:00
|
|
|
l.widthFn = func() int { return 0 }
|
|
|
|
l.throttle = 15 * time.Minute
|
|
|
|
|
2017-08-09 21:45:18 +00:00
|
|
|
t1 := make(chan *Update)
|
2017-06-16 20:42:43 +00:00
|
|
|
go func() {
|
2017-09-22 18:37:42 +00:00
|
|
|
t1 <- &Update{"first", time.Now(), false} // t = 0+ε ms, throttle is open
|
|
|
|
t1 <- &Update{"second", time.Now(), false} // t = 0+2ε ms, throttle is closed
|
|
|
|
close(t1) // t = 0+3ε ms, throttle is closed
|
2017-06-16 20:42:43 +00:00
|
|
|
}()
|
|
|
|
|
2017-11-16 16:31:30 +00:00
|
|
|
l.Enqueue(UnthrottledChanTask(t1))
|
2017-06-16 20:42:43 +00:00
|
|
|
l.Close()
|
|
|
|
|
|
|
|
assert.Equal(t, strings.Join([]string{
|
|
|
|
"first\r",
|
|
|
|
"second\r",
|
|
|
|
"second, done\n",
|
|
|
|
}, ""), buf.String())
|
|
|
|
}
|
2017-11-16 16:33:10 +00:00
|
|
|
|
|
|
|
func TestLoggerHandlesSilentTasks(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
task := make(chan *Update)
|
|
|
|
close(task)
|
|
|
|
|
2018-10-30 08:06:55 +00:00
|
|
|
l := NewLogger(&buf, ForceProgress(true))
|
2017-11-16 16:33:10 +00:00
|
|
|
l.Enqueue(ChanTask(task))
|
|
|
|
l.Close()
|
|
|
|
|
|
|
|
assert.Equal(t, "", buf.String())
|
|
|
|
}
|