-
Notifications
You must be signed in to change notification settings - Fork 38
/
backoff_internal_test.go
87 lines (60 loc) · 2.03 KB
/
backoff_internal_test.go
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
package backoff
import (
"os"
"testing"
"time"
"github.com/wakatime/wakatime-cli/pkg/config"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestShouldBackoff(t *testing.T) {
at := time.Now().Add(time.Second * -1)
should := shouldBackoff(1, at)
assert.True(t, should)
}
func TestShouldBackoff_AfterResetTime(t *testing.T) {
at := time.Now().Add((resetAfter + 1) * time.Second)
should := shouldBackoff(0, at)
assert.False(t, should)
}
func TestShouldBackoff_NegateBackoff(t *testing.T) {
should := shouldBackoff(0, time.Time{})
assert.False(t, should)
}
func TestUpdateBackoffSettings(t *testing.T) {
v := viper.New()
tmpFile, err := os.CreateTemp(os.TempDir(), "wakatime")
require.NoError(t, err)
defer os.Remove(tmpFile.Name())
v.Set("config", tmpFile.Name())
v.Set("internal-config", tmpFile.Name())
at := time.Now().Add(time.Second * -1)
err = updateBackoffSettings(v, 2, at)
require.NoError(t, err)
ini, err := config.NewIniWriter(v, func(vp *viper.Viper) (string, error) {
assert.Equal(t, v, vp)
return tmpFile.Name(), nil
})
require.NoError(t, err)
backoffAt := ini.File.Section("internal").Key("backoff_at").MustTimeFormat(config.DateFormat)
assert.WithinDuration(t, time.Now(), backoffAt, 15*time.Second)
assert.Equal(t, "2", ini.File.Section("internal").Key("backoff_retries").String())
}
func TestUpdateBackoffSettings_NotInBackoff(t *testing.T) {
v := viper.New()
tmpFile, err := os.CreateTemp(os.TempDir(), "wakatime")
require.NoError(t, err)
defer os.Remove(tmpFile.Name())
v.Set("config", tmpFile.Name())
v.Set("internal-config", tmpFile.Name())
err = updateBackoffSettings(v, 0, time.Time{})
require.NoError(t, err)
ini, err := config.NewIniWriter(v, func(vp *viper.Viper) (string, error) {
assert.Equal(t, v, vp)
return tmpFile.Name(), nil
})
require.NoError(t, err)
assert.Empty(t, ini.File.Section("internal").Key("backoff_at").String())
assert.Equal(t, "0", ini.File.Section("internal").Key("backoff_retries").String())
}