This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory_test.go
135 lines (115 loc) · 3.87 KB
/
factory_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
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
// Copyright (c) 2018 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"expvar"
"fmt"
"io"
"os"
"testing"
"time"
assert "github.com/stretchr/testify/require"
"github.com/uber/jaeger-lib/metrics"
"go.uber.org/zap"
"github.com/jaegertracing/jaeger/pkg/config"
)
func TestInitializationErrors(t *testing.T) {
f := NewFactory()
v, command := config.Viperize(f.AddFlags)
dir := "/root/this_should_fail" // If this test fails, you have some issues in your system
keyParam := fmt.Sprintf("--badger.directory-key=%s", dir)
valueParam := fmt.Sprintf("--badger.directory-value=%s", dir)
command.ParseFlags([]string{
"--badger.ephemeral=false",
"--badger.consistency=true",
keyParam,
valueParam,
})
f.InitFromViper(v)
err := f.Initialize(metrics.NullFactory, zap.NewNop())
assert.Error(t, err)
}
func TestForCodecov(t *testing.T) {
// These tests are testing our vendor packages and are intended to satisfy Codecov.
f := NewFactory()
v, _ := config.Viperize(f.AddFlags)
f.InitFromViper(v)
err := f.Initialize(metrics.NullFactory, zap.NewNop())
assert.NoError(t, err)
// Now, remove the badger directories
err = os.RemoveAll(f.tmpDir)
assert.NoError(t, err)
// Now try to close, since the files have been deleted this should throw an error
err = f.Close()
assert.Error(t, err)
}
func TestMaintenanceRun(t *testing.T) {
// For Codecov - this does not test anything
f := NewFactory()
v, command := config.Viperize(f.AddFlags)
// Lets speed up the maintenance ticker..
command.ParseFlags([]string{
"--badger.maintenance-interval=10ms",
})
f.InitFromViper(v)
// Safeguard
mFactory := metrics.NewLocalFactory(0)
_, gs := mFactory.Snapshot()
assert.True(t, gs[lastMaintenanceRunName] == 0)
f.Initialize(mFactory, zap.NewNop())
waiter := func(previousValue int64) int64 {
sleeps := 0
_, gs := mFactory.Snapshot()
for gs[lastMaintenanceRunName] == previousValue && sleeps < 8 {
// Wait for the scheduler
time.Sleep(time.Duration(50) * time.Millisecond)
sleeps++
_, gs = mFactory.Snapshot()
}
assert.True(t, gs[lastMaintenanceRunName] > previousValue)
return gs[lastMaintenanceRunName]
}
runtime := waiter(0) // First run, check that it was ran and caches previous size
// This is to for codecov only. Can break without anything else breaking as it does test badger's
// internal implementation
vlogSize := expvar.Get("badger_vlog_size_bytes").(*expvar.Map).Get(f.tmpDir).(*expvar.Int)
currSize := vlogSize.Value()
vlogSize.Set(currSize + 1<<31)
runtime = waiter(runtime)
_, gs = mFactory.Snapshot()
assert.True(t, gs[lastValueLogCleanedName] > 0)
err := io.Closer(f).Close()
assert.NoError(t, err)
}
// TestMaintenanceCodecov this test is not intended to test anything, but hopefully increase coverage by triggering a log line
func TestMaintenanceCodecov(t *testing.T) {
// For Codecov - this does not test anything
f := NewFactory()
v, command := config.Viperize(f.AddFlags)
// Lets speed up the maintenance ticker..
command.ParseFlags([]string{
"--badger.maintenance-interval=10ms",
})
f.InitFromViper(v)
mFactory := metrics.NewLocalFactory(0)
f.Initialize(mFactory, zap.NewNop())
waiter := func() {
for sleeps := 0; sleeps < 8; sleeps++ {
// Wait for the scheduler
time.Sleep(time.Duration(50) * time.Millisecond)
}
}
_ = f.store.Close()
waiter() // This should trigger the logging of error
}