nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/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  	"sync/atomic"
    20  	"time"
    21  )
    22  
    23  var currPort uint32
    24  
    25  func init() {
    26  	currPort = uint32(time.Now().UnixNano()%20000 + 20000)
    27  }
    28  
    29  // NextPort returns the next port, incrementing by one.
    30  func NextPort() uint32 {
    31  	return atomic.AddUint32(&currPort, 1)
    32  }
    33  
    34  // AddrTestIPC returns a test IPC address.  It will be in the current
    35  // directory.
    36  func AddrTestIPC() string {
    37  	return (fmt.Sprintf("ipc://mangostest%d", NextPort()))
    38  }
    39  
    40  // AddrTestWSS returns a websocket over TLS address.
    41  func AddrTestWSS() string {
    42  	return (fmt.Sprintf("wss://127.0.0.1:%d/", NextPort()))
    43  }
    44  
    45  // AddrTestWS returns a websocket address.
    46  func AddrTestWS() string {
    47  	return (fmt.Sprintf("ws://127.0.0.1:%d/", NextPort()))
    48  }
    49  
    50  // AddrTestTCP returns a TCP address.
    51  func AddrTestTCP() string {
    52  	return (fmt.Sprintf("tcp://127.0.0.1:%d", NextPort()))
    53  }
    54  
    55  // AddrTestTLS returns a TLS over TCP address.
    56  func AddrTestTLS() string {
    57  	return (fmt.Sprintf("tls+tcp://127.0.0.1:%d", NextPort()))
    58  }
    59  
    60  // AddrTestInp returns an inproc address.
    61  func AddrTestInp() string {
    62  	return (fmt.Sprintf("inproc://test_%d", NextPort()))
    63  }