Skip to content

Commit

Permalink
Added some trivial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helios-ag committed Oct 13, 2018
1 parent 4d4ec6a commit b157d80
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion checkers/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
//
// "Url" is _required_; memcached connection url, format is "10.0.0.1:11011". Port (:11011) is mandatory
// "Timeout" defines timeout for socket write/read (useful for servers hosted on different machine)
// "Ping" is optional; Ping establishes tcp connection to redis server.
// "Ping" is optional; Ping establishes tcp connection to memcached server.
type MemcachedConfig struct {
Url string
Timeout int32
Expand Down
51 changes: 48 additions & 3 deletions checkers/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/bradfitz/gomemcache/memcache"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
"math/rand"
"strconv"
"testing"
Expand Down Expand Up @@ -40,6 +39,19 @@ func TestNewMemcached(t *testing.T) {
Expect(err.Error()).To(ContainSubstring("unable to validate memcached config"))
Expect(mc).To(BeNil())
})

t.Run("Memcached should contain Client and Config", func(t *testing.T) {
url := testUrl
cfg := &MemcachedConfig{
Url: url,
Ping: true,
}
mc, err := NewMemcached(cfg)

Expect(err).ToNot(HaveOccurred())
Expect(mc).ToNot(BeNil())
})

}

func TestValidateMemcachedConfig(t *testing.T) {
Expand Down Expand Up @@ -102,6 +114,18 @@ func TestValidateMemcachedConfig(t *testing.T) {
Expect(err.Error()).To(ContainSubstring("Unable to parse URL"))
})

t.Run("Shouldn't error with properly set config", func(t *testing.T) {
cfg := &MemcachedConfig{
Url: testUrl,
Get: &MemcachedGetOptions{
Key: "should_return_valid",
Expect: []byte("should_return_valid"),
},
}
err := validateMemcachedConfig(cfg)
Expect(err).To(BeNil())
})

}

func TestMemcachedStatus(t *testing.T) {
Expand Down Expand Up @@ -182,6 +206,27 @@ func TestMemcachedStatus(t *testing.T) {
Expect(err).ToNot(HaveOccurred())
Expect(val.Value).To(Equal([]byte(MemcachedDefaultSetValue)))
})

t.Run("should use default .Value if .Value is set to empty string", func(t *testing.T) {
cfg := &MemcachedConfig{
Set: &MemcachedSetOptions{
Key: "should_return_default",
Value: "",
},
}
checker, server, err := setupMemcached(cfg)
if err != nil {
t.Fatal(err)
}
defer server.Close()

_, err = checker.Status()
Expect(err).ToNot(HaveOccurred())

val, err := checker.wrapper.GetClient().Get(cfg.Set.Key)
Expect(err).ToNot(HaveOccurred())
Expect(val.Value).To(Equal([]byte(MemcachedDefaultSetValue)))
})
})

t.Run("When get is enabled", func(t *testing.T) {
Expand Down Expand Up @@ -312,7 +357,7 @@ type MockMemcachedClient struct {}

func (m *MockMemcachedClient) Get(key string) (item *memcache.Item, err error) {
if emulateServerShutdown {
return nil, errors.New("Unable to complete get")
return nil, fmt.Errorf("Unable to complete get")
}
switch key {
case "should_return_valid":
Expand All @@ -330,7 +375,7 @@ func (m *MockMemcachedClient) Get(key string) (item *memcache.Item, err error) {

func (m *MockMemcachedClient) Set(item *memcache.Item) error {
if emulateServerShutdown {
return errors.New("Unable to complete set")
return fmt.Errorf("Unable to complete set")
}
return nil
}

0 comments on commit b157d80

Please sign in to comment.