Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Node Options & more #61

Merged
merged 1 commit into from
Feb 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions cluster_i_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,65 @@ func TestExecuteConcurrentCommandsOnCluster(t *testing.T) {
}
}

func TestExecuteConcurrentCommandsOnClusterWithMaxConnections(t *testing.T) {
nodeOpts := &NodeOptions{
RemoteAddress: getRiakAddress(),
}

var node *Node
var err error
if node, err = NewNode(nodeOpts); err != nil {
t.Error(err.Error())
}
if node == nil {
t.FailNow()
}

nodes := []*Node{node}
opts := &ClusterOptions{
Nodes: nodes,
}

cluster, err := NewCluster(opts)
if err != nil {
t.Error(err.Error())
}

defer func() {
if err := cluster.Stop(); err != nil {
t.Error(err.Error())
}
}()

if err := cluster.Start(); err != nil {
t.Error(err.Error())
}

count := nodeOpts.MaxConnections
pingChan := make(chan *PingCommand)
for i := uint16(0); i < count; i++ {
go func() {
command := &PingCommand{}
if err := cluster.Execute(command); err != nil {
t.Error(err.Error())
}
pingChan <- command
}()
}

j := uint16(0)
for i := uint16(0); i < count; i++ {
pingCommand := <-pingChan
if expected, actual := true, pingCommand.Success(); expected != actual {
t.Errorf("expected %v, got %v", expected, actual)
}
j++
}
if expected, actual := count, j; expected != actual {
t.Errorf("expected %v, got %v", expected, actual)
}
}

func TestExecuteCommandThreeTimesOnDifferentNodes(t *testing.T) {
nodeCount := 3
listenerChan := make(chan bool, nodeCount)
Expand Down
18 changes: 18 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ func NewNode(options *NodeOptions) (*Node, error) {
if options.RemoteAddress == "" {
options.RemoteAddress = defaultRemoteAddress
}
if options.MinConnections == 0 {
options.MinConnections = defaultMinConnections
}
if options.MaxConnections == 0 {
options.MaxConnections = defaultMaxConnections
}
if options.TempNetErrorRetries == 0 {
options.TempNetErrorRetries = defaultTempNetErrorRetries
}
if options.IdleTimeout == 0 {
options.IdleTimeout = defaultIdleTimeout
}
if options.ConnectTimeout == 0 {
options.ConnectTimeout = defaultConnectTimeout
}
if options.RequestTimeout == 0 {
options.RequestTimeout = defaultRequestTimeout
}
if options.HealthCheckInterval == 0 {
options.HealthCheckInterval = defaultHealthCheckInterval
}
Expand Down
16 changes: 8 additions & 8 deletions node_i_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func TestRecoverViaDefaultPingHealthCheck(t *testing.T) {

go func() {
opts := &NodeOptions{
RemoteAddress: tl.addr.String(),
MinConnections: 0,
RemoteAddress: tl.addr.String(),
MinConnections: 0,
}
node, err := NewNode(opts)
if err != nil {
Expand Down Expand Up @@ -154,12 +154,12 @@ func TestRecoverViaDefaultPingHealthCheck(t *testing.T) {

for {
select {
case <-doneChan:
logDebug("[TestRecoverViaDefaultPingHealthCheck]", "stopping node")
node.stop()
return
case <-time.After(time.Second * 1):
logDebug("[TestRecoverViaDefaultPingHealthCheck]", "still waiting to stop node...")
case <-doneChan:
logDebug("[TestRecoverViaDefaultPingHealthCheck]", "stopping node")
node.stop()
return
case <-time.After(time.Second * 1):
logDebug("[TestRecoverViaDefaultPingHealthCheck]", "still waiting to stop node...")
}
}
}()
Expand Down
42 changes: 42 additions & 0 deletions node_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package riak

import (
"fmt"
"net"
"testing"
)
Expand Down Expand Up @@ -64,6 +65,47 @@ func TestCreateNodeWithOptions(t *testing.T) {
}
}

func TestEnsureNodeValuesWithZeroValOptions(t *testing.T) {
opts := &NodeOptions{
TempNetErrorRetries: 8,
}
node, err := NewNode(opts)
if err != nil {
t.Error(err.Error())
}
exp := fmt.Sprintf("%s|0|0", defaultRemoteAddress)
if expected, actual := exp, node.String(); expected != actual {
t.Errorf("expected %v, got %v", expected, actual)
}
if expected, actual := nodeCreated, node.getState(); expected != actual {
t.Errorf("expected %v, got %v", expected, actual)
}
if node.addr.Port != int(defaultRemotePort) {
t.Errorf("expected port %v, got: %v", defaultRemotePort, node.addr.Port)
}
if expected, actual := node.cm.minConnections, defaultMinConnections; expected != actual {
t.Errorf("expected %v, got: %v", expected, actual)
}
if expected, actual := node.cm.maxConnections, defaultMaxConnections; expected != actual {
t.Errorf("expected %v, got: %v", expected, actual)
}
if expected, actual := node.cm.idleTimeout, defaultIdleTimeout; expected != actual {
t.Errorf("expected %v, got: %v", expected, actual)
}
if expected, actual := node.cm.connectTimeout, defaultConnectTimeout; expected != actual {
t.Errorf("expected %v, got: %v", expected, actual)
}
if expected, actual := node.cm.requestTimeout, defaultRequestTimeout; expected != actual {
t.Errorf("expected %v, got: %v", expected, actual)
}
if got, want := node.cm.tempNetErrorRetries, opts.TempNetErrorRetries; got != want {
t.Errorf("got %v, want %v", got, want)
}
if expected, actual := node.healthCheckInterval, defaultHealthCheckInterval; expected != actual {
t.Errorf("expected %v, got: %v", expected, actual)
}
}

func TestEnsureDefaultNodeValues(t *testing.T) {
node, err := NewNode(nil)
if err != nil {
Expand Down