From cbca8e7967b3b76214659c8dc37de6d4eb9f875d Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 9 Nov 2015 15:47:08 -0800 Subject: [PATCH] fixes #155 Numerous golint issues --- compat/compat.go | 2 + core.go | 2 +- errors.go | 5 +- examples/pair/pair.go | 14 +++--- macat/macat.go | 8 ++-- options.go | 6 +-- perf/main.go | 4 +- perf/throughput.go | 2 +- port.go | 1 + properties.go | 8 ++-- protocol.go | 9 ++-- protocol/pull/pull.go | 2 +- protocol/rep/rep.go | 4 +- protocol/req/req.go | 2 +- protocol/respondent/respondent.go | 2 +- protocol/surveyor/surveyor.go | 2 +- test/benchmark_test.go | 8 ++-- test/certs.go | 12 ++--- test/certs_test.go | 2 +- test/common_test.go | 8 ++-- test/device_test.go | 2 +- test/transport.go | 80 ++++++++++++++++++++----------- test/ttl_test.go | 26 +++++----- transport.go | 2 + transport/ipc/ipc.go | 12 ++--- transport/tcp/tcp.go | 6 +-- transport/tcp/tcp_test.go | 24 +++++----- transport/tlstcp/tlstcp.go | 20 ++++---- transport/ws/ws.go | 48 +++++++++---------- transport/ws/ws_test.go | 8 ++-- transport/wss/wss.go | 2 +- util.go | 5 +- waiter.go | 3 +- 33 files changed, 188 insertions(+), 153 deletions(-) diff --git a/compat/compat.go b/compat/compat.go index 11d3e11..5ce944b 100644 --- a/compat/compat.go +++ b/compat/compat.go @@ -56,6 +56,7 @@ import ( // either normal or raw mode sockets. type Domain int +// Constants for socket type. const ( AF_SP = Domain(0) AF_SP_RAW = Domain(1) @@ -65,6 +66,7 @@ const ( // that Mangos supports. type Protocol int +// Constants for protocols. const ( PUSH = Protocol(mangos.ProtoPush) PULL = Protocol(mangos.ProtoPull) diff --git a/core.go b/core.go index 474507f..295bced 100644 --- a/core.go +++ b/core.go @@ -135,7 +135,7 @@ func newSocket(proto Protocol) *socket { // MakeSocket is intended for use by Protocol implementations. The intention // is that they can wrap this to provide a "proto.NewSocket()" implementation. -func MakeSocket(proto Protocol) *socket { +func MakeSocket(proto Protocol) Socket { return newSocket(proto) } diff --git a/errors.go b/errors.go index ffa7c73..07c64e9 100644 --- a/errors.go +++ b/errors.go @@ -18,6 +18,7 @@ import ( "errors" ) +// Various error codes. var ( ErrBadAddr = errors.New("invalid address") ErrBadHeader = errors.New("invalid header received") @@ -39,6 +40,6 @@ var ( ErrGarbled = errors.New("message garbled") ErrAddrInUse = errors.New("address in use") ErrBadProperty = errors.New("invalid property name") - ErrTlsNoConfig = errors.New("missing TLS configuration") - ErrTlsNoCert = errors.New("missing TLS certificates") + ErrTLSNoConfig = errors.New("missing TLS configuration") + ErrTLSNoCert = errors.New("missing TLS certificates") ) diff --git a/examples/pair/pair.go b/examples/pair/pair.go index a668e50..9ed9ace 100644 --- a/examples/pair/pair.go +++ b/examples/pair/pair.go @@ -42,14 +42,14 @@ func die(format string, v ...interface{}) { os.Exit(1) } -func send_name(sock mangos.Socket, name string) { +func sendName(sock mangos.Socket, name string) { fmt.Printf("%s: SENDING \"%s\"\n", name, name) if err := sock.Send([]byte(name)); err != nil { die("failed sending: %s", err) } } -func recv_name(sock mangos.Socket, name string) { +func recvName(sock mangos.Socket, name string) { var msg []byte var err error if msg, err = sock.Recv(); err == nil { @@ -57,12 +57,12 @@ func recv_name(sock mangos.Socket, name string) { } } -func send_recv(sock mangos.Socket, name string) { +func sendRecv(sock mangos.Socket, name string) { for { sock.SetOption(mangos.OptionRecvDeadline, 100*time.Millisecond) - recv_name(sock, name) + recvName(sock, name) time.Sleep(time.Second) - send_name(sock, name) + sendName(sock, name) } } @@ -77,7 +77,7 @@ func node0(url string) { if err = sock.Listen(url); err != nil { die("can't listen on pair socket: %s", err.Error()) } - send_recv(sock, "node0") + sendRecv(sock, "node0") } func node1(url string) { @@ -92,7 +92,7 @@ func node1(url string) { if err = sock.Dial(url); err != nil { die("can't dial on pair socket: %s", err.Error()) } - send_recv(sock, "node1") + sendRecv(sock, "node1") } func main() { diff --git a/macat/macat.go b/macat/macat.go index 0197aaa..12761f9 100644 --- a/macat/macat.go +++ b/macat/macat.go @@ -52,9 +52,9 @@ var proto string var dialAddrs []string var listenAddrs []string var subscriptions []string -var recvTimeout int = -1 -var sendTimeout int = -1 -var sendInterval int = -1 +var recvTimeout = -1 +var sendTimeout = -1 +var sendInterval = -1 var sendDelay int var sendData []byte var printFormat string @@ -553,7 +553,7 @@ func main() { fatalf("Protocol not specified.") } - sock.SetOption(mangos.OptionTlsConfig, &tlscfg) + sock.SetOption(mangos.OptionTLSConfig, &tlscfg) if len(listenAddrs) == 0 && len(dialAddrs) == 0 { fatalf("No address specified.") diff --git a/options.go b/options.go index bc928cb..31be6af 100644 --- a/options.go +++ b/options.go @@ -74,9 +74,9 @@ const ( // indicate an infinite time. Default is 1 second. OptionSurveyTime = "SURVEY-TIME" - // OptionTlsConfig is used to supply TLS configuration details. + // OptionTLSConfig is used to supply TLS configuration details. // The parameter is a tls.Config pointer. - OptionTlsConfig = "TLS-CONFIG" + OptionTLSConfig = "TLS-CONFIG" // OptionWriteQLen is used to set the size, in messages, of the write // queue channel. By default, it's 128. This option cannot be set if @@ -109,7 +109,7 @@ const ( // those that do, if a message traverses more than this many devices, // it will be dropped. This is used to provide protection against // loops in the topology. The default is protocol specific. - OptionTtl = "TTL" + OptionTTL = "TTL" // OptionMaxRecvSize supplies the maximum receive size for inbound // messages. This option exists because the wire protocol allows diff --git a/perf/main.go b/perf/main.go index 2ea4361..4f9498b 100644 --- a/perf/main.go +++ b/perf/main.go @@ -25,7 +25,7 @@ import ( "strconv" ) -func Usage() { +func usage() { fmt.Printf("Bad Usage!\n") os.Exit(1) } @@ -170,5 +170,5 @@ func main() { args = args[1:] } } - Usage() + usage() } diff --git a/perf/throughput.go b/perf/throughput.go index 377016b..ac92047 100644 --- a/perf/throughput.go +++ b/perf/throughput.go @@ -26,7 +26,7 @@ import ( "time" ) -// LatencyServer is the server side -- very much equivalent to local_thr in +// ThroughputServer is the server side -- very much equivalent to local_thr in // nanomsg/perf. It does the measurement by counting packets received. func ThroughputServer(addr string, msgSize int, count int) { s, err := pair.NewSocket() diff --git a/port.go b/port.go index 6091b10..e121b19 100644 --- a/port.go +++ b/port.go @@ -58,6 +58,7 @@ type Port interface { // PortAction determines whether the action on a Port is addition or removal. type PortAction int +// PortAction values. const ( PortActionAdd = iota PortActionRemove diff --git a/properties.go b/properties.go index 2001868..a687463 100644 --- a/properties.go +++ b/properties.go @@ -27,11 +27,11 @@ const ( // end dialer. The value is a net.Addr. PropRemoteAddr = "REMOTE-ADDR" - // PropTlsConnState is used to supply TLS connection details. The + // PropTLSConnState is used to supply TLS connection details. The // value is a tls.ConnectionState. It is only valid when TLS is used. - PropTlsConnState = "TLS-STATE" + PropTLSConnState = "TLS-STATE" - // PropHttpRequest conveys an *http.Request. This property only exists + // PropHTTPRequest conveys an *http.Request. This property only exists // for websocket connections. - PropHttpRequest = "HTTP-REQUEST" + PropHTTPRequest = "HTTP-REQUEST" ) diff --git a/protocol.go b/protocol.go index 32d1e16..0943e5c 100644 --- a/protocol.go +++ b/protocol.go @@ -171,6 +171,9 @@ const ( ProtoStar = (100 * 16) ) +// ProtocolName returns the name corresponding to a given protocol number. +// This is useful for transports like WebSocket, which use a text name +// rather than the number in the handshake. func ProtocolName(number uint16) string { names := map[uint16]string{ ProtoPair: "pair", @@ -206,10 +209,10 @@ func ValidPeers(p1, p2 Protocol) bool { // have no data to send. func NullRecv(ep Endpoint) { for { - if m := ep.RecvMsg(); m == nil { + var m *Message + if m = ep.RecvMsg(); m == nil { return - } else { - m.Free() } + m.Free() } } diff --git a/protocol/pull/pull.go b/protocol/pull/pull.go index 44d2943..0025cc2 100644 --- a/protocol/pull/pull.go +++ b/protocol/pull/pull.go @@ -100,7 +100,7 @@ func (x *pull) GetOption(name string) (interface{}, error) { } } -// NewProtocol() allocates a new PULL protocol object. +// NewProtocol allocates a new PULL protocol object. func NewProtocol() mangos.Protocol { return &pull{} } diff --git a/protocol/rep/rep.go b/protocol/rep/rep.go index 4c8fefa..b822e02 100644 --- a/protocol/rep/rep.go +++ b/protocol/rep/rep.go @@ -264,7 +264,7 @@ func (r *rep) SetOption(name string, v interface{}) error { r.sock.SetSendError(mangos.ErrProtoState) } return nil - case mangos.OptionTtl: + case mangos.OptionTTL: if ttl, ok := v.(int); !ok { return mangos.ErrBadValue } else if ttl < 1 || ttl > 255 { @@ -282,7 +282,7 @@ func (r *rep) GetOption(name string) (interface{}, error) { switch name { case mangos.OptionRaw: return r.raw, nil - case mangos.OptionTtl: + case mangos.OptionTTL: return r.ttl, nil default: return nil, mangos.ErrBadOption diff --git a/protocol/req/req.go b/protocol/req/req.go index ee14836..47db8d5 100644 --- a/protocol/req/req.go +++ b/protocol/req/req.go @@ -298,7 +298,7 @@ func (r *req) GetOption(option string) (interface{}, error) { } } -// NewReq returns a new REQ protocol object. +// NewProtocol returns a new REQ protocol object. func NewProtocol() mangos.Protocol { return &req{} } diff --git a/protocol/respondent/respondent.go b/protocol/respondent/respondent.go index dbb4b4f..9ef51f2 100644 --- a/protocol/respondent/respondent.go +++ b/protocol/respondent/respondent.go @@ -258,7 +258,7 @@ func (x *resp) SetOption(name string, v interface{}) error { x.sock.SetSendError(mangos.ErrProtoState) } return nil - case mangos.OptionTtl: + case mangos.OptionTTL: if ttl, ok := v.(int); !ok { return mangos.ErrBadValue } else if ttl < 1 || ttl > 255 { diff --git a/protocol/surveyor/surveyor.go b/protocol/surveyor/surveyor.go index ad0d59f..c1cd9bf 100644 --- a/protocol/surveyor/surveyor.go +++ b/protocol/surveyor/surveyor.go @@ -263,7 +263,7 @@ func (x *surveyor) GetOption(name string) (interface{}, error) { } // NewProtocol returns a new SURVEYOR protocol object. -func NewSurveyor() mangos.Protocol { +func NewProtocol() mangos.Protocol { return &surveyor{} } diff --git a/test/benchmark_test.go b/test/benchmark_test.go index 3c32d10..07008fe 100644 --- a/test/benchmark_test.go +++ b/test/benchmark_test.go @@ -38,8 +38,8 @@ func benchmarkReq(t *testing.B, url string, size int) { cliopts := make(map[string]interface{}) if strings.HasPrefix(url, "wss://") || strings.HasPrefix(url, "tls+tcp://") { - srvopts[mangos.OptionTlsConfig] = srvCfg - cliopts[mangos.OptionTlsConfig] = cliCfg + srvopts[mangos.OptionTLSConfig] = srvCfg + cliopts[mangos.OptionTLSConfig] = cliCfg } srvrdy := make(chan struct{}) srvsock, err := rep.NewSocket() @@ -120,8 +120,8 @@ func benchmarkPair(t *testing.B, url string, size int) { cliopts := make(map[string]interface{}) if strings.HasPrefix(url, "wss://") || strings.HasPrefix(url, "tls+tcp://") { - srvopts[mangos.OptionTlsConfig] = srvCfg - cliopts[mangos.OptionTlsConfig] = cliCfg + srvopts[mangos.OptionTLSConfig] = srvCfg + cliopts[mangos.OptionTLSConfig] = cliCfg } finish := make(chan struct{}) diff --git a/test/certs.go b/test/certs.go index 0c52866..ab86c63 100644 --- a/test/certs.go +++ b/test/certs.go @@ -164,9 +164,9 @@ var clientTmpl = &x509.Certificate{ ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, } -// NewTlsConfig creates a suitable TLS configuration, using +// NewTLSConfig creates a suitable TLS configuration, using // either a server or client. A self-signed CA Cert is included. -func NewTlsConfig(server bool) (*tls.Config, error) { +func NewTLSConfig(server bool) (*tls.Config, error) { cfg := &tls.Config{} keys, err := newKeys() @@ -187,20 +187,20 @@ var lock sync.Mutex var clientConfig *tls.Config var serverConfig *tls.Config -// GetTlsConfig is like NewTlsConfig, but it caches to avoid regenerating +// GetTLSConfig is like NewTLSConfig, but it caches to avoid regenerating // key material pointlessly. -func GetTlsConfig(server bool) (*tls.Config, error) { +func GetTLSConfig(server bool) (*tls.Config, error) { var err error var cfg *tls.Config lock.Lock() if server { if cfg = serverConfig; cfg == nil { - cfg, err = NewTlsConfig(true) + cfg, err = NewTLSConfig(true) serverConfig = cfg } } else { if cfg = clientConfig; cfg == nil { - cfg, err = NewTlsConfig(false) + cfg, err = NewTLSConfig(false) clientConfig = cfg } } diff --git a/test/certs_test.go b/test/certs_test.go index 38a3539..a9c80a5 100644 --- a/test/certs_test.go +++ b/test/certs_test.go @@ -54,7 +54,7 @@ func TestNewKeys(t *testing.T) { func TestNewTLSConfig(t *testing.T) { t.Logf("Creating TLS config") - cfg, err := NewTlsConfig(true) + cfg, err := NewTLSConfig(true) if err != nil { t.Errorf("Failed generation: %v", err) return diff --git a/test/common_test.go b/test/common_test.go index 7ab5a2a..dd8e172 100644 --- a/test/common_test.go +++ b/test/common_test.go @@ -34,8 +34,8 @@ import ( var protoReq = req.NewProtocol() var protoRep = rep.NewProtocol() -var cliCfg, _ = NewTlsConfig(false) -var srvCfg, _ = NewTlsConfig(true) +var cliCfg, _ = NewTLSConfig(false) +var srvCfg, _ = NewTLSConfig(true) // T is a structure that subtests can inherit from. type T struct { @@ -236,7 +236,7 @@ func (c *T) Dial() bool { case strings.HasPrefix(c.addr, "tls+tcp://"): fallthrough case strings.HasPrefix(c.addr, "wss://"): - options[mangos.OptionTlsConfig] = cliCfg + options[mangos.OptionTLSConfig] = cliCfg } err := c.Sock.DialOptions(c.addr, options) @@ -255,7 +255,7 @@ func (c *T) Listen() bool { case strings.HasPrefix(c.addr, "tls+tcp://"): fallthrough case strings.HasPrefix(c.addr, "wss://"): - options[mangos.OptionTlsConfig] = srvCfg + options[mangos.OptionTLSConfig] = srvCfg } err := c.Sock.ListenOptions(c.addr, options) if err != nil { diff --git a/test/device_test.go b/test/device_test.go index 448194a..0691e44 100644 --- a/test/device_test.go +++ b/test/device_test.go @@ -210,7 +210,7 @@ func testDevLoop(t *testing.T, addr string) { options := make(map[string]interface{}) if strings.HasPrefix(addr, "wss://") || strings.HasPrefix(addr, "tls+tcp://") { - options[mangos.OptionTlsConfig] = srvCfg + options[mangos.OptionTLSConfig] = srvCfg } if err := s1.ListenOptions(addr, options); err != nil { diff --git a/test/transport.go b/test/transport.go index 9c7a2b7..0b2e11b 100644 --- a/test/transport.go +++ b/test/transport.go @@ -28,6 +28,8 @@ import ( "github.com/gdamore/mangos/protocol/req" ) +// TranTest provides a common test structure for transports, so that they +// can implement a battery of standard tests. type TranTest struct { addr string tran mangos.Transport @@ -37,18 +39,21 @@ type TranTest struct { sockReq mangos.Socket } +// NewTranTest creates a TranTest. func NewTranTest(tran mangos.Transport, addr string) *TranTest { tt := &TranTest{addr: addr, tran: tran} if strings.HasPrefix(tt.addr, "tls+tcp://") || strings.HasPrefix(tt.addr, "wss://") { - tt.cliCfg, _ = GetTlsConfig(false) - tt.srvCfg, _ = GetTlsConfig(true) + tt.cliCfg, _ = GetTLSConfig(false) + tt.srvCfg, _ = GetTLSConfig(true) } tt.sockRep, _ = rep.NewSocket() tt.sockReq, _ = req.NewSocket() return tt } -func (tt *TranTest) TranTestListenAndAccept(t *testing.T) { +// TestListenAndAccept tests that we can both listen and accept connections +// for the given transport. +func (tt *TranTest) TestListenAndAccept(t *testing.T) { t.Logf("Establishing listener for %s", tt.addr) l, err := tt.tran.NewListener(tt.addr, tt.sockRep) if err != nil { @@ -57,7 +62,7 @@ func (tt *TranTest) TranTestListenAndAccept(t *testing.T) { } defer l.Close() if tt.srvCfg != nil { - if err = l.SetOption(mangos.OptionTlsConfig, tt.srvCfg); err != nil { + if err = l.SetOption(mangos.OptionTLSConfig, tt.srvCfg); err != nil { t.Errorf("Failed setting TLS config: %v", err) return } @@ -79,7 +84,7 @@ func (tt *TranTest) TranTestListenAndAccept(t *testing.T) { return } if tt.cliCfg != nil { - if err = d.SetOption(mangos.OptionTlsConfig, tt.cliCfg); err != nil { + if err = d.SetOption(mangos.OptionTLSConfig, tt.cliCfg); err != nil { t.Errorf("Failed setting TLS config: %v", err) return } @@ -132,7 +137,10 @@ func (tt *TranTest) TranTestListenAndAccept(t *testing.T) { wg.Wait() } -func (tt *TranTest) TranTestDuplicateListen(t *testing.T) { +// TestDuplicateListen checks to make sure that an attempt to listen +// on a second socket, when another listener is already present, properly +// fails with ErrAddrInUse. +func (tt *TranTest) TestDuplicateListen(t *testing.T) { var err error time.Sleep(100 * time.Millisecond) t.Logf("Testing Duplicate Listen on %s", tt.addr) @@ -143,7 +151,7 @@ func (tt *TranTest) TranTestDuplicateListen(t *testing.T) { } defer l1.Close() if tt.srvCfg != nil { - if err = l1.SetOption(mangos.OptionTlsConfig, tt.srvCfg); err != nil { + if err = l1.SetOption(mangos.OptionTLSConfig, tt.srvCfg); err != nil { t.Errorf("Failed setting TLS config: %v", err) return } @@ -160,7 +168,7 @@ func (tt *TranTest) TranTestDuplicateListen(t *testing.T) { } defer l2.Close() if tt.srvCfg != nil { - if err = l2.SetOption(mangos.OptionTlsConfig, tt.srvCfg); err != nil { + if err = l2.SetOption(mangos.OptionTLSConfig, tt.srvCfg); err != nil { t.Errorf("Failed setting TLS config: %v", err) return } @@ -172,13 +180,15 @@ func (tt *TranTest) TranTestDuplicateListen(t *testing.T) { t.Logf("Got expected error: %v", err) } -func (tt *TranTest) TranTestConnRefused(t *testing.T) { +// TestConnRefused tests that attempts to dial to an address without a listener +// properly fail with EConnRefused. +func (tt *TranTest) TestConnRefused(t *testing.T) { d, err := tt.tran.NewDialer(tt.addr, tt.sockReq) if err != nil || d == nil { t.Errorf("New Dialer failed: %v", err) } if tt.cliCfg != nil { - if err = d.SetOption(mangos.OptionTlsConfig, tt.cliCfg); err != nil { + if err = d.SetOption(mangos.OptionTLSConfig, tt.cliCfg); err != nil { t.Errorf("Failed setting TLS config: %v", err) return } @@ -191,7 +201,9 @@ func (tt *TranTest) TranTestConnRefused(t *testing.T) { t.Logf("Got expected error: %v", err) } -func (tt *TranTest) TranTestSendRecv(t *testing.T) { +// TestSendRecv test that the transport can send and receive. It uses the +// REQ/REP protocol for messages. +func (tt *TranTest) TestSendRecv(t *testing.T) { ping := []byte("REQUEST_MESSAGE") ack := []byte("RESPONSE_MESSAGE") @@ -205,7 +217,7 @@ func (tt *TranTest) TranTestSendRecv(t *testing.T) { } defer l.Close() if tt.srvCfg != nil { - if err = l.SetOption(mangos.OptionTlsConfig, tt.srvCfg); err != nil { + if err = l.SetOption(mangos.OptionTLSConfig, tt.srvCfg); err != nil { t.Errorf("Failed setting TLS config: %v", err) return } @@ -222,7 +234,7 @@ func (tt *TranTest) TranTestSendRecv(t *testing.T) { t.Logf("Connecting REQ on %s", tt.addr) d, err := tt.tran.NewDialer(tt.addr, tt.sockReq) if tt.cliCfg != nil { - if err = d.SetOption(mangos.OptionTlsConfig, tt.cliCfg); err != nil { + if err = d.SetOption(mangos.OptionTLSConfig, tt.cliCfg); err != nil { t.Errorf("Failed setting TLS config: %v", err) return } @@ -324,7 +336,8 @@ func (tt *TranTest) TranTestSendRecv(t *testing.T) { } } -func (tt *TranTest) TranTestScheme(t *testing.T) { +// TestScheme tests the Scheme() entry point on the transport. +func (tt *TranTest) TestScheme(t *testing.T) { scheme := tt.tran.Scheme() t.Log("Checking scheme") if !strings.HasPrefix(tt.addr, scheme+"://") { @@ -334,7 +347,8 @@ func (tt *TranTest) TranTestScheme(t *testing.T) { t.Log("Scheme match") } -func (tt *TranTest) TranTestListenerSetOptionInvalid(t *testing.T) { +// TestListenerSetOptionInvalid tests passing invalid options to a listener. +func (tt *TranTest) TestListenerSetOptionInvalid(t *testing.T) { t.Log("Trying invalid listener SetOption") l, err := tt.tran.NewListener(tt.addr, tt.sockRep) if err != nil { @@ -352,7 +366,9 @@ func (tt *TranTest) TranTestListenerSetOptionInvalid(t *testing.T) { } } -func (tt *TranTest) TranTestListenerGetOptionInvalid(t *testing.T) { +// TestListenerGetOptionInvalid tests trying to get an invalid option on +// a listener. +func (tt *TranTest) TestListenerGetOptionInvalid(t *testing.T) { t.Log("Trying invalid listener GetOption") l, err := tt.tran.NewListener(tt.addr, tt.sockRep) if err != nil { @@ -370,7 +386,8 @@ func (tt *TranTest) TranTestListenerGetOptionInvalid(t *testing.T) { } } -func (tt *TranTest) TranTestDialerSetOptionInvalid(t *testing.T) { +// TestDialerSetOptionInvalid tests trying to set an invalid option on a Dialer. +func (tt *TranTest) TestDialerSetOptionInvalid(t *testing.T) { t.Log("Trying invalid dialer SetOption") d, err := tt.tran.NewDialer(tt.addr, tt.sockRep) if err != nil { @@ -388,7 +405,9 @@ func (tt *TranTest) TranTestDialerSetOptionInvalid(t *testing.T) { } } -func (tt *TranTest) TranTestDialerGetOptionInvalid(t *testing.T) { +// TestDialerGetOptionInvalid tests attempting to get an invalid option on +// a Dialer. +func (tt *TranTest) TestDialerGetOptionInvalid(t *testing.T) { t.Log("Trying invalid listener GetOption") d, err := tt.tran.NewDialer(tt.addr, tt.sockRep) if err != nil { @@ -406,6 +425,8 @@ func (tt *TranTest) TranTestDialerGetOptionInvalid(t *testing.T) { } } +// TestDialerBadScheme tests to makes sure that giving a bogus scheme +// to create a dialer fails properly. func (tt *TranTest) TestDialerBadScheme(t *testing.T) { t.Logf("NewDialer with bogus scheme") d, err := tt.tran.NewDialer("bogus://address", tt.sockRep) @@ -418,6 +439,8 @@ func (tt *TranTest) TestDialerBadScheme(t *testing.T) { } } +// TestListenerBadScheme tests to makes sure that giving a bogus scheme +// to create a listener fails properly. func (tt *TranTest) TestListenerBadScheme(t *testing.T) { t.Logf("NewListener with bogus scheme") d, err := tt.tran.NewListener("bogus://address", tt.sockRep) @@ -430,16 +453,17 @@ func (tt *TranTest) TestListenerBadScheme(t *testing.T) { } } -func (tt *TranTest) TranTestAll(t *testing.T) { - tt.TranTestScheme(t) - tt.TranTestListenAndAccept(t) - tt.TranTestConnRefused(t) - tt.TranTestDuplicateListen(t) - tt.TranTestSendRecv(t) - tt.TranTestDialerSetOptionInvalid(t) - tt.TranTestDialerGetOptionInvalid(t) - tt.TranTestListenerSetOptionInvalid(t) - tt.TranTestListenerGetOptionInvalid(t) +// TestAll runs a full battery of standard tests on the transport. +func (tt *TranTest) TestAll(t *testing.T) { + tt.TestScheme(t) + tt.TestListenAndAccept(t) + tt.TestConnRefused(t) + tt.TestDuplicateListen(t) + tt.TestSendRecv(t) + tt.TestDialerSetOptionInvalid(t) + tt.TestDialerGetOptionInvalid(t) + tt.TestListenerSetOptionInvalid(t) + tt.TestListenerGetOptionInvalid(t) tt.TestDialerBadScheme(t) tt.TestListenerBadScheme(t) } diff --git a/test/ttl_test.go b/test/ttl_test.go index 681c86b..241d3fd 100644 --- a/test/ttl_test.go +++ b/test/ttl_test.go @@ -26,7 +26,7 @@ import ( "github.com/gdamore/mangos/transport/inproc" ) -func TestTtlInvalidZero(t *testing.T) { +func TestTTLInvalidZero(t *testing.T) { srep, err := rep.NewSocket() if err != nil { t.Errorf("Failed to make REP: %v", err) @@ -34,7 +34,7 @@ func TestTtlInvalidZero(t *testing.T) { } defer srep.Close() - err = srep.SetOption(mangos.OptionTtl, 0) + err = srep.SetOption(mangos.OptionTTL, 0) switch err { case mangos.ErrBadValue: // expected result case nil: @@ -44,7 +44,7 @@ func TestTtlInvalidZero(t *testing.T) { } } -func TestTtlInvalidNegative(t *testing.T) { +func TestTTLInvalidNegative(t *testing.T) { srep, err := rep.NewSocket() if err != nil { t.Errorf("Failed to make REP: %v", err) @@ -52,7 +52,7 @@ func TestTtlInvalidNegative(t *testing.T) { } defer srep.Close() - err = srep.SetOption(mangos.OptionTtl, -1) + err = srep.SetOption(mangos.OptionTTL, -1) switch err { case mangos.ErrBadValue: // expected result case nil: @@ -62,7 +62,7 @@ func TestTtlInvalidNegative(t *testing.T) { } } -func TestTtlInvalidTooBig(t *testing.T) { +func TestTTLInvalidTooBig(t *testing.T) { srep, err := rep.NewSocket() if err != nil { t.Errorf("Failed to make REP: %v", err) @@ -70,7 +70,7 @@ func TestTtlInvalidTooBig(t *testing.T) { } defer srep.Close() - err = srep.SetOption(mangos.OptionTtl, 256) + err = srep.SetOption(mangos.OptionTTL, 256) switch err { case mangos.ErrBadValue: // expected result case nil: @@ -80,7 +80,7 @@ func TestTtlInvalidTooBig(t *testing.T) { } } -func TestTtlInvalidNotInt(t *testing.T) { +func TestTTLInvalidNotInt(t *testing.T) { srep, err := rep.NewSocket() if err != nil { t.Errorf("Failed to make REP: %v", err) @@ -88,7 +88,7 @@ func TestTtlInvalidNotInt(t *testing.T) { } defer srep.Close() - err = srep.SetOption(mangos.OptionTtl, "garbage") + err = srep.SetOption(mangos.OptionTTL, "garbage") switch err { case mangos.ErrBadValue: // expected result case nil: @@ -98,7 +98,7 @@ func TestTtlInvalidNotInt(t *testing.T) { } } -func TestTtlSet(t *testing.T) { +func TestTTLSet(t *testing.T) { srep, err := rep.NewSocket() if err != nil { t.Errorf("Failed to make REP: %v", err) @@ -106,13 +106,13 @@ func TestTtlSet(t *testing.T) { } defer srep.Close() - err = srep.SetOption(mangos.OptionTtl, 2) + err = srep.SetOption(mangos.OptionTTL, 2) if err != nil { t.Errorf("Failed SetOption: %v", err) return } - v, err := srep.GetOption(mangos.OptionTtl) + v, err := srep.GetOption(mangos.OptionTTL) if err != nil { t.Errorf("Failed GetOption: %v", err) return @@ -124,7 +124,7 @@ func TestTtlSet(t *testing.T) { } } -func TestTtlDrop(t *testing.T) { +func TestTTLDrop(t *testing.T) { nhop := 3 srep := make([]mangos.Socket, 0, nhop) sreq := make([]mangos.Socket, 0, nhop) @@ -211,7 +211,7 @@ func TestTtlDrop(t *testing.T) { } // Now try setting the option - err = rp.SetOption(mangos.OptionTtl, nhop-1) + err = rp.SetOption(mangos.OptionTTL, nhop-1) if err != nil { t.Errorf("Failed set TTL: %v", err) return diff --git a/transport.go b/transport.go index be95b84..6da093c 100644 --- a/transport.go +++ b/transport.go @@ -141,6 +141,8 @@ type Transport interface { NewListener(url string, sock Socket) (PipeListener, error) } +// StripScheme removes the leading scheme (such as "http://") from an address +// string. This is mostly a utility for benefit of transport providers. func StripScheme(t Transport, addr string) (string, error) { if !strings.HasPrefix(addr, t.Scheme()+"://") { return addr, ErrBadTran diff --git a/transport/ipc/ipc.go b/transport/ipc/ipc.go index d0f06c6..69e20b0 100644 --- a/transport/ipc/ipc.go +++ b/transport/ipc/ipc.go @@ -29,11 +29,11 @@ func (o options) get(name string) (interface{}, error) { if o == nil { return nil, mangos.ErrBadOption } - if v, ok := o[name]; !ok { + v, ok := o[name] + if !ok { return nil, mangos.ErrBadOption - } else { - return v, nil } + return v, nil } // SetOption sets an option. We have none, so just ErrBadOption. @@ -76,11 +76,11 @@ type listener struct { // Listen implements the PipeListener Listen method. func (l *listener) Listen() error { - if listener, err := net.ListenUnix("unix", l.addr); err != nil { + listener, err := net.ListenUnix("unix", l.addr) + if err != nil { return err - } else { - l.listener = listener } + l.listener = listener return nil } diff --git a/transport/tcp/tcp.go b/transport/tcp/tcp.go index 3d0f9d8..adf1fe9 100644 --- a/transport/tcp/tcp.go +++ b/transport/tcp/tcp.go @@ -26,11 +26,11 @@ type options map[string]interface{} // GetOption retrieves an option value. func (o options) get(name string) (interface{}, error) { - if v, ok := o[name]; !ok { + v, ok := o[name] + if !ok { return nil, mangos.ErrBadOption - } else { - return v, nil } + return v, nil } // SetOption sets an option. diff --git a/transport/tcp/tcp_test.go b/transport/tcp/tcp_test.go index 2a481b4..907c922 100644 --- a/transport/tcp/tcp_test.go +++ b/transport/tcp/tcp_test.go @@ -317,30 +317,30 @@ func TestTCPOptions(t *testing.T) { return } - if val, err := d.GetOption(n); err != nil { + val, err := d.GetOption(n) + if err != nil { t.Errorf("Get option %s failed: %v", n, err) return - } else { - switch v := val.(type) { - case bool: - if !v { - t.Errorf("Option %s value not true", n) - return - } - default: - t.Errorf("Option %s wrong type!", n) + } + switch v := val.(type) { + case bool: + if !v { + t.Errorf("Option %s value not true", n) return } + default: + t.Errorf("Option %s wrong type!", n) + return } - if err := d.SetOption(n, 1234); err != mangos.ErrBadValue { + if err = d.SetOption(n, 1234); err != mangos.ErrBadValue { t.Errorf("Expected ErrBadValue, but did not get it") return } } // Negative test: try a bad option - if err := d.SetOption("NO-SUCH-OPTION", 0); err != mangos.ErrBadOption { + if err = d.SetOption("NO-SUCH-OPTION", 0); err != mangos.ErrBadOption { t.Errorf("Expected ErrBadOption, but did not get it") return } diff --git a/transport/tlstcp/tlstcp.go b/transport/tlstcp/tlstcp.go index 50b2d4e..e9d099a 100644 --- a/transport/tlstcp/tlstcp.go +++ b/transport/tlstcp/tlstcp.go @@ -33,7 +33,7 @@ func (o options) get(name string) (interface{}, error) { func (o options) set(name string, val interface{}) error { switch name { - case mangos.OptionTlsConfig: + case mangos.OptionTLSConfig: switch v := val.(type) { case *tls.Config: // Make a private copy @@ -68,7 +68,7 @@ func (o options) configTCP(conn *net.TCPConn) error { func newOptions(t *tlsTran) options { o := make(map[string]interface{}) - o[mangos.OptionTlsConfig] = t.config + o[mangos.OptionTLSConfig] = t.config return options(o) } @@ -89,12 +89,12 @@ func (d *dialer) Dial() (mangos.Pipe, error) { tconn.Close() return nil, err } - if v, ok := d.opts[mangos.OptionTlsConfig]; ok { + if v, ok := d.opts[mangos.OptionTLSConfig]; ok { config = v.(*tls.Config) } conn := tls.Client(tconn, config) return mangos.NewConnPipe(conn, d.sock, - mangos.PropTlsConnState, conn.ConnectionState()) + mangos.PropTLSConnState, conn.ConnectionState()) } func (d *dialer) SetOption(n string, v interface{}) error { @@ -116,16 +116,16 @@ type listener struct { func (l *listener) Listen() error { var err error - if v, ok := l.opts[mangos.OptionTlsConfig]; !ok { - return mangos.ErrTlsNoConfig - } else { - l.config = v.(*tls.Config) + v, ok := l.opts[mangos.OptionTLSConfig] + if !ok { + return mangos.ErrTLSNoConfig } + l.config = v.(*tls.Config) if l.config == nil { - return mangos.ErrTlsNoConfig + return mangos.ErrTLSNoConfig } if l.config.Certificates == nil || len(l.config.Certificates) == 0 { - return mangos.ErrTlsNoCert + return mangos.ErrTLSNoCert } if l.listener, err = net.ListenTCP("tcp", l.addr); err != nil { diff --git a/transport/ws/ws.go b/transport/ws/ws.go index 574967c..b75d77e 100644 --- a/transport/ws/ws.go +++ b/transport/ws/ws.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package ws implements an simple websocket transport for mangos. +// Package ws implements a simple WebSocket transport for mangos. // This transport is considered EXPERIMENTAL. package ws @@ -55,11 +55,11 @@ func (o options) get(name string) (interface{}, error) { if o == nil { return nil, mangos.ErrBadOption } - if v, ok := o[name]; !ok { + v, ok := o[name] + if !ok { return nil, mangos.ErrBadOption - } else { - return v, nil } + return v, nil } // SetOption sets an option. We have none, so just ErrBadOption. @@ -72,7 +72,7 @@ func (o options) set(name string, val interface{}) error { case bool: o[name] = v } - case mangos.OptionTlsConfig: + case mangos.OptionTLSConfig: switch v := val.(type) { case *tls.Config: // Make a private copy. @@ -106,13 +106,13 @@ type wsTran int func (w *wsPipe) Recv() (*mangos.Message, error) { // We ignore the message type for receive. - if _, body, err := w.ws.ReadMessage(); err != nil { + _, body, err := w.ws.ReadMessage() + if err != nil { return nil, err - } else { - msg := mangos.NewMessage(0) - msg.Body = body - return msg, nil } + msg := mangos.NewMessage(0) + msg.Body = body + return msg, nil } func (w *wsPipe) Send(m *mangos.Message) error { @@ -173,7 +173,7 @@ func (d *dialer) Dial() (mangos.Pipe, error) { wd := &websocket.Dialer{} wd.Subprotocols = []string{d.proto.PeerName() + ".sp.nanomsg.org"} - if v, ok := d.opts[mangos.OptionTlsConfig]; ok { + if v, ok := d.opts[mangos.OptionTLSConfig]; ok { wd.TLSClientConfig = v.(*tls.Config) } @@ -210,7 +210,7 @@ type listener struct { ug websocket.Upgrader htsvr *http.Server mux *http.ServeMux - url_ *url.URL + url *url.URL listener net.Listener proto mangos.Protocol opts options @@ -243,13 +243,13 @@ func (l *listener) Listen() error { var tcfg *tls.Config if l.iswss { - v, ok := l.opts[mangos.OptionTlsConfig] + v, ok := l.opts[mangos.OptionTLSConfig] if !ok || v == nil { - return mangos.ErrTlsNoConfig + return mangos.ErrTLSNoConfig } tcfg = v.(*tls.Config) if tcfg.Certificates == nil || len(tcfg.Certificates) == 0 { - return mangos.ErrTlsNoCert + return mangos.ErrTLSNoCert } } @@ -257,7 +257,7 @@ func (l *listener) Listen() error { // case of a port already in use. This also lets us configure // properties of the underlying TCP connection. - if taddr, err = net.ResolveTCPAddr("tcp", l.url_.Host); err != nil { + if taddr, err = net.ResolveTCPAddr("tcp", l.url.Host); err != nil { return err } @@ -271,7 +271,7 @@ func (l *listener) Listen() error { l.pending = nil l.running = true - l.htsvr = &http.Server{Addr: l.url_.Host, Handler: l.mux} + l.htsvr = &http.Server{Addr: l.url.Host, Handler: l.mux} go l.htsvr.Serve(l.listener) @@ -325,7 +325,7 @@ func (l *listener) handler(ws *websocket.Conn, req *http.Request) { w.props[mangos.PropRemoteAddr] = ws.RemoteAddr() if req.TLS != nil { - w.props[mangos.PropTlsConnState] = *req.TLS + w.props[mangos.PropTLSConnState] = *req.TLS } w.wg.Add(1) @@ -373,7 +373,7 @@ func (l *listener) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func (l *listener) Address() string { - return l.url_.String() + return l.url.String() } func (wsTran) Scheme() string { @@ -402,7 +402,7 @@ func (t wsTran) NewListener(addr string, sock mangos.Socket) (mangos.PipeListene if v, e := sock.GetOption(mangos.OptionMaxRecvSize); e == nil { l.maxrx = v.(int) } - l.mux.Handle(l.url_.Path, l) + l.mux.Handle(l.url.Path, l) } return l, e } @@ -416,16 +416,16 @@ func (wsTran) listener(addr string, proto mangos.Protocol) (*listener, error) { if strings.HasPrefix(addr, "wss://") { l.iswss = true } - l.url_, err = url.ParseRequestURI(addr) + l.url, err = url.ParseRequestURI(addr) if err != nil { return nil, err } - if len(l.url_.Path) == 0 { - l.url_.Path = "/" + if len(l.url.Path) == 0 { + l.url.Path = "/" } l.mux = http.NewServeMux() - l.htsvr = &http.Server{Addr: l.url_.Host, Handler: l.mux} + l.htsvr = &http.Server{Addr: l.url.Host, Handler: l.mux} return l, nil } diff --git a/transport/ws/ws_test.go b/transport/ws/ws_test.go index 1eb5f3d..a530224 100644 --- a/transport/ws/ws_test.go +++ b/transport/ws/ws_test.go @@ -23,17 +23,17 @@ import ( var tt = test.NewTranTest(NewTransport(), "ws://127.0.0.1:3395/mysock") func TestWebsockListenAndAccept(t *testing.T) { - tt.TranTestListenAndAccept(t) + tt.TestListenAndAccept(t) } func TestWebsockDuplicateListen(t *testing.T) { - tt.TranTestDuplicateListen(t) + tt.TestDuplicateListen(t) } func TestWebsockConnRefused(t *testing.T) { - tt.TranTestConnRefused(t) + tt.TestConnRefused(t) } func TestWebsockSendRecv(t *testing.T) { - tt.TranTestSendRecv(t) + tt.TestSendRecv(t) } diff --git a/transport/wss/wss.go b/transport/wss/wss.go index eec9408..7b19c11 100644 --- a/transport/wss/wss.go +++ b/transport/wss/wss.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package ws implements an simple websocket transport for mangos. +// Package wss implements a secure WebSocket transport for mangos. // This transport is considered EXPERIMENTAL. package wss diff --git a/util.go b/util.go index 5c345dd..5adcc54 100644 --- a/util.go +++ b/util.go @@ -52,8 +52,11 @@ func debugf(format string, args ...interface{}) { } } +// DrainChannel waits for the channel of Messages to finish +// emptying (draining) for up to the expiration. It returns +// true if the drain completed (the channel is empty), false otherwise. func DrainChannel(ch chan<- *Message, expire time.Time) bool { - var dur time.Duration = time.Millisecond * 10 + var dur = time.Millisecond * 10 for { if len(ch) == 0 { diff --git a/waiter.go b/waiter.go index 84429a2..e61f592 100644 --- a/waiter.go +++ b/waiter.go @@ -44,9 +44,8 @@ func (cv *CondTimed) WaitAbsTimeout(when time.Time) bool { now := time.Now() if when.After(now) { return cv.WaitRelTimeout(when.Sub(now)) - } else { - return cv.WaitRelTimeout(time.Duration(0)) } + return cv.WaitRelTimeout(time.Duration(0)) } // Waiter is a way to wait for completion, but it includes a timeout. It