github.com/waldiirawan/apm-agent-go/v2@v2.2.2/utils_test.go (about) 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 package apm 19 20 import ( 21 "math/rand" 22 "os" 23 "path/filepath" 24 "runtime" 25 "testing" 26 "time" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestGetCurrentProcess(t *testing.T) { 32 process := getCurrentProcess() 33 expected := filepath.Base(os.Args[0]) 34 35 // On Linux, the process title can be at most 36 // 16 bytes, including the null terminator. 37 if runtime.GOOS == "linux" && len(expected) >= 16 { 38 expected = expected[:15] 39 } 40 41 assert.Equal(t, expected, process.Title) 42 } 43 44 func TestGracePeriod(t *testing.T) { 45 var p time.Duration = -1 46 var seq []time.Duration 47 for i := 0; i < 1000; i++ { 48 next := nextGracePeriod(p) 49 if next == p { 50 assert.Equal(t, []time.Duration{ 51 0, 52 time.Second, 53 4 * time.Second, 54 9 * time.Second, 55 16 * time.Second, 56 25 * time.Second, 57 36 * time.Second, 58 }, seq) 59 return 60 } 61 p = next 62 seq = append(seq, p) 63 } 64 t.Fatal("failed to find fixpoint") 65 } 66 67 func TestJitterDuration(t *testing.T) { 68 rng := rand.New(rand.NewSource(time.Now().UnixNano())) 69 assert.Equal(t, time.Duration(0), jitterDuration(0, rng, 0.1)) 70 assert.Equal(t, time.Second, jitterDuration(time.Second, rng, 0)) 71 for i := 0; i < 100; i++ { 72 assert.InDelta(t, time.Second, jitterDuration(time.Second, rng, 0.1), float64(100*time.Millisecond)) 73 } 74 }