Skip to content

Commit 844fe63

Browse files
Initialize the interval trigger (#259)
* Initialize the interval trigger Without lastRotate initialized an interval trigger will always trigger a rotation the first time its callback is executed.
1 parent fea0092 commit 844fe63

File tree

4 files changed

+140
-2
lines changed

4 files changed

+140
-2
lines changed

file/helper_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func SyncParent(path string) error {
4242
parent := filepath.Dir(path)
4343
f, err := os.Open(parent)
4444

45-
//nolint:nilerr // ignore error
4645
if err != nil {
46+
//lint:ignore nilerr failing open is ok
4747
return nil
4848
}
4949
defer f.Close()

file/rotator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func TestDailyRotation(t *testing.T) {
173173
WriteMsg(t, r)
174174
}
175175

176-
AssertDirContents(t, dir, logname+"-"+today+"-1.ndjson", logname+"-"+today+"-2.ndjson", logname+"-"+today+"-3.ndjson", logname+"-diagnostic-"+twoDaysAgo+".zip")
176+
AssertDirContents(t, dir, logname+"-"+today+".ndjson", logname+"-"+today+"-1.ndjson", logname+"-"+today+"-2.ndjson", logname+"-diagnostic-"+twoDaysAgo+".zip")
177177
}
178178

179179
// Tests the FileConfig.RotateOnStartup parameter

file/trigger.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ func newIntervalTrigger(interval time.Duration, clock clock) trigger {
138138
return lastInterval != currentInterval
139139
}
140140
}
141+
142+
t.lastRotate = clock.Now()
141143
return &t
142144
}
143145

