1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
package tasklog
import (
"bytes"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type ChanTask chan *Update
func (e ChanTask) Updates() <-chan *Update { return e }
func (e ChanTask) Throttled() bool { return true }
type UnthrottledChanTask chan *Update
func (e UnthrottledChanTask) Updates() <-chan *Update { return e }
func (e UnthrottledChanTask) Throttled() bool { return false }
func TestLoggerLogsTasks(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(true))
l.throttle = 0
l.widthFn = func() int { return 0 }
l.Enqueue(ChanTask(task))
l.Close()
assert.Equal(t, "first\rsecond\rsecond, done.\n", buf.String())
}
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())
}
func TestLoggerLogsMultipleTasksInOrder(t *testing.T) {
var buf bytes.Buffer
t1 := make(chan *Update)
go func() {
t1 <- &Update{"first", time.Now(), false}
t1 <- &Update{"second", time.Now(), false}
close(t1)
}()
t2 := make(chan *Update)
go func() {
t2 <- &Update{"third", time.Now(), false}
t2 <- &Update{"fourth", time.Now(), false}
close(t2)
}()
l := NewLogger(&buf, ForceProgress(true))
l.throttle = 0
l.widthFn = func() int { return 0 }
l.Enqueue(ChanTask(t1), ChanTask(t2))
l.Close()
assert.Equal(t, strings.Join([]string{
"first\r",
"second\r",
"second, done.\n",
"third\r",
"fourth\r",
"fourth, done.\n",
}, ""), buf.String())
}
func TestLoggerLogsMultipleTasksWithoutBlocking(t *testing.T) {
var buf bytes.Buffer
l := NewLogger(&buf, ForceProgress(true))
l.throttle = 0
t1, t2 := make(chan *Update), make(chan *Update)
l.widthFn = func() int { return 0 }
l.Enqueue(ChanTask(t1))
t1 <- &Update{"first", time.Now(), false}
l.Enqueue(ChanTask(t2))
close(t1)
t2 <- &Update{"second", time.Now(), false}
close(t2)
l.Close()
assert.Equal(t, strings.Join([]string{
"first\r",
"first, done.\n",
"second\r",
"second, done.\n",
}, ""), buf.String())
}
func TestLoggerThrottlesWrites(t *testing.T) {
var buf bytes.Buffer
t1 := make(chan *Update)
go func() {
start := time.Now()
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
}()
l := NewLogger(&buf, ForceProgress(true))
l.widthFn = func() int { return 0 }
l.throttle = 15 * time.Millisecond
l.Enqueue(ChanTask(t1))
l.Close()
assert.Equal(t, strings.Join([]string{
"first\r",
"forced\r",
"third\r",
"third, done.\n",
}, ""), buf.String())
}
func TestLoggerThrottlesLastWrite(t *testing.T) {
var buf bytes.Buffer
t1 := make(chan *Update)
go func() {
start := time.Now()
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
}()
l := NewLogger(&buf, ForceProgress(true))
l.widthFn = func() int { return 0 }
l.throttle = 15 * time.Millisecond
l.Enqueue(ChanTask(t1))
l.Close()
assert.Equal(t, strings.Join([]string{
"first\r",
"second, done.\n",
}, ""), buf.String())
}
func TestLoggerLogsAllDurableUpdates(t *testing.T) {
var buf bytes.Buffer
l := NewLogger(&buf, ForceProgress(true))
l.widthFn = func() int { return 0 }
l.throttle = 15 * time.Minute
t1 := make(chan *Update)
go func() {
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
}()
l.Enqueue(UnthrottledChanTask(t1))
l.Close()
assert.Equal(t, strings.Join([]string{
"first\r",
"second\r",
"second, done.\n",
}, ""), buf.String())
}
func TestLoggerHandlesSilentTasks(t *testing.T) {
var buf bytes.Buffer
task := make(chan *Update)
close(task)
l := NewLogger(&buf, ForceProgress(true))
l.Enqueue(ChanTask(task))
l.Close()
assert.Equal(t, "", buf.String())
}
|