github.com/prebid/prebid-server/v2@v2.18.0/server/server_test.go (about) 1 package server 2 3 import ( 4 "fmt" 5 "net" 6 "net/http" 7 "os" 8 "strconv" 9 "testing" 10 "time" 11 12 "github.com/prebid/prebid-server/v2/config" 13 metricsconfig "github.com/prebid/prebid-server/v2/metrics/config" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 ) 17 18 func TestServerShutdown(t *testing.T) { 19 server := &http.Server{} 20 ln := &mockListener{} 21 22 stopper := make(chan os.Signal) 23 done := make(chan struct{}) 24 go shutdownAfterSignals(server, stopper, done) 25 go server.Serve(ln) //nolint: errcheck 26 27 stopper <- os.Interrupt 28 <-done 29 30 // If the test didn't hang, then we know server.Shutdown really _did_ return, and shutdownAfterSignals 31 // passed the message along as expected. 32 } 33 34 func TestWait(t *testing.T) { 35 inbound := make(chan os.Signal) 36 chan1 := make(chan os.Signal) 37 chan2 := make(chan os.Signal) 38 chan3 := make(chan os.Signal) 39 done := make(chan struct{}) 40 41 go forwardSignal(t, done, chan1) 42 go forwardSignal(t, done, chan2) 43 go forwardSignal(t, done, chan3) 44 45 go func(chan os.Signal) { 46 inbound <- os.Interrupt 47 }(inbound) 48 49 wait(inbound, done, chan1, chan2, chan3) 50 // If this doesn't hang, then wait() is sending and receiving messages as expected. 51 } 52 53 // forwardSignal is basically a working mock for shutdownAfterSignals(). 54 // It is used to test wait() effectively 55 func forwardSignal(t *testing.T, outbound chan<- struct{}, inbound <-chan os.Signal) { 56 var s struct{} 57 sig := <-inbound 58 if sig != os.Interrupt { 59 t.Errorf("Unexpected signal: %s", sig.String()) 60 } 61 outbound <- s 62 } 63 64 func TestNewSocketServer(t *testing.T) { 65 const mockSocket = "socket_addr:socket_port" 66 cfg := new(config.Configuration) 67 cfg.UnixSocketName = mockSocket 68 69 mockServer := &http.Server{ 70 Addr: cfg.UnixSocketName, 71 Handler: nil, 72 ReadTimeout: 15 * time.Second, 73 WriteTimeout: 15 * time.Second, 74 } 75 76 ret := newSocketServer(cfg, nil) 77 require.NotNil(t, ret, "ret : isNil()") 78 79 assert.Equal(t, mockServer.Addr, ret.Addr, fmt.Sprintf("Addr invalide: %v != %v", 80 ret.Addr, mockServer.Addr)) 81 assert.Equal(t, mockServer.ReadTimeout, ret.ReadTimeout, fmt.Sprintf("ReadTimeout invalide: %v != %v", 82 ret.ReadTimeout, mockServer.ReadTimeout)) 83 assert.Equal(t, mockServer.WriteTimeout, ret.WriteTimeout, fmt.Sprintf("WriteTimeout invalide: %v != %v", 84 ret.WriteTimeout, mockServer.WriteTimeout)) 85 86 ret.Close() 87 } 88 89 func TestNewMainServer(t *testing.T) { 90 const ( 91 mockPort = 8000 // chose your socket_port 92 mockAddress = "prebid.com" // chose your socket_address 93 ) 94 cfg := new(config.Configuration) 95 cfg.Port = mockPort 96 cfg.Host = mockAddress 97 98 mockServer := &http.Server{ 99 Addr: fmt.Sprintf("%s:%d", mockAddress, mockPort), 100 Handler: nil, 101 ReadTimeout: 15 * time.Second, 102 WriteTimeout: 15 * time.Second, 103 } 104 105 ret := newMainServer(cfg, nil) 106 require.NotNil(t, ret, "ret : isNil()") 107 108 assert.Equal(t, ret.Addr, mockServer.Addr, fmt.Sprintf("Addr invalide: %v != %v", 109 ret.Addr, mockServer.Addr)) 110 assert.Equal(t, ret.ReadTimeout, mockServer.ReadTimeout, 111 fmt.Sprintf("ReadTimeout invalide: %v != %v", ret.ReadTimeout, mockServer.ReadTimeout)) 112 assert.Equal(t, ret.WriteTimeout, mockServer.WriteTimeout, 113 fmt.Sprintf("WriteTimeout invalide: %v != %v", ret.WriteTimeout, mockServer.WriteTimeout)) 114 115 ret.Close() 116 } 117 118 func TestNewTCPListener(t *testing.T) { 119 const mockAddress = ":8000" //:chose your socket_port 120 121 ret, err := newTCPListener(mockAddress, nil) 122 assert.Equal(t, nil, err, fmt.Sprintf("err_ : %v", err)) 123 assert.NotEqual(t, nil, ret, "ret : isNil()") 124 125 if ret != nil { 126 ret.Close() 127 } 128 } 129 130 func TestNewUnixListener(t *testing.T) { 131 const mockFile = "file_referer" // chose your file_referer 132 133 ret, err := newUnixListener(mockFile, nil) 134 assert.Equal(t, nil, err, "err_ : NOT-Nil()") 135 assert.NotEqual(t, nil, ret, "ret : isNil()") 136 137 if ret != nil { 138 ret.Close() 139 } 140 } 141 142 func TestNewAdminServer(t *testing.T) { 143 const ( 144 mockHost = "prebid.com" // chose your host 145 mockAdmin = 6060 // chose your admin_port 146 ) 147 cfg := new(config.Configuration) 148 cfg.Host = mockHost 149 cfg.AdminPort = mockAdmin 150 151 mockServer := &http.Server{ 152 Addr: cfg.Host + ":" + strconv.Itoa(cfg.AdminPort), 153 Handler: nil, 154 } 155 156 ret := newAdminServer(cfg, nil) 157 require.NotNil(t, ret, "ret : isNil()") 158 assert.Equal(t, mockServer.Addr, ret.Addr, fmt.Sprintf("Addr invalide: %v != %v", 159 ret.Addr, mockServer.Addr)) 160 161 ret.Close() 162 } 163 164 func TestRunServer(t *testing.T) { 165 const mockName = "mockServer_name" 166 167 err := runServer(nil, mockName, nil) 168 assert.NotEqual(t, nil, err, "runServer(nil, 'mockName', nil) : didn't trigger any error.") 169 170 s := http.Server{} 171 err = runServer(&s, mockName, nil) 172 assert.NotEqual(t, nil, err, "runServer(not_nil, 'mockName', nil) : didn't trigger any error.") 173 174 var l net.Listener 175 l, _ = net.Listen("error", ":8000") 176 err = runServer(&s, mockName, l) 177 assert.NotEqual(t, nil, err, "Listen('error', ':8000') : didn't trigger any error.") 178 } 179 180 func TestListen(t *testing.T) { 181 var ( 182 handler, adminHandler http.Handler 183 184 metrics = new(metricsconfig.DetailedMetricsEngine) 185 cfg = &config.Configuration{ 186 Host: "prebid.com", 187 AdminPort: 6060, 188 Port: 8000, 189 UnixSocketEnable: false, 190 UnixSocketName: "prebid_socket", 191 } 192 ) 193 194 err := Listen(cfg, handler, adminHandler, metrics) 195 assert.NotEqual(t, nil, err, "err : isNil()") 196 }