github.com/sereiner/library@v0.0.0-20200518095232-1fa3e640cc5f/net/net_test.go (about)

     1  package net
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestIsTCPPortAvailable(t *testing.T) {
     8  	if IsTCPPortAvailable(443) {
     9  		t.Error("IsTCPPortAvailable is fail")
    10  	}
    11  
    12  	if !IsTCPPortAvailable(6666) {
    13  		t.Error("IsTCPPortAvailable is fail")
    14  	}
    15  }
    16  
    17  func TestRandomTCPPort(t *testing.T) {
    18  	if RandomTCPPort() == -1 {
    19  		t.Error("RandomTCPPort is fail")
    20  	}
    21  }
    22  
    23  func TestGetAvailablePort(t *testing.T) {
    24  	ports := []int{443, 6666}
    25  	if GetAvailablePort(ports) == -1 || GetAvailablePort(ports) != 6666 {
    26  		t.Error("GetAvailablePort is fail")
    27  	}
    28  
    29  	ports = []int{443, 22}
    30  	if GetAvailablePort(ports) != -1 {
    31  		t.Error("GetAvailablePort is fail")
    32  	}
    33  }