file/trigger_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//go:build !windows
19+
20+
package file
21+
22+
import (
23+
"testing"
24+
"time"
25+
26+
"github.com/stretchr/testify/assert"
27+
)
28+
29+
func TestInitTrigger(t *testing.T) {
30+
var trigger initTrigger
31+
assert.Equal(t, trigger.TriggerRotation(0), rotateReasonInitializing)
32+
assert.Equal(t, trigger.TriggerRotation(0), rotateReasonNoRotate)
33+
assert.Equal(t, trigger.TriggerRotation(0), rotateReasonNoRotate)
34+
assert.Equal(t, trigger.TriggerRotation(0), rotateReasonNoRotate)
35+
}
36+
37+
func TestSizeTrigger(t *testing.T) {
38+
trigger := sizeTrigger{
39+
maxSizeBytes: 5,
40+
size: 0,
41+
}
42+
43+
assert.EqualValues(t, trigger.size, 0)
44+
assert.Equal(t, trigger.TriggerRotation(1), rotateReasonNoRotate)
45+
assert.EqualValues(t, trigger.size, 1)
46+
assert.Equal(t, trigger.TriggerRotation(1), rotateReasonNoRotate)
47+
assert.EqualValues(t, trigger.size, 2)
48+
assert.Equal(t, trigger.TriggerRotation(1), rotateReasonNoRotate)
49+
assert.EqualValues(t, trigger.size, 3)
50+
assert.Equal(t, trigger.TriggerRotation(1), rotateReasonNoRotate)
51+
assert.EqualValues(t, trigger.size, 4)
52+
assert.Equal(t, trigger.TriggerRotation(1), rotateReasonNoRotate)
53+
assert.EqualValues(t, trigger.size, 5)
54+
assert.Equal(t, trigger.TriggerRotation(1), rotateReasonFileSize)
55+
assert.EqualValues(t, trigger.size, 0)
56+
}
57+
58+
type always20240615 struct{}
59+
60+
func (always20240615) Now() time.Time {
61+
return time.Date(2024, 06, 15, 12, 30, 30, 0, time.UTC)
62+
}
63+
64+
func TestIntervalTrigger(t *testing.T) {
65+
var ignored uint = 1
66+
67+
var testCases = []struct {
68+
duration string
69+
afterSecond bool
70+
afterMinute bool
71+
afterHour bool
72+
afterDay bool
73+
afterWeek bool
74+
afterMonth bool
75+
afterYear bool
76+
}{
77+
{"1s", true, true, true, true, true, true, true},
78+
{"1m", false, true, true, true, true, true, true},
79+
{"1h", false, false, true, true, true, true, true},
80+
{"24h", false, false, false, true, true, true, true},
81+
{"168h", false, false, false, false, true, true, true}, // week: 7 * 24 = 168
82+
{"720h", false, false, false, false, false, true, true}, // month:30 * 24 = 720
83+
{"8760h", false, false, false, false, false, false, true}, // year: 24 * 365 = 8760
84+
}
85+
86+
clock := &always20240615{}
87+
88+
for _, testCase := range testCases {
89+
duration, err := time.ParseDuration(testCase.duration)
90+
assert.Nil(t, err)
91+
genericTrigger := newIntervalTrigger(duration, clock)
92+
trigger, ok := genericTrigger.(*intervalTrigger)
93+
assert.True(t, ok)
94+
95+
// ensure lastRotate is initialized
96+
assert.NotZero(t, trigger.lastRotate)
97+
98+
// Should not fire immediately
99+
assert.Equal(t, trigger.TriggerRotation(ignored), rotateReasonNoRotate)
100+
101+
// Test after a second and ensure it doesn't fire immediately after
102+
trigger.lastRotate = clock.Now().Add(time.Second * -1)
103+
assert.Equal(t, trigger.TriggerRotation(ignored) == rotateReasonTimeInterval, testCase.afterSecond)
104+
assert.Equal(t, trigger.TriggerRotation(ignored), rotateReasonNoRotate)
105+
106+
// Test after a minute and ensure it doesn't fire immediately after
107+
trigger.lastRotate = clock.Now().Add(time.Minute * -1)
108+
assert.Equal(t, trigger.TriggerRotation(ignored) == rotateReasonTimeInterval, testCase.afterMinute)
109+
assert.Equal(t, trigger.TriggerRotation(ignored), rotateReasonNoRotate)
110+
111+
// Test after an hour and ensure it doesn't fire immediately after
112+
trigger.lastRotate = clock.Now().Add(time.Hour * -1)
113+
assert.Equal(t, trigger.TriggerRotation(ignored) == rotateReasonTimeInterval, testCase.afterHour)
114+
assert.Equal(t, trigger.TriggerRotation(ignored), rotateReasonNoRotate)
115+
116+
// Test after a day and ensure it doesn't fire immediately after
117+
trigger.lastRotate = clock.Now().Add(time.Hour * -24)
118+
assert.Equal(t, trigger.TriggerRotation(ignored) == rotateReasonTimeInterval, testCase.afterDay)
119+
assert.Equal(t, trigger.TriggerRotation(ignored), rotateReasonNoRotate)
120+
121+
// Test after a week and ensure it doesn't fire immediately after
122+
trigger.lastRotate = clock.Now().Add(time.Hour * -24 * 7)
123+
assert.Equal(t, trigger.TriggerRotation(ignored) == rotateReasonTimeInterval, testCase.afterWeek)
124+
assert.Equal(t, trigger.TriggerRotation(ignored), rotateReasonNoRotate)
125+
126+
// Test after a month and ensure it doesn't fire immediately after
127+
trigger.lastRotate = clock.Now().Add(time.Hour * -24 * 31)
128+
assert.Equal(t, trigger.TriggerRotation(ignored) == rotateReasonTimeInterval, testCase.afterMonth)
129+
assert.Equal(t, trigger.TriggerRotation(ignored), rotateReasonNoRotate)
130+
131+
// Test after a year and ensure it doesn't fire immediately after
132+
trigger.lastRotate = clock.Now().Add(time.Hour * -24 * 365)
133+
assert.Equal(t, trigger.TriggerRotation(ignored) == rotateReasonTimeInterval, testCase.afterYear)
134+
assert.Equal(t, trigger.TriggerRotation(ignored), rotateReasonNoRotate)
135+
}
136+
}

0 commit comments

Comments
 (0)