github.com/quay/claircore@v1.5.28/pkg/poolstats/collector_test.go (about) 1 package poolstats 2 3 import ( 4 "strings" 5 "testing" 6 "time" 7 8 "github.com/prometheus/client_golang/prometheus" 9 "github.com/prometheus/client_golang/prometheus/testutil" 10 ) 11 12 type mockStater struct { 13 stats stat 14 } 15 16 func (m *mockStater) Stat() stat { 17 return m.stats 18 } 19 20 var _ stat = (*pgxStatMock)(nil) 21 22 type pgxStatMock struct { 23 acquireCount int64 24 acquireDuration time.Duration 25 canceledAcquireCount int64 26 emptyAcquireCount int64 27 acquiredConns int32 28 constructingConns int32 29 idleConns int32 30 maxConns int32 31 totalConns int32 32 } 33 34 func (m *pgxStatMock) AcquireCount() int64 { 35 return m.acquireCount 36 } 37 38 func (m *pgxStatMock) AcquireDuration() time.Duration { 39 return m.acquireDuration 40 } 41 42 func (m *pgxStatMock) AcquiredConns() int32 { 43 return m.acquiredConns 44 } 45 46 func (m *pgxStatMock) CanceledAcquireCount() int64 { 47 return m.canceledAcquireCount 48 } 49 50 func (m *pgxStatMock) ConstructingConns() int32 { 51 return m.constructingConns 52 } 53 54 func (m *pgxStatMock) EmptyAcquireCount() int64 { 55 return m.emptyAcquireCount 56 } 57 58 func (m *pgxStatMock) IdleConns() int32 { 59 return m.idleConns 60 } 61 62 func (m *pgxStatMock) MaxConns() int32 { 63 return m.maxConns 64 } 65 66 func (m *pgxStatMock) TotalConns() int32 { 67 return m.totalConns 68 } 69 70 func TestDescribe(t *testing.T) { 71 expectedDescriptorCount := 9 72 timeout := time.After(time.Second * 5) 73 stater := &mockStater{&pgxStatMock{}} 74 statFn := func() stat { return stater.Stat() } 75 testObject := newCollector(statFn, t.Name()) 76 77 ch := make(chan *prometheus.Desc) 78 go testObject.Describe(ch) 79 80 uniqueDescriptors := make(map[string]struct{}) 81 var i int 82 for i = 0; i < expectedDescriptorCount; i++ { 83 select { 84 case desc := <-ch: 85 uniqueDescriptors[desc.String()] = struct{}{} 86 case <-timeout: 87 t.Fatalf("timed out wait for %d'th descriptor", i) 88 } 89 } 90 if got, want := expectedDescriptorCount-i, 0; got != want { 91 t.Errorf("got: %d, want: %d", got, want) 92 } 93 if len(uniqueDescriptors) != expectedDescriptorCount { 94 t.Errorf("Expected %d descriptors to be registered but there were %d", expectedDescriptorCount, len(uniqueDescriptors)) 95 } 96 } 97 98 func TestCollect(t *testing.T) { 99 mockStats := &pgxStatMock{ 100 acquireCount: int64(1), 101 acquireDuration: time.Second * 2, 102 acquiredConns: int32(3), 103 canceledAcquireCount: int64(4), 104 constructingConns: int32(5), 105 emptyAcquireCount: int64(6), 106 idleConns: int32(7), 107 maxConns: int32(8), 108 totalConns: int32(9), 109 } 110 stater := &mockStater{mockStats} 111 staterfn := func() stat { return stater.Stat() } 112 testObject := newCollector(staterfn, t.Name()) 113 want := strings.NewReader(`# HELP pgxpool_acquire_count Cumulative count of successful acquires from the pool. 114 # TYPE pgxpool_acquire_count counter 115 pgxpool_acquire_count{application_name="TestCollect"} 1 116 # HELP pgxpool_acquire_duration_seconds_total Total duration of all successful acquires from the pool in nanoseconds. 117 # TYPE pgxpool_acquire_duration_seconds_total counter 118 pgxpool_acquire_duration_seconds_total{application_name="TestCollect"} 2 119 # HELP pgxpool_acquired_conns Number of currently acquired connections in the pool. 120 # TYPE pgxpool_acquired_conns gauge 121 pgxpool_acquired_conns{application_name="TestCollect"} 3 122 # HELP pgxpool_canceled_acquire_count Cumulative count of acquires from the pool that were canceled by a context. 123 # TYPE pgxpool_canceled_acquire_count counter 124 pgxpool_canceled_acquire_count{application_name="TestCollect"} 4 125 # HELP pgxpool_constructing_conns Number of conns with construction in progress in the pool. 126 # TYPE pgxpool_constructing_conns gauge 127 pgxpool_constructing_conns{application_name="TestCollect"} 5 128 # HELP pgxpool_empty_acquire Cumulative count of successful acquires from the pool that waited for a resource to be released or constructed because the pool was empty. 129 # TYPE pgxpool_empty_acquire counter 130 pgxpool_empty_acquire{application_name="TestCollect"} 6 131 # HELP pgxpool_idle_conns Number of currently idle conns in the pool. 132 # TYPE pgxpool_idle_conns gauge 133 pgxpool_idle_conns{application_name="TestCollect"} 7 134 # HELP pgxpool_max_conns Maximum size of the pool. 135 # TYPE pgxpool_max_conns gauge 136 pgxpool_max_conns{application_name="TestCollect"} 8 137 # HELP pgxpool_total_conns Total number of resources currently in the pool. The value is the sum of ConstructingConns, AcquiredConns, and IdleConns. 138 # TYPE pgxpool_total_conns gauge 139 pgxpool_total_conns{application_name="TestCollect"} 9 140 `) 141 142 ls, err := testutil.CollectAndLint(testObject) 143 if err != nil { 144 t.Error(err) 145 } 146 for _, l := range ls { 147 t.Log(l) 148 } 149 if err := testutil.CollectAndCompare(testObject, want); err != nil { 150 t.Error(err) 151 } 152 }