github.com/prebid/prebid-server/v2@v2.18.0/server/listener_test.go (about) 1 package server 2 3 import ( 4 "errors" 5 "net" 6 "testing" 7 "time" 8 9 "github.com/prebid/prebid-server/v2/config" 10 "github.com/prebid/prebid-server/v2/metrics" 11 gometrics "github.com/rcrowley/go-metrics" 12 ) 13 14 func TestNormalConnectionMetrics(t *testing.T) { 15 doTest(t, true, true) 16 } 17 18 func TestAcceptErrorMetrics(t *testing.T) { 19 doTest(t, false, false) 20 } 21 22 func TestCloseErrorMetrics(t *testing.T) { 23 doTest(t, true, false) 24 } 25 26 func doTest(t *testing.T, allowAccept bool, allowClose bool) { 27 reg := gometrics.NewRegistry() 28 me := metrics.NewMetrics(reg, nil, config.DisabledMetrics{}, nil, nil) 29 30 var listener net.Listener = &mockListener{ 31 listenSuccess: allowAccept, 32 closeSuccess: allowClose, 33 } 34 35 listener = &monitorableListener{listener, me} 36 conn, err := listener.Accept() 37 if !allowAccept { 38 if err == nil { 39 t.Error("The listener.Accept() error should propagate from the underlying listener.") 40 } 41 assertCount(t, "When Accept() fails, connection count", me.ConnectionCounter.Count(), 0) 42 assertCount(t, "When Accept() fails, Accept() errors", me.ConnectionAcceptErrorMeter.Count(), 1) 43 assertCount(t, "When Accept() fails, Close() errors", me.ConnectionCloseErrorMeter.Count(), 0) 44 return 45 } 46 assertCount(t, "When Accept() succeeds, active connections", me.ConnectionCounter.Count(), 1) 47 assertCount(t, "When Accept() succeeds, Accept() errors", me.ConnectionAcceptErrorMeter.Count(), 0) 48 49 err = conn.Close() 50 if allowClose { 51 assertCount(t, "When Accept() and Close() succeed, connection count", me.ConnectionCounter.Count(), 0) 52 assertCount(t, "When Accept() and Close() succeed, Accept() errors", me.ConnectionAcceptErrorMeter.Count(), 0) 53 assertCount(t, "When Accept() and Close() succeed, Close() errors", me.ConnectionCloseErrorMeter.Count(), 0) 54 } else { 55 if err == nil { 56 t.Error("The connection.Close() error should propagate from the underlying listener.") 57 } 58 assertCount(t, "When Accept() succeeds sand Close() fails, connection count", me.ConnectionCounter.Count(), 1) 59 assertCount(t, "When Accept() succeeds sand Close() fails, Accept() errors", me.ConnectionAcceptErrorMeter.Count(), 0) 60 assertCount(t, "When Accept() succeeds sand Close() fails, Close() errors", me.ConnectionCloseErrorMeter.Count(), 1) 61 } 62 } 63 64 func assertCount(t *testing.T, context string, actual int64, expected int) { 65 t.Helper() 66 if actual != int64(expected) { 67 t.Errorf("%s: expected %d, got %d", context, expected, actual) 68 } 69 } 70 71 type mockListener struct { 72 listenSuccess bool 73 closeSuccess bool 74 } 75 76 func (l *mockListener) Accept() (net.Conn, error) { 77 if l.listenSuccess { 78 return &mockConnection{l.closeSuccess}, nil 79 } else { 80 return nil, errors.New("Failed to open connection") 81 } 82 } 83 84 func (l *mockListener) Close() error { 85 return nil 86 } 87 88 func (l *mockListener) Addr() net.Addr { 89 return &mockAddr{} 90 } 91 92 type mockConnection struct { 93 closeSuccess bool 94 } 95 96 func (c *mockConnection) Read(b []byte) (n int, err error) { 97 return len(b), nil 98 } 99 100 func (c *mockConnection) Write(b []byte) (n int, err error) { 101 return 102 } 103 104 func (c *mockConnection) Close() error { 105 if c.closeSuccess { 106 return nil 107 } else { 108 return errors.New("Failed to close connection.") 109 } 110 } 111 112 func (c *mockConnection) LocalAddr() net.Addr { 113 return &mockAddr{} 114 } 115 116 func (c *mockConnection) RemoteAddr() net.Addr { 117 return &mockAddr{} 118 } 119 120 func (c *mockConnection) SetDeadline(t time.Time) error { 121 return nil 122 } 123 124 func (c *mockConnection) SetReadDeadline(t time.Time) error { 125 return nil 126 } 127 128 func (c *mockConnection) SetWriteDeadline(t time.Time) error { 129 return nil 130 } 131 132 type mockAddr struct{} 133 134 func (m *mockAddr) Network() string { 135 return "tcp" 136 } 137 138 func (m *mockAddr) String() string { 139 return "192.0.2.1:25" 140 }