@@ -24,6 +24,7 @@ import (
24
24
"github.com/stretchr/testify/assert"
25
25
"github.com/stretchr/testify/require"
26
26
"go.uber.org/zap"
27
+ "go.uber.org/zap/zaptest/observer"
27
28
28
29
"github.com/jaegertracing/jaeger/pkg/testutils"
29
30
"github.com/jaegertracing/jaeger/thrift-gen/sampling"
@@ -242,54 +243,81 @@ func TestDeepCopy(t *testing.T) {
242
243
SamplingRate : 0.5 ,
243
244
},
244
245
}
245
- copy := deepCopy (s )
246
- assert .False (t , copy == s )
247
- assert .EqualValues (t , copy , s )
246
+ cp := deepCopy (s )
247
+ assert .False (t , cp == s )
248
+ assert .EqualValues (t , cp , s )
248
249
}
249
250
250
251
func TestAutoUpdateStrategy (t * testing.T ) {
251
- // copy from fixtures/strategies.json
252
252
tempFile , _ := ioutil .TempFile ("" , "for_go_test_*.json" )
253
- tempFile .Close ()
253
+ require .NoError (t , tempFile .Close ())
254
+ defer func () {
255
+ require .NoError (t , os .Remove (tempFile .Name ()))
256
+ }()
254
257
258
+ // copy known fixture content into temp file which we can later overwrite
255
259
srcFile , dstFile := "fixtures/strategies.json" , tempFile .Name ()
256
260
srcBytes , err := ioutil .ReadFile (srcFile )
257
261
require .NoError (t , err )
258
- err = ioutil .WriteFile (dstFile , srcBytes , 0644 )
259
- require .NoError (t , err )
262
+ require .NoError (t , ioutil .WriteFile (dstFile , srcBytes , 0644 ))
260
263
261
- interval := time .Millisecond * 10
262
- store , err := NewStrategyStore (Options {
264
+ ss , err := NewStrategyStore (Options {
263
265
StrategiesFile : dstFile ,
264
- ReloadInterval : interval ,
266
+ ReloadInterval : time . Millisecond * 10 ,
265
267
}, zap .NewNop ())
266
268
require .NoError (t , err )
267
- defer store .(* strategyStore ).Close ()
269
+ store := ss .(* strategyStore )
270
+ defer store .Close ()
268
271
272
+ // confirm baseline value
269
273
s , err := store .GetSamplingStrategy ("foo" )
270
274
require .NoError (t , err )
271
275
assert .EqualValues (t , makeResponse (sampling .SamplingStrategyType_PROBABILISTIC , 0.8 ), * s )
272
276
273
- // update file
274
- newStr := strings .Replace (string (srcBytes ), "0.8" , "0.9" , 1 )
275
- err = ioutil .WriteFile (dstFile , []byte (newStr ), 0644 )
276
- require .NoError (t , err )
277
-
278
- // wait for reloading
279
- time .Sleep (interval * 4 )
277
+ // verify that reloading in no-op
278
+ value := store .reloadSamplingStrategyFile (dstFile , string (srcBytes ))
279
+ assert .Equal (t , string (srcBytes ), value )
280
280
281
- // verity reloading
282
- s , err = store .GetSamplingStrategy ("foo" )
283
- require .NoError (t , err )
281
+ // update file with new probability of 0.9
282
+ newStr := strings .Replace (string (srcBytes ), "0.8" , "0.9" , 1 )
283
+ require .NoError (t , ioutil .WriteFile (dstFile , []byte (newStr ), 0644 ))
284
+
285
+ // wait for reload timer
286
+ for i := 0 ; i < 1000 ; i ++ { // wait up to 1sec
287
+ s , err = store .GetSamplingStrategy ("foo" )
288
+ require .NoError (t , err )
289
+ if s .ProbabilisticSampling != nil && s .ProbabilisticSampling .SamplingRate == 0.9 {
290
+ break
291
+ }
292
+ time .Sleep (1 * time .Millisecond )
293
+ }
284
294
assert .EqualValues (t , makeResponse (sampling .SamplingStrategyType_PROBABILISTIC , 0.9 ), * s )
295
+ }
285
296
286
- // check bad file content
287
- _ = ioutil .WriteFile (dstFile , []byte ("bad value" ), 0644 )
288
- time .Sleep (interval * 2 )
297
+ func TestAutoUpdateStrategyErrors (t * testing.T ) {
298
+ tempFile , _ := ioutil .TempFile ("" , "for_go_test_*.json" )
299
+ require .NoError (t , tempFile .Close ())
300
+ defer func () {
301
+ _ = os .Remove (tempFile .Name ())
302
+ }()
303
+
304
+ zapCore , logs := observer .New (zap .InfoLevel )
305
+ logger := zap .New (zapCore )
306
+
307
+ s , err := NewStrategyStore (Options {
308
+ StrategiesFile : "fixtures/strategies.json" ,
309
+ ReloadInterval : time .Hour ,
310
+ }, logger )
311
+ require .NoError (t , err )
312
+ store := s .(* strategyStore )
313
+ defer store .Close ()
289
314
290
- // remove file(test read file failure)
291
- _ = os .Remove (dstFile )
292
- // wait for delete and update failure
293
- time .Sleep (interval * 2 )
315
+ // check invalid file path or read failure
316
+ assert .Equal (t , "blah" , store .reloadSamplingStrategyFile (tempFile .Name ()+ "bad-path" , "blah" ))
317
+ assert .Len (t , logs .FilterMessage ("failed to load sampling strategies" ).All (), 1 )
294
318
319
+ // check bad file content
320
+ require .NoError (t , ioutil .WriteFile (tempFile .Name (), []byte ("bad value" ), 0644 ))
321
+ assert .Equal (t , "blah" , store .reloadSamplingStrategyFile (tempFile .Name (), "blah" ))
322
+ assert .Len (t , logs .FilterMessage ("failed to update sampling strategies from file" ).All (), 1 )
295
323
}
0 commit comments