go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/internal/test/addrs.go (about)

     1  // Copyright 2019 The Mangos Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use file except in compliance with the License.
     5  // You may obtain a copy of the license at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package test
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  	"sync/atomic"
    22  	"time"
    23  )
    24  
    25  var currPort uint32
    26  
    27  func init() {
    28  	currPort = uint32(time.Now().UnixNano()%20000 + 20000)
    29  }
    30  
    31  // NextPort returns the next port, incrementing by one.
    32  func NextPort() uint32 {
    33  	return atomic.AddUint32(&currPort, 1)
    34  }
    35  
    36  // AddrTestIPC returns a test IPC address.  It will be in the temporary
    37  // directory.
    38  func AddrTestIPC() string {
    39  	temp := filepath.ToSlash(os.TempDir())
    40  	return fmt.Sprintf("ipc://%s/mangostest%d", temp, NextPort())
    41  }
    42  
    43  // AddrTestWSS returns a websocket over TLS address.
    44  func AddrTestWSS() string {
    45  	return (fmt.Sprintf("wss://127.0.0.1:%d/", NextPort()))
    46  }
    47  
    48  // AddrTestWS returns a websocket address.
    49  func AddrTestWS() string {
    50  	return (fmt.Sprintf("ws://127.0.0.1:%d/", NextPort()))
    51  }
    52  
    53  // AddrTestTCP returns a TCP address.
    54  func AddrTestTCP() string {
    55  	return (fmt.Sprintf("tcp://127.0.0.1:%d", NextPort()))
    56  }
    57  
    58  // AddrTestTLS returns a TLS over TCP address.
    59  func AddrTestTLS() string {
    60  	return (fmt.Sprintf("tls+tcp://127.0.0.1:%d", NextPort()))
    61  }
    62  
    63  // AddrTestInp returns an inproc address.
    64  func AddrTestInp() string {
    65  	return (fmt.Sprintf("inproc://test_%d", NextPort()))
    66  }