forked from basho/riak-go-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection_test.go
103 lines (97 loc) · 3.16 KB
/
connection_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2015-present Basho Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package riak
import (
"net"
"testing"
)
func TestCreateConnection(t *testing.T) {
addr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:8098")
if err != nil {
t.Error(err.Error())
}
opts := &connectionOptions{
remoteAddress: addr,
connectTimeout: tenSeconds,
requestTimeout: tenSeconds,
tempNetErrorRetries: 10,
}
var conn *connection
if conn, err = newConnection(opts); err == nil {
if conn.addr.Port != 8098 {
t.Errorf("expected port 8098, got: %s", string(conn.addr.Port))
}
if conn.addr.Zone != "" {
t.Errorf("expected empty zone, got: %s", string(conn.addr.Zone))
}
if !conn.addr.IP.Equal(localhost) {
t.Errorf("expected %v, got: %v", localhost, conn.addr.IP)
}
if conn.connectTimeout != tenSeconds {
t.Errorf("expected %v, got: %v", tenSeconds, conn.connectTimeout)
}
if conn.requestTimeout != tenSeconds {
t.Errorf("expected %v, got: %v", tenSeconds, conn.requestTimeout)
}
if expected, actual := false, conn.inFlight; expected != actual {
t.Errorf("expected %v, got: %v", expected, actual)
}
if got, want := conn.tempNetErrorRetries, opts.tempNetErrorRetries; got != want {
t.Errorf("got %v, want %v", got, want)
}
} else {
t.Error(err.Error())
}
}
func TestCreateConnectionWithBadAddress(t *testing.T) {
_, err := net.ResolveTCPAddr("tcp4", "123456.89.9813948.19328419348:80983r6")
if err == nil {
t.Error("expected error")
}
}
func TestCreateConnectionRequiresOptions(t *testing.T) {
if _, err := newConnection(nil); err == nil {
t.Error("expected error when creating Connection without options")
}
}
func TestEnsureDefaultConnectionValues(t *testing.T) {
addr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:8087")
if err != nil {
t.Error(err.Error())
}
opts := &connectionOptions{remoteAddress: addr}
var conn *connection
if conn, err = newConnection(opts); err == nil {
if conn.addr.Port != 8087 {
t.Errorf("expected port 8087, got: %s", string(conn.addr.Port))
}
if conn.addr.Zone != "" {
t.Errorf("expected empty zone, got: %s", string(conn.addr.Zone))
}
if !conn.addr.IP.Equal(localhost) {
t.Errorf("expected %v, got: %v", localhost, conn.addr.IP)
}
if conn.connectTimeout != defaultConnectTimeout {
t.Errorf("expected %v, got: %v", defaultConnectTimeout, conn.connectTimeout)
}
if conn.requestTimeout != defaultRequestTimeout {
t.Errorf("expected %v, got: %v", defaultRequestTimeout, conn.requestTimeout)
}
if expected, actual := false, conn.inFlight; expected != actual {
t.Errorf("expected %v, got: %v", expected, actual)
}
} else {
t.Error(err.Error())
}
}