Repository navigation
Expand file tree
/
Copy pathwindow_test.go
More file actions
156 lines (136 loc) · 3.36 KB
/
Copy pathwindow_test.go
File metadata and controls
156 lines (136 loc) · 3.36 KB
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
package main
import (
"bufio"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func setUp(now time.Time) RollingWindow {
window := Window{
now.Add(-10 * time.Minute),
now.Add(-5 * time.Minute),
now.Add(-65 * time.Second),
now.Add(-55 * time.Second),
now.Add(-10 * time.Second),
}
rollingWin := RollingWindow{
&window,
60,
LogFile{"test_state.log", 0644},
time.RFC3339Nano,
}
return rollingWin
}
func cleanUp(rw *RollingWindow) {
os.Remove(rw.logFile.name)
}
func Test_garbageCollectOlderTimestamps(t *testing.T) {
// should remove unrelevant requests from the Window
now := time.Now()
rollingWin := setUp(now)
expected := Window{
now.Add(-55 * time.Second),
now.Add(-10 * time.Second),
}
rollingWin.garbageCollectOlderTimestamps(now)
got := *rollingWin.Window
assert.Equal(t, expected, got)
cleanUp(&rollingWin)
}
func Test_isTimeStampWithinWindow(t *testing.T) {
now := time.Now()
windowBound := now.Add(-60 * time.Second)
timeOutsideWindow := now.Add(-65 * time.Second)
timeWithinWindow := now.Add(-10 * time.Second)
// should return false when the time is outside the window frame
got := isTimeStampWithinWindow(windowBound, timeOutsideWindow)
expected := false
assert.Equal(t, expected, got)
// should return true when the time is within the window frame
got = isTimeStampWithinWindow(windowBound, timeWithinWindow)
expected = true
assert.Equal(t, expected, got)
}
func Test_loadRollingWindowFromFile(t *testing.T) {
// should return an empty RollingWindow object when the log file is missing
now := time.Now()
rw := RollingWindow{
&Window{},
60,
LogFile{"test_state.log", 0644},
time.RFC3339Nano,
}
loadRollingWindowFromFile(&rw)
assert.Equal(t, 0, len(*rw.Window))
cleanUp(&rw)
// should populate rolling window with details from the specified log file
rw = RollingWindow{
&Window{},
60,
LogFile{"test_state.log", 0644},
time.RFC3339Nano,
}
rw.appendRequestToLog(now)
rollingWin := RollingWindow{
&Window{},
60,
LogFile{"test_state.log", 0644},
time.RFC3339Nano,
}
loadRollingWindowFromFile(&rollingWin)
assert.Equal(
t,
now.Format(rw.timeFormat),
(*rollingWin.Window)[0].Format(rw.timeFormat),
)
cleanUp(&rw)
}
func Test_appendRequestToLog(t *testing.T) {
// should append timestamp to the specified log file
now := time.Now()
rollingWin := RollingWindow{
&Window{},
60,
LogFile{"test_state.log", 0644},
time.RFC3339Nano,
}
rollingWin.appendRequestToLog(now)
file, _ := os.Open(rollingWin.logFile.name)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
log := scanner.Text()
expected := now
got, _ := time.Parse(rollingWin.timeFormat, log)
assert.Equal(
t,
expected.Format(rollingWin.timeFormat),
got.Format(rollingWin.timeFormat),
)
}
cleanUp(&rollingWin)
}
func Test_computeWindowBoundary(t *testing.T) {
// should compute a left boundary of a window
now := time.Now()
rollingWin := RollingWindow{
&Window{},
60,
LogFile{"test_state.log", 0644},
time.RFC3339Nano,
}
got := rollingWin.computeWindowBoundary(now)
expected := now.Add(-60 * time.Second)
assert.Equal(t, expected, got)
}
func Test_addRequestToWindow(t *testing.T) {
// should add request to the window
now := time.Now()
rollingWin := setUp(now)
expected := now.Add(-10 * time.Second)
assert.Equal(
t,
expected,
(*rollingWin.Window)[len(*rollingWin.Window)-1],
)
}