github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/server/server_test.go (about)

     1  /*
     2  Copyright 2018 The OpenEBS Author
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package server
    18  
    19  import (
    20  	"net"
    21  	"net/http"
    22  	"testing"
    23  )
    24  
    25  func TestStartHttpServer(t *testing.T) {
    26  
    27  	s := Server{
    28  		ListenPort:  ":9090",
    29  		MetricsPath: "/metrics",
    30  		Handler:     http.HandlerFunc(index),
    31  	}
    32  	ErrorMessages := make(chan error)
    33  	go func() {
    34  		//Block port 9090 and attempt to start http server at 9090.
    35  		if p1, err := net.Listen("tcp", "localhost:9090"); err == nil {
    36  			defer p1.Close()
    37  		}
    38  		ErrorMessages <- s.Start()
    39  	}()
    40  	msg := <-ErrorMessages
    41  	if msg != nil {
    42  		t.Log("Trying to start http server in a port which is busy.")
    43  		t.Log(msg)
    44  	}
    45  }
    46  
    47  func index(w http.ResponseWriter, r *http.Request) {
    48  	// sample handler created for testing
    49  }