github.com/uber-go/tally/v4@v4.1.17/prometheus/config_test.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package prometheus
    22  
    23  import (
    24  	"io/ioutil"
    25  	"net"
    26  	"os"
    27  	"path"
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/stretchr/testify/assert"
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestListenErrorCallsOnRegisterError(t *testing.T) {
    36  	// Ensure that Listen error calls default OnRegisterError to panic
    37  	listener, err := net.Listen("tcp", "127.0.0.1:0")
    38  	require.NoError(t, err)
    39  
    40  	defer func() { _ = listener.Close() }()
    41  
    42  	assert.NotPanics(t, func() {
    43  		cfg := Configuration{
    44  			ListenAddress: listener.Addr().String(),
    45  			OnError:       "log",
    46  		}
    47  		_, _ = cfg.NewReporter(ConfigurationOptions{})
    48  		time.Sleep(time.Second)
    49  	})
    50  }
    51  
    52  func TestUnixDomainSocketListener(t *testing.T) {
    53  	dir, err := ioutil.TempDir("", "tally-test-prometheus")
    54  	require.NoError(t, err)
    55  
    56  	defer os.RemoveAll(dir)
    57  
    58  	uds := path.Join(dir, "test-metrics.sock")
    59  	cfg := Configuration{ListenNetwork: "unix", ListenAddress: uds}
    60  	_, _ = cfg.NewReporter(ConfigurationOptions{})
    61  
    62  	time.Sleep(time.Second)
    63  	_, err = os.Stat(uds)
    64  	require.NoError(t, err)
    65  	require.NoError(t, os.Remove(uds))
    66  }
    67  
    68  func TestTcpListener(t *testing.T) {
    69  	cases := map[string]Configuration{
    70  		"127.0.0.1:0":        {ListenAddress: "127.0.0.1:0"},
    71  		"tcp://127.0.0.1:0":  {ListenNetwork: "tcp", ListenAddress: "127.0.0.1:0"},
    72  		"tcp4://127.0.0.1:0": {ListenNetwork: "tcp4", ListenAddress: "127.0.0.1:0"},
    73  	}
    74  
    75  	for cn, cc := range cases {
    76  		t.Run(cn, func(t *testing.T) {
    77  			assert.NotPanics(t, func() {
    78  				_, _ = cc.NewReporter(ConfigurationOptions{})
    79  				time.Sleep(time.Second)
    80  			})
    81  		})
    82  	}
    83  